Skip to content

Commit 278075e

Browse files
viddocaptain-yossarian
authored andcommitted
Require an example path for custom snapshot resolver consistency check (jestjs#7356)
1 parent 6799d48 commit 278075e

11 files changed

+77
-34
lines changed

docs/Configuration.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ The path to a module that can resolve test<->snapshot path. This config option l
700700
Example snapshot resolver module:
701701

702702
```js
703-
// my-snapshot-resolver-module
704703
module.exports = {
705704
// resolves from test to snapshot path
706705
resolveSnapshotPath: (testPath, snapshotExtension) =>
@@ -711,6 +710,9 @@ module.exports = {
711710
snapshotFilePath
712711
.replace('__snapshots__', '__tests__')
713712
.slice(0, -snapshotExtension.length),
713+
714+
// Example test path, used for preflight concistency check of the implementation above
715+
testPathForConsistencyCheck: 'some/__tests__/example.test.js',
714716
};
715717
```
716718

e2e/snapshot-resolver/customSnapshotResolver.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module.exports = {
88
snapshotFilePath
99
.replace('__snapshots__', '__tests__')
1010
.slice(0, -snapshotExtension.length),
11+
12+
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js',
1113
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`malformed custom resolver in project config inconsistent functions throws 1`] = `"<bold>Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('some-path/__tests__/snapshot_resolver.test.js')) === some-path/__SPECS__/snapshot_resolver.test.js</>"`;
3+
exports[`malformed custom resolver in project config inconsistent functions throws 1`] = `"<bold>Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('foo/__tests__/bar.test.js')) === foo/__SPECS__/bar.test.js</>"`;
44
55
exports[`malformed custom resolver in project config missing resolveSnapshotPath throws 1`] = `
6-
"<bold>Custom snapshot resolver must implement a \`resolveSnapshotPath\` function.</>
6+
"<bold>Custom snapshot resolver must implement a \`resolveSnapshotPath\` as a function.</>
77
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver"
88
`;
99
1010
exports[`malformed custom resolver in project config missing resolveTestPath throws 1`] = `
11-
"<bold>Custom snapshot resolver must implement a \`resolveTestPath\` function.</>
11+
"<bold>Custom snapshot resolver must implement a \`resolveTestPath\` as a function.</>
12+
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver"
13+
`;
14+
15+
exports[`malformed custom resolver in project config missing testPathForConsistencyCheck throws 1`] = `
16+
"<bold>Custom snapshot resolver must implement a \`testPathForConsistencyCheck\` as a string.</>
1217
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver"
1318
`;

packages/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver-inconsistent-fns.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module.exports = {
88
snapshotFilePath
99
.replace('__snapshots__', '__SPECS__')
1010
.slice(0, -snapshotExtension.length),
11+
12+
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js',
1113
};

packages/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver-missing-resolveSnapshotPath.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
module.exports = {
44
resolveTestPath: () => {},
5+
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js',
56
};

packages/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver-missing-resolveTestPath.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
module.exports = {
44
resolveSnapshotPath: () => {},
5+
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js',
56
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
2+
3+
module.exports = {
4+
resolveSnapshotPath: (testPath, snapshotExtension) => {},
5+
resolveTestPath: (snapshotFilePath, snapshotExtension) => {},
6+
};

packages/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module.exports = {
88
snapshotFilePath
99
.replace('__snapshots__', '__tests__')
1010
.slice(0, -snapshotExtension.length),
11+
12+
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js',
1113
};

packages/jest-snapshot/src/__tests__/snapshot_resolver.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ describe('malformed custom resolver in project config', () => {
9999
}).toThrowErrorMatchingSnapshot();
100100
});
101101

