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: Allow updating inline snapshots when test includes JSX #12760

Merged
merged 9 commits into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
- `[@jest/expect-utils]` Fix deep equality of ImmutableJS Record ([#13055](https://github.com/facebook/jest/pull/13055))
- `[jest-haste-map]` Increase the maximum possible file size that jest-haste-map can handle ([#13094](https://github.com/facebook/jest/pull/13094))
- `[jest-snapshot]` Make `prettierPath` optional in `SnapshotState` ([#13149](https://github.com/facebook/jest/pull/13149))
- `[jest-snapshot]` Fix parsing error from inline snapshot files with `JSX` ([#12760](https://github.com/facebook/jest/pull/12760))
- `[jest-worker]` When a process runs out of memory worker exits correctly and doesn't spin indefinitely ([#13054](https://github.com/facebook/jest/pull/13054))

### Chore & Maintenance
Expand Down
49 changes: 49 additions & 0 deletions e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts
Expand Up @@ -109,3 +109,52 @@ it('successfully runs the tests with external babel config', () => {
expect(updateSnapshotRun.exitCode).toBe(0);
expect(updateSnapshotRun.stderr).toContain('1 snapshot updated.');
});

it('successfully runs the tests with inline babel config', () => {
writeFiles(DIR, {
'package.json': JSON.stringify({
...pkg,
jest: {
testEnvironment: 'jsdom',
transform: {
'^.+\\.(js|jsx)$': ['babel-jest', babelConfig],
},
},
}),
});

const normalRun = runWithJson(DIR, []);
expect(normalRun.exitCode).toBe(1);
expect(normalRun.stderr).toContain('1 snapshot failed from 1 test suite.');
expect(normalRun.json.testResults[0].message).toMatchInlineSnapshot(`
" ● <div>x</div>

expect(received).toMatchInlineSnapshot(snapshot)

Snapshot name: \`<div>x</div> 1\`

- Snapshot - 1
+ Received + 1

<div>
- y
+ x
</div>

3 |
4 | test('<div>x</div>', () => {
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
| ^
6 | <div>
7 | y
8 | </div>

at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
"
`);

const updateSnapshotRun = runJest(DIR, ['--updateSnapshot']);

expect(updateSnapshotRun.exitCode).toBe(0);
expect(updateSnapshotRun.stderr).toContain('1 snapshot updated.');
});
1 change: 1 addition & 0 deletions packages/jest-snapshot/package.json
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@babel/core": "^7.11.6",
"@babel/generator": "^7.7.2",
"@babel/plugin-syntax-jsx": "^7.7.2",
"@babel/plugin-syntax-typescript": "^7.7.2",
"@babel/traverse": "^7.7.2",
"@babel/types": "^7.3.3",
Expand Down
41 changes: 34 additions & 7 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -6,7 +6,7 @@
*/

import * as path from 'path';
import type {PluginItem} from '@babel/core';
import type {ParseResult, PluginItem} from '@babel/core';
import {Expression, File, Program, isAwaitExpression} from '@babel/types';
import * as fs from 'graceful-fs';
import type {
Expand Down Expand Up @@ -95,12 +95,39 @@ const saveSnapshotsForFile = (
// by one to formatting parser.
const snapshotMatcherNames: Array<string> = [];

const ast = parseSync(sourceFile, {
filename: sourceFilePath,
plugins,
presets,
root: rootDir,
});
let ast: ParseResult | null = null;

try {
ast = parseSync(sourceFile, {
filename: sourceFilePath,
plugins,
presets,
root: rootDir,
});
} catch (error: any) {
// attempt to recover from missing jsx plugin
if (error.message.includes('@babel/plugin-syntax-jsx')) {
try {
const jsxSyntaxPlugin: PluginItem = [
require.resolve('@babel/plugin-syntax-jsx'),
{},
// unique name to make sure Babel does not complain about a possible duplicate plugin.
'JSX syntax plugin added by Jest snapshot',
Comment on lines +113 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need this as the error is due to this plugin missing

];
ast = parseSync(sourceFile, {
filename: sourceFilePath,
plugins: [...plugins, jsxSyntaxPlugin],
presets,
root: rootDir,
});
Comment on lines +109 to +122
Copy link
Member

@SimenB SimenB Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eps1lon went for this (plus #13150)

} catch {
throw error;
}
} else {
throw error;
}
}

if (!ast) {
throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`);
}
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Expand Up @@ -1030,7 +1030,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-syntax-jsx@npm:^7.0.0, @babel/plugin-syntax-jsx@npm:^7.18.6":
"@babel/plugin-syntax-jsx@npm:^7.0.0, @babel/plugin-syntax-jsx@npm:^7.18.6, @babel/plugin-syntax-jsx@npm:^7.7.2":
version: 7.18.6
resolution: "@babel/plugin-syntax-jsx@npm:7.18.6"
dependencies:
Expand Down Expand Up @@ -12847,6 +12847,7 @@ __metadata:
dependencies:
"@babel/core": ^7.11.6
"@babel/generator": ^7.7.2
"@babel/plugin-syntax-jsx": ^7.7.2
"@babel/plugin-syntax-typescript": ^7.7.2
"@babel/preset-flow": ^7.7.2
"@babel/preset-react": ^7.12.1
Expand Down