Skip to content

Commit

Permalink
fix: fix error cause bug
Browse files Browse the repository at this point in the history
  • Loading branch information
BondarenkoAlex committed Oct 8, 2024
1 parent 0a0a9f7 commit 788e667
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
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`
- `[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
46 changes: 46 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,38 @@ describe('toThrow', () => {
throw new Error('good', {cause: errorA});
}).not.toThrow(expected);
});

test('isNot false, cause is object', () => {
jestExpect(() => {
throw errorB;
}).toThrow({
message: 'B',
// eslint-disable-next-line sort-keys
cause: {
message: 'A',
},
});
});

test('isNot false, cause is string', () => {
jestExpect(() => {
throw new Error('Message', {cause: 'line 123'});
}).toThrow({
message: 'Message',
// eslint-disable-next-line sort-keys
cause: 'line 123',
});
});

test('isNot false, cause is boolean', () => {
jestExpect(() => {
throw new Error('Message', {cause: true});
}).toThrow({
message: 'Message',
// eslint-disable-next-line sort-keys
cause: true,
});
});
});

describe('fail', () => {
Expand All @@ -329,6 +361,20 @@ describe('toThrow', () => {
/^(?=.*Expected message and cause: ).*Received message and cause: /s,
);
});

test('isNot true, incorrect cause object', () => {
expect(() =>
jestExpect(() => {
throw new Error('Message', {cause: {cause: undefined}});
}).toThrow({
message: 'Message',
// eslint-disable-next-line sort-keys
cause: {cause: null},
}),
).toThrow(
/^(?=.*Expected message and cause: ).*Received message and cause: /s,
);
});
});
});

Expand Down
32 changes: 23 additions & 9 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,36 @@ 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) {
return JSON.stringify(buildSerializeError(error));
}

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

function createMessageAndCause(error: Error) {
if (error.cause instanceof Error) {
return createMessageAndCauseMessage(error);
function buildSerializeError(error) {
if (!isObject(error)) {
return error;
}

const result = {};
for (const name of Object.getOwnPropertyNames(error)) {
if (['stack', 'fileName', 'lineNumber'].includes(name)) {
continue;
}
if (name === 'cause') {
result[name] = buildSerializeError(error['cause']);
continue;
}
result[name] = error[name];
}

return error.message;
return result;
}

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

function messageAndCause(error: Error) {
Expand Down

0 comments on commit 788e667

Please sign in to comment.