Skip to content

Commit

Permalink
Merge branch 'main' into fix/expect-util/dataview
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 20, 2023
2 parents 3257475 + 571b230 commit a280fd2
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
- `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072))
- `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965))
- `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470))

### Fixes

- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))

Expand All @@ -22,6 +24,7 @@

- `[*]` [**BREAKING**] Drop support for Node.js versions 14 and 19 ([#14460](https://github.com/jestjs/jest/pull/14460))
- `[*]` [**BREAKING**] Drop support for `[email protected]`, minimum version is now `5.0` ([#14542](https://github.com/facebook/jest/pull/14542))
- `[babel-jest, babel-preset-jest]` [**BREAKING**] Increase peer dependency of `@babel/core` to `^7.11` ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[jest-cli, jest-config, @jest/types]` [**BREAKING**] Remove deprecated `--init` argument ([#14490](https://github.com/jestjs/jest/pull/14490))

## 29.7.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ exports[`updates existing snapshot: updated snapshot 1`] = `
"
`;
exports[`works fine when function throws error with cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw ErrorWithCause(
'apple',
ErrorWithCause('banana', ErrorWithCause('orange')),
);
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: banana
Cause: orange"
\`);
});
"
`;
exports[`works fine when function throws error with string cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw ErrorWithCause('apple', 'here is a cause');
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: here is a cause"
\`);
});
"
`;
exports[`works fine when function throws error: initial write 1`] = `
"test('works fine when function throws error', () => {
expect(() => {
Expand Down
63 changes: 63 additions & 0 deletions e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,69 @@ test('works fine when function throws error', () => {
}
});

test('works fine when function throws error with cause', () => {
const filename = 'works-fine-when-function-throws-error-with-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw ErrorWithCause('apple',
ErrorWithCause('banana',
ErrorWithCause('orange')
)
);
})
.toThrowErrorMatchingInlineSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(fileAfter).toMatchSnapshot('initial write with cause');
expect(exitCode).toBe(0);
}
});

test('works fine when function throws error with string cause', () => {
const filename =
'works-fine-when-function-throws-error-with-string-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
function ErrorWithCause(message, cause) {
const err = new Error(message, {cause});
if (err.cause !== cause) {
// cause does not exist in old versions of node
err.cause = cause;
}
return err;
}
expect(() => {
throw ErrorWithCause('apple', 'here is a cause');
})
.toThrowErrorMatchingInlineSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(fileAfter).toMatchSnapshot('initial write with cause');
expect(exitCode).toBe(0);
}
});

test('updates existing snapshot', () => {
const filename = 'updates-existing-snapshot.test.js';
const template = makeTemplate(`
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@types/graceful-fs": "^4.1.3"
},
"peerDependencies": {
"@babel/core": "^7.8.0"
"@babel/core": "^7.11.0"
},
"engines": {
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const IDVisitor = {
) {
ids.add(path);
},
blacklist: [
denylist: [
'TypeAnnotation',
'TSTypeAnnotation',
'TSTypeQuery',
Expand Down Expand Up @@ -135,7 +135,7 @@ FUNCTIONS.mock = args => {

const ids: Set<NodePath<Identifier>> = new Set();
const parentScope = moduleFactory.parentPath.scope;
// @ts-expect-error: ReferencedIdentifier and blacklist are not known on visitors
// @ts-expect-error: ReferencedIdentifier and denylist are not known on visitors
moduleFactory.traverse(IDVisitor, {ids});
for (const id of ids) {
const {name} = id.node;
Expand Down Expand Up @@ -371,7 +371,7 @@ export default function jestHoist(): PluginObj<{
CallExpression: visitCallExpr,
VariableDeclarator: visitVariableDeclarator,
// do not traverse into nested blocks, or we'll hoist calls in there out to this block
blacklist: ['BlockStatement'],
denylist: ['BlockStatement'],
});
callsHoistPoint.remove();
varsHoistPoint.remove();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"babel-preset-current-node-syntax": "^1.0.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
"@babel/core": "^7.11.0"
},
"engines": {
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
Expand Down
16 changes: 15 additions & 1 deletion packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {types} from 'util';
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
import type {MatcherFunctionWithContext} from 'expect';
Expand Down Expand Up @@ -518,12 +519,25 @@ const _toThrowErrorMatchingSnapshot = (
);
}

let message = error.message;
while ('cause' in error) {
error = error.cause;
if (types.isNativeError(error) || error instanceof Error) {
message += `\nCause: ${error.message}`;
} else {
if (typeof error === 'string') {
message += `\nCause: ${error}`;
}
break;
}
}

return _toMatchSnapshot({
context,
hint,
inlineSnapshot,
isInline,
matcherName,
received: error.message,
received: message,
});
};
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6482,7 +6482,7 @@ __metadata:
graceful-fs: ^4.2.9
slash: ^3.0.0
peerDependencies:
"@babel/core": ^7.8.0
"@babel/core": ^7.11.0
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -6697,7 +6697,7 @@ __metadata:
babel-plugin-jest-hoist: "workspace:^"
babel-preset-current-node-syntax: ^1.0.0
peerDependencies:
"@babel/core": ^7.0.0
"@babel/core": ^7.11.0
languageName: unknown
linkType: soft

Expand Down

0 comments on commit a280fd2

Please sign in to comment.