Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error cause bug #15339

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

### Fixes

- `[expect]` Fix `error causes` bug ([#15339](https://github.com/jestjs/jest/pull/15339))
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
Expand Down
31 changes: 31 additions & 0 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,37 @@ describe('toThrow', () => {
throw new Error('good', {cause: errorA});
}).not.toThrow(expected);
});

test('isNot false, compare Error with object', () => {
jestExpect(() => {
throw errorB;
}).toThrow({
cause: {
message: 'A',
},
message: 'B',
});
});

test('isNot false, cause is string', () => {
jestExpect(() => {
throw new Error('Message', {cause: 'line 123'});
}).toThrow({
cause: 'line 123',
message: 'Message',
});
});

test('isNot false, cause is object', () => {
jestExpect(() => {
throw new Error('Message', {
cause: {prop1: true, prop2: false, prop3: null, prop4: undefined},
});
}).toThrow({
cause: {prop1: true, prop2: false, prop3: null, prop4: undefined},
message: 'Message',
});
});
});

describe('fail', () => {
Expand Down
41 changes: 31 additions & 10 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,43 @@ const formatStack = (thrown: Thrown | null) =>
},
);

function createMessageAndCauseMessage(error: Error): string {
if (error.cause instanceof Error) {
return `{ message: ${error.message}, cause: ${createMessageAndCauseMessage(
error.cause,
)}}`;
function createMessageAndCause(error: Error) {
if (error.cause) {
const seen = new WeakSet();
return JSON.stringify(buildSerializeError(error), (_, value) => {
if (value !== null && value === undefined && typeof value === 'object') {
if (seen.has(value)) return;
seen.add(value); // stop circular references
}
return value === undefined ? String(undefined) : value;
});
}

return `{ message: ${error.message} }`;
return error.message;
}

function createMessageAndCause(error: Error) {
if (error.cause instanceof Error) {
return createMessageAndCauseMessage(error);
function buildSerializeError(error: {[key: string]: any}) {
if (!isObject(error)) {
return error;
}

return error.message;
const result: {[key: string]: any} = {};
for (const name of Object.getOwnPropertyNames(error).sort()) {
if (['stack', 'fileName', 'lineNumber'].includes(name)) {
continue;
}
if (name === 'cause') {
result[name] = buildSerializeError(error['cause']);
continue;
}
result[name] = error[name];
}

return result;
}

function isObject(obj: unknown) {
return !!obj && typeof obj === 'object';
}

function messageAndCause(error: Error) {
Expand Down
Loading