-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resolver for custom snapshots paths (#6143)
* Add resolver for snapshot paths * Remove snapshotResolver from versioned doc * Simplify resolveSnapshotPath * Make error feedback more actionable * Assert test result before snapshot file Should help troubleshoot failing tests on CI * Run integration test with correct flags Same as the base snapshot.test.js * Add tests for malformed resolver module * Resolve paths in tests like implementation To avoid cross-platform mismatches * Rename integration-tests => e2e * Fix code review feedback * Add changelog entry * Fix review comments for e2e/__tests__/snapshot_resolver.test.js * Fix prettier error * Move changelog entry to correct place * Move up type import above normal imports * Add consistency check * Update snapshot_resolver.js
- Loading branch information
Showing
32 changed files
with
396 additions
and
53 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const runJest = require('../runJest'); | ||
|
||
const snapshotDir = path.resolve( | ||
__dirname, | ||
'../snapshot-resolver/__snapshots__', | ||
); | ||
const snapshotFile = path.resolve(snapshotDir, 'snapshot.test.js.snap'); | ||
|
||
describe('Custom snapshot resolver', () => { | ||
const cleanup = () => { | ||
if (fs.existsSync(snapshotFile)) { | ||
fs.unlinkSync(snapshotFile); | ||
} | ||
if (fs.existsSync(snapshotDir)) { | ||
fs.rmdirSync(snapshotDir); | ||
} | ||
}; | ||
|
||
beforeEach(cleanup); | ||
afterAll(cleanup); | ||
|
||
it('Resolves snapshot files using custom resolver', () => { | ||
const result = runJest('snapshot-resolver', ['-w=1', '--ci=false']); | ||
|
||
expect(result.stderr).toMatch('1 snapshot written from 1 test suite'); | ||
|
||
// $FlowFixMe dynamic require | ||
const content = require(snapshotFile); | ||
expect(content).toHaveProperty( | ||
'snapshots are written to custom location 1', | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test('snapshots are written to custom location', () => { | ||
expect('foobar').toMatchSnapshot(); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
resolveSnapshotPath: (testPath, snapshotExtension) => | ||
testPath.replace('__tests__', '__snapshots__') + snapshotExtension, | ||
|
||
resolveTestPath: (snapshotFilePath, snapshotExtension) => | ||
snapshotFilePath | ||
.replace('__snapshots__', '__tests__') | ||
.slice(0, -snapshotExtension.length), | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"snapshotResolver": "<rootDir>/customSnapshotResolver.js" | ||
} | ||
} |
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
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
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
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
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
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
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
13 changes: 13 additions & 0 deletions
13
packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.js.snap
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
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</>"`; | ||
exports[`malformed custom resolver in project config missing resolveSnapshotPath throws 1`] = ` | ||
"<bold>Custom snapshot resolver must implement a \`resolveSnapshotPath\` function.</> | ||
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" | ||
`; | ||
exports[`malformed custom resolver in project config missing resolveTestPath throws 1`] = ` | ||
"<bold>Custom snapshot resolver must implement a \`resolveTestPath\` function.</> | ||
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" | ||
`; |
9 changes: 9 additions & 0 deletions
9
packages/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver-inconsistent-fns.js
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
resolveSnapshotPath: (testPath, snapshotExtension) => | ||
testPath.replace('__tests__', '__snapshots__') + snapshotExtension, | ||
|
||
resolveTestPath: (snapshotFilePath, snapshotExtension) => | ||
snapshotFilePath | ||
.replace('__snapshots__', '__SPECS__') | ||
.slice(0, -snapshotExtension.length), | ||
}; |
3 changes: 3 additions & 0 deletions
3
...est-snapshot/src/__tests__/fixtures/customSnapshotResolver-missing-resolveSnapshotPath.js
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
resolveTestPath: () => {}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
...es/jest-snapshot/src/__tests__/fixtures/customSnapshotResolver-missing-resolveTestPath.js
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
resolveSnapshotPath: () => {}, | ||
}; |
Oops, something went wrong.