Skip to content

Commit

Permalink
Apply Simen's fix
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 27, 2022
1 parent bf75889 commit 586ac6e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
32 changes: 16 additions & 16 deletions e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
expect(updateSnapshotRun.json.testResults[0].message).toMatchInlineSnapshot(`
" ● Test suite failed to run
SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (5:26):
SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (12:26):
3 |
4 | test('<div>x</div>', () => {
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
| ^
6 | <div>
7 | y
8 | </div>
10 |
11 | test('<div>x</div>', () => {
> 12 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
| ^
13 | <div>
14 | y
15 | </div>
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
Expand All @@ -66,15 +66,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
+ x
</div>
3 |
4 | test('<div>x</div>', () => {
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
| ^
6 | <div>
7 | y
8 | </div>
10 |
11 | test('<div>x</div>', () => {
> 12 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
| ^
13 | <div>
14 | y
15 | </div>
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:12:50)
"
`);
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import renderer from 'react-test-renderer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import renderer from 'react-test-renderer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const initialize = async ({
const snapshotState = new SnapshotState(snapshotPath, {
expand: globalConfig.expand,
prettierPath: config.prettierPath,
rootDir: config.rootDir,
snapshotFormat: config.snapshotFormat,
updateSnapshot: globalConfig.updateSnapshot,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/src/setup_jest_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ export default async function setupJestGlobals({

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const {prettierPath, snapshotFormat} = config;
const {prettierPath, rootDir, snapshotFormat} = config;
const snapshotResolver = await buildSnapshotResolver(config, localRequire);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
prettierPath,
rootDir,
snapshotFormat,
updateSnapshot,
});
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type InlineSnapshot = {

export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
rootDir: string,
prettierPath: string,
): void {
let prettier: Prettier | null = null;
Expand All @@ -64,6 +65,7 @@ export function saveInlineSnapshots(
saveSnapshotsForFile(
snapshotsByFile[sourceFilePath],
sourceFilePath,
rootDir,
prettier && semver.gte(prettier.version, '1.5.0') ? prettier : undefined,
);
}
Expand All @@ -72,6 +74,7 @@ export function saveInlineSnapshots(
const saveSnapshotsForFile = (
snapshots: Array<InlineSnapshot>,
sourceFilePath: string,
rootDir: string,
prettier?: Prettier,
) => {
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');
Expand All @@ -96,7 +99,7 @@ const saveSnapshotsForFile = (
filename: sourceFilePath,
plugins,
presets,
root: path.dirname(sourceFilePath),
root: rootDir,
});
if (!ast) {
throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`);
Expand Down
9 changes: 8 additions & 1 deletion packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type SnapshotStateOptions = {
prettierPath: string;
expand?: boolean;
snapshotFormat: PrettyFormatOptions;
rootDir: string;
};

export type SnapshotMatchOptions = {
Expand Down Expand Up @@ -64,6 +65,7 @@ export default class SnapshotState {
private _uncheckedKeys: Set<string>;
private _prettierPath: string;
private _snapshotFormat: PrettyFormatOptions;
private _rootDir: string;

added: number;
expand: boolean;
Expand Down Expand Up @@ -92,6 +94,7 @@ export default class SnapshotState {
this._updateSnapshot = options.updateSnapshot;
this.updated = 0;
this._snapshotFormat = options.snapshotFormat;
this._rootDir = options.rootDir;
}

markSnapshotsAsCheckedForTest(testName: string): void {
Expand Down Expand Up @@ -154,7 +157,11 @@ export default class SnapshotState {
saveSnapshotFile(this._snapshotData, this._snapshotPath);
}
if (hasInlineSnapshots) {
saveInlineSnapshots(this._inlineSnapshots, this._prettierPath);
saveInlineSnapshots(
this._inlineSnapshots,
this._rootDir,
this._prettierPath,
);
}
status.saved = true;
} else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) {
Expand Down

0 comments on commit 586ac6e

Please sign in to comment.