Skip to content

Commit ee812ec

Browse files
committed
Add Jest snaphost resolver
1 parent 5bd6b4e commit ee812ec

63 files changed

Lines changed: 51 additions & 13 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
dist/
33
types/
44
/test/restDocs/*.json
5-
/test/restDocs/results/
6-
/test/restDocs/snapshots/
5+
/test/restDocs/__tests__/
6+
/test/restDocs/__snapshots__/

.prettierignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
/dist
44
/test/*cache.json
55
/test/restDocs/*.json
6-
/test/restDocs/results/
7-
/test/restDocs/snapshots/
6+
/test/restDocs/__tests__/
7+
/test/restDocs/__snapshots__/

bin/cspell.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const files = (await sh.runSh('npx -y ls-ignore --paths')).stdout
55
.split('\n')
66
.filter(x => x.trim() !== '')
77
.filter(x => !(x.startsWith('test/restDocs') && x.endsWith('.json')))
8-
.filter(x => !x.startsWith('test/restDocs/results/'))
9-
.filter(x => !x.startsWith('test/restDocs/snapshots/'));
8+
.filter(x => !x.startsWith('test/restDocs/__tests__/'))
9+
.filter(x => !x.startsWith('test/restDocs/__snapshots__/'));
1010
await sh.runSh(`npx -y cspell ${files.join(' ')}`);

custom-resolver.cjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const path = require('path');
2+
var fs = require('fs');
3+
const restDocs = fs.realpathSync('./test/restDocs');
4+
5+
const isChildOf = (parent, child) => {
6+
const relative = path.relative(parent, child);
7+
return relative && !relative.startsWith('..') && !path.isAbsolute(relative);
8+
};
9+
10+
module.exports = {
11+
// resolves from test to snapshot path
12+
resolveSnapshotPath: (testPath, snapshotExtension) => {
13+
if (isChildOf(restDocs, testPath)) {
14+
return testPath.replace('__tests__', '__snapshots__') + snapshotExtension;
15+
}
16+
return path.join(
17+
path.join(path.dirname(testPath), '__snapshots__'),
18+
path.basename(testPath) + snapshotExtension
19+
);
20+
},
21+
// resolves from snapshot to test path
22+
resolveTestPath: (snapshotFilePath, snapshotExtension) => {
23+
if (isChildOf(restDocs, snapshotFilePath)) {
24+
return snapshotFilePath
25+
.replace('__snapshots__', '__tests__')
26+
.slice(0, -snapshotExtension.length);
27+
}
28+
return path.join(
29+
path.dirname(snapshotFilePath),
30+
'..',
31+
path.basename(snapshotFilePath, snapshotExtension)
32+
);
33+
},
34+
// Example test path, used for preflight consistency check of the implementation above
35+
testPathForConsistencyCheck: 'some/__tests__/example.test.js',
36+
};

jest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const config: JestConfigWithTsJest = {
1313
moduleNameMapper: {
1414
'^(\\.{1,2}/.*)\\.js$': '$1',
1515
},
16+
// TODO: replace with ts after https://github.com/facebook/jest/pull/12014 is resolved
17+
snapshotResolver: './custom-resolver.cjs',
1618
testRegex: '/test/.*(?<!\\.integration)\\.spec\\.ts$',
1719
};
1820

test/restDocs/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
results/
1+
__tests__/

test/restDocs/snapshots/dt/gapi.client.admin-directory_v1/index.d.ts.shot renamed to test/restDocs/__snapshots__/dt/gapi.client.admin-directory_v1/index.d.ts.snap

File renamed without changes.

test/restDocs/snapshots/dt/gapi.client.admin-directory_v1/package.json.shot renamed to test/restDocs/__snapshots__/dt/gapi.client.admin-directory_v1/package.json.snap

File renamed without changes.

test/restDocs/snapshots/dt/gapi.client.admin-directory_v1/tsconfig.json.shot renamed to test/restDocs/__snapshots__/dt/gapi.client.admin-directory_v1/tsconfig.json.snap

File renamed without changes.

test/restDocs/snapshots/dt/gapi.client.admin-directory_v1/tslint.json.shot renamed to test/restDocs/__snapshots__/dt/gapi.client.admin-directory_v1/tslint.json.snap

File renamed without changes.

0 commit comments

Comments
 (0)