-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
+87
−8
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d340183
Current behavior
eps1lon 23ce2ca
Apply Simen's fix
eps1lon 36b2c4d
Merge branch 'main' into fix/update-inline-snapshot-jsx
SimenB 7ed613b
update tests
SimenB e8f607f
windows?
SimenB 5d4a69b
Merge branch 'main' into fix/update-inline-snapshot-jsx
SimenB 002ec06
Merge branch 'main' into fix/update-inline-snapshot-jsx
SimenB f7d1818
fix: recover from missing jsx plugin
SimenB 2ef3386
changelog
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: recover from missing jsx plugin
commit f7d1818adfb2861422a694006c1e545e17551084
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
ast = parseSync(sourceFile, { | ||
filename: sourceFilePath, | ||
plugins: [...plugins, jsxSyntaxPlugin], | ||
presets, | ||
root: rootDir, | ||
}); | ||
Comment on lines
+109
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} catch { | ||
throw error; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
if (!ast) { | ||
throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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