102+
it('missing testPathForConsistencyCheck throws ', () => {
103+
const projectConfig = newProjectConfig(
104+
'customSnapshotResolver-missing-test-path-for-consistency-check.js',
105+
);
106+
expect(() => {
107+
buildSnapshotResolver(projectConfig);
108+
}).toThrowErrorMatchingSnapshot();
109+
});
110+
102111
it('inconsistent functions throws ', () => {
103112
const projectConfig = newProjectConfig(
104113
'customSnapshotResolver-inconsistent-fns.js',

packages/jest-snapshot/src/snapshot_resolver.js

+42-30
Original file line numberDiff line numberDiff line change
@@ -25,68 +25,80 @@ export const buildSnapshotResolver = (
2525
function createSnapshotResolver(snapshotResolverPath: ?Path): SnapshotResolver {
2626
return typeof snapshotResolverPath === 'string'
2727
? createCustomSnapshotResolver(snapshotResolverPath)
28-
: {
29-
resolveSnapshotPath: (testPath: Path) =>
30-
path.join(
31-
path.join(path.dirname(testPath), '__snapshots__'),
32-
path.basename(testPath) + DOT_EXTENSION,
33-
),
28+
: createDefaultSnapshotResolver();
29+
}
30+
31+
function createDefaultSnapshotResolver() {
32+
return {
33+
resolveSnapshotPath: (testPath: Path) =>
34+
path.join(
35+
path.join(path.dirname(testPath), '__snapshots__'),
36+
path.basename(testPath) + DOT_EXTENSION,
37+
),
38+
39+
resolveTestPath: (snapshotPath: Path) =>
40+
path.resolve(
41+
path.dirname(snapshotPath),
42+
'..',
43+
path.basename(snapshotPath, DOT_EXTENSION),
44+
),
3445

35-
resolveTestPath: (snapshotPath: Path) =>
36-
path.resolve(
37-
path.dirname(snapshotPath),
38-
'..',
39-
path.basename(snapshotPath, DOT_EXTENSION),
40-
),
41-
};
46+
testPathForConsistencyCheck: path.posix.join(
47+
'consistency_check',
48+
'__tests__',
49+
'example.test.js',
50+
),
51+
};
4252
}
4353

4454
function createCustomSnapshotResolver(
4555
snapshotResolverPath: Path,
4656
): SnapshotResolver {
4757
const custom = (require(snapshotResolverPath): SnapshotResolver);
4858

49-
if (typeof custom.resolveSnapshotPath !== 'function') {
50-
throw new TypeError(mustImplement('resolveSnapshotPath'));
51-
}
52-
if (typeof custom.resolveTestPath !== 'function') {
53-
throw new TypeError(mustImplement('resolveTestPath'));
54-
}
59+
[
60+
['resolveSnapshotPath', 'function'],
61+
['resolveTestPath', 'function'],
62+
['testPathForConsistencyCheck', 'string'],
63+
].forEach(([propName, requiredType]) => {
64+
if (typeof custom[propName] !== requiredType) {
65+
throw new TypeError(mustImplement(propName, requiredType));
66+
}
67+
});
5568

5669
const customResolver = {
5770
resolveSnapshotPath: testPath =>
5871
custom.resolveSnapshotPath(testPath, DOT_EXTENSION),
5972
resolveTestPath: snapshotPath =>
6073
custom.resolveTestPath(snapshotPath, DOT_EXTENSION),
74+
testPathForConsistencyCheck: custom.testPathForConsistencyCheck,
6175
};
6276

6377
verifyConsistentTransformations(customResolver);
6478

6579
return customResolver;
6680
}
6781

68-
function mustImplement(functionName: string) {
82+
function mustImplement(propName: string, requiredType: string) {
6983
return (
7084
chalk.bold(
71-
`Custom snapshot resolver must implement a \`${functionName}\` function.`,
85+
`Custom snapshot resolver must implement a \`${propName}\` as a ${requiredType}.`,
7286
) +
7387
'\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
7488
);
7589
}
7690

7791
function verifyConsistentTransformations(custom: SnapshotResolver) {
78-
const fakeTestPath = path.posix.join(
79-
'some-path',
80-
'__tests__',
81-
'snapshot_resolver.test.js',
82-
);
83-
const transformedPath = custom.resolveTestPath(
84-
custom.resolveSnapshotPath(fakeTestPath),
92+
const resolvedSnapshotPath = custom.resolveSnapshotPath(
93+
custom.testPathForConsistencyCheck,
8594
);
86-
if (transformedPath !== fakeTestPath) {
95+
const resolvedTestPath = custom.resolveTestPath(resolvedSnapshotPath);
96+
if (resolvedTestPath !== custom.testPathForConsistencyCheck) {
8797
throw new Error(
8898
chalk.bold(
89-
`Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${fakeTestPath}')) === ${transformedPath}`,
99+
`Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${
100+
custom.testPathForConsistencyCheck
101+
}')) === ${resolvedTestPath}`,
90102
),
91103
);
92104
}

types/SnapshotResolver.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type {Path} from './Config';
44

55
export type SnapshotResolver = {|
6+
testPathForConsistencyCheck: string,
67
resolveSnapshotPath(testPath: Path): Path,
78
resoveTestPath(snapshotPath: Path): Path,
89
|};

0 commit comments

Comments
 (0)