-
-
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.
feat(snapshot): support snapshotResolver and snapshotSerializers writ…
…ten in ESM
- Loading branch information
1 parent
27b89ec
commit f8aa171
Showing
16 changed files
with
327 additions
and
29 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
110 changes: 110 additions & 0 deletions
110
e2e/snapshot-serializers-esm/__tests__/snapshot.test.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,110 @@ | ||
/** | ||
* 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. | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
describe('snapshot serializers', () => { | ||
it('works with first plugin', () => { | ||
const test = { | ||
foo: 1, | ||
}; | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with second plugin', () => { | ||
const test = { | ||
bar: 2, | ||
}; | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with nested serializable objects', () => { | ||
const test = { | ||
foo: { | ||
bar: 2, | ||
}, | ||
}; | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with default serializers', () => { | ||
const test = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: null, | ||
props: { | ||
id: 'foo', | ||
}, | ||
type: 'div', | ||
}; | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with prepended plugins and default serializers', () => { | ||
const test = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: null, | ||
props: { | ||
aProp: {a: 6}, | ||
bProp: {foo: 8}, | ||
}, | ||
type: 'div', | ||
}; | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with prepended plugins from expect method called once', () => { | ||
const test = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: null, | ||
props: { | ||
aProp: {a: 6}, | ||
bProp: {foo: 8}, | ||
}, | ||
type: 'div', | ||
}; | ||
// Add plugin that overrides foo specified by Jest config in package.json | ||
expect.addSnapshotSerializer({ | ||
print: (val, serialize) => `Foo: ${serialize(val.foo)}`, | ||
test: val => val && val.hasOwnProperty('foo'), | ||
}); | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with prepended plugins from expect method called twice', () => { | ||
const test = { | ||
$$typeof: Symbol.for('react.test.json'), | ||
children: null, | ||
props: { | ||
aProp: {a: 6}, | ||
bProp: {foo: 8}, | ||
}, | ||
type: 'div', | ||
}; | ||
// Add plugin that overrides preceding added plugin | ||
expect.addSnapshotSerializer({ | ||
print: (val, serialize) => `FOO: ${serialize(val.foo)}`, | ||
test: val => val && val.hasOwnProperty('foo'), | ||
}); | ||
expect(test).toMatchSnapshot(); | ||
}); | ||
|
||
it('works with array of strings in property matcher', () => { | ||
expect({ | ||
arrayOfStrings: ['stream'], | ||
}).toMatchSnapshot({ | ||
arrayOfStrings: ['stream'], | ||
}); | ||
}); | ||
|
||
it('works with expect.XXX within array in property matcher', () => { | ||
expect({ | ||
arrayOfStrings: ['stream'], | ||
}).toMatchSnapshot({ | ||
arrayOfStrings: [expect.any(String)], | ||
}); | ||
}); | ||
}); |
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,12 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"transform": { | ||
"\\.js$": "<rootDir>/transformer.js" | ||
}, | ||
"snapshotSerializers": [ | ||
"./plugins/foo", | ||
"<rootDir>/plugins/bar" | ||
] | ||
} | ||
} |
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,11 @@ | ||
/** | ||
* 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 {createPlugin} from '../utils'; | ||
|
||
// We inject the call to "createPlugin('bar') through the transformer" |
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,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 {createPlugin} from '../../utils'; | ||
export default createPlugin('foo'); |
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,17 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
process(src, filename) { | ||
if (/bar.js$/.test(filename)) { | ||
return `${src};\nmodule.exports = createPlugin('bar');`; | ||
} | ||
return src; | ||
}, | ||
}; |
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,12 @@ | ||
/** | ||
* 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. | ||
* | ||
*/ | ||
|
||
export const createPlugin = prop => ({ | ||
print: (val, serialize) => `${prop} - ${serialize(val[prop])}`, | ||
test: val => val && val.hasOwnProperty(prop), | ||
}); |
10 changes: 10 additions & 0 deletions
10
e2e/transform/transform-esm-snapshotResolver/__tests__/snapshot.test.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,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. | ||
*/ | ||
|
||
test('snapshots are written to custom location', () => { | ||
expect('foobar').toMatchSnapshot(); | ||
}); |
22 changes: 22 additions & 0 deletions
22
e2e/transform/transform-esm-snapshotResolver/customSnapshotResolver.mjs
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,22 @@ | ||
/** | ||
* 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 {SnapshotResolver} from 'jest-snapshot'; | ||
|
||
const snapshotResolver = { | ||
resolveSnapshotPath: (testPath, snapshotExtension) => | ||
testPath.replace('__tests__', '__snapshots__') + snapshotExtension, | ||
|
||
resolveTestPath: (snapshotFilePath, snapshotExtension) => | ||
snapshotFilePath | ||
.replace('__snapshots__', '__tests__') | ||
.slice(0, -(snapshotExtension || '').length), | ||
|
||
testPathForConsistencyCheck: 'foo/__tests__/bar.test.js', | ||
}; | ||
|
||
export default snapshotResolver; |
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.mjs" | ||
} | ||
} |
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
Oops, something went wrong.