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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: recover from missing jsx plugin
SimenB committed Aug 19, 2022
commit f7d1818adfb2861422a694006c1e545e17551084
1 change: 1 addition & 0 deletions packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
@@ -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",
41 changes: 34 additions & 7 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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}`);
}
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -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:
@@ -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