-
-
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 custom snapshot serializers (#1741)
- Loading branch information
Showing
21 changed files
with
329 additions
and
15 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
7 changes: 7 additions & 0 deletions
7
integration_tests/__tests__/__snapshots__/snapshot-serializers-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,7 @@ | ||
exports[`Snapshot serializers renders snapshot 1`] = ` | ||
Object { | ||
"snapshot serializers works with first plugin 1": "foo: 1", | ||
"snapshot serializers works with nested serializable objects 1": "foo: bar: 2", | ||
"snapshot serializers works with second plugin 1": "bar: 2", | ||
} | ||
`; |
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,44 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const {cleanup} = require('../utils'); | ||
const runJest = require('../runJest'); | ||
|
||
const testDir = path.resolve(__dirname, '../snapshot-serializers'); | ||
const snapshotsDir = path.resolve(testDir, '__tests__/__snapshots__'); | ||
const snapshotPath = path.resolve(snapshotsDir, 'snapshot-test.js.snap'); | ||
|
||
const runAndAssert = () => { | ||
const result = runJest.json('snapshot-serializers'); | ||
const json = result.json; | ||
expect(json.numTotalTests).toBe(3); | ||
expect(json.numPassedTests).toBe(3); | ||
expect(json.numFailedTests).toBe(0); | ||
expect(json.numPendingTests).toBe(0); | ||
expect(result.status).toBe(0); | ||
}; | ||
|
||
describe('Snapshot serializers', () => { | ||
beforeEach(() => cleanup(snapshotsDir)); | ||
afterEach(() => cleanup(snapshotsDir)); | ||
|
||
it('renders snapshot', () => { | ||
runAndAssert(); | ||
const snapshot = require(snapshotPath); | ||
expect(snapshot).toMatchSnapshot(); | ||
}); | ||
|
||
it('compares snapshots correctly', () => { | ||
// run twice, second run compares result with snapshot from first run | ||
runAndAssert(); | ||
runAndAssert(); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
integration_tests/snapshot-serializers/__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,35 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'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(); | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"snapshotSerializers": [ | ||
"<rootDir>/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,2 @@ | ||
const {createPlugin} = require('../utils'); | ||
module.exports = createPlugin('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,2 @@ | ||
const {createPlugin} = require('../utils'); | ||
module.exports = 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,6 @@ | ||
exports.createPlugin = prop => { | ||
return { | ||
test: val => val && val.hasOwnProperty(prop), | ||
print: (val, serialize) => `${prop}: ${serialize(val[prop])}`, | ||
}; | ||
}; |
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,19 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
const snapshot = jest.genMockFromModule('jest-snapshot'); | ||
let plugins = []; | ||
|
||
snapshot.addPlugins = p => { | ||
plugins = plugins.concat(p); | ||
}; | ||
snapshot.getPlugins = p => plugins; | ||
snapshot.__reset = () => plugins = []; | ||
|
||
module.exports = snapshot; |
30 changes: 30 additions & 0 deletions
30
packages/jest-jasmine2/src/__tests__/setup-jest-globals-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,30 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
describe('addPlugins', () => { | ||
const setup = require('../setup-jest-globals'); | ||
|
||
beforeEach(() => { | ||
require('jest-snapshot').__reset(); | ||
}); | ||
|
||
const test = serializers => { | ||
const {getPlugins} = require('jest-snapshot'); | ||
const config = { | ||
snapshotSerializers: [], | ||
}; | ||
setup({config}); | ||
expect(getPlugins()).toEqual(config.snapshotSerializers); | ||
}; | ||
|
||
it('should add plugins from an empty array', () => test([])); | ||
it('should add a single plugin', () => test(['foo'])); | ||
it('should add multiple plugins', () => test(['foo', '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
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,35 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
beforeEach(() => jest.resetModules()); | ||
|
||
const testPath = serializers => { | ||
const {addPlugins, getPlugins} = require('../plugins'); | ||
const serializerPaths = serializers.map(s => | ||
require.resolve(`./plugins/${s}`), | ||
); | ||
addPlugins(serializerPaths); | ||
const expected = serializerPaths.map(p => require(p)); | ||
|
||
const plugins = getPlugins(); | ||
expect(plugins.length).toBe(serializers.length + 2); | ||
plugins.splice(0, 2); | ||
expect(plugins).toEqual(expected); | ||
}; | ||
|
||
it('should get plugins', () => { | ||
const {getPlugins} = require('../plugins'); | ||
const plugins = getPlugins(); | ||
expect(plugins.length).toBe(2); | ||
}); | ||
|
||
it('should add plugins from an empty array', () => testPath([])); | ||
it('should add a single plugin path', () => testPath(['foo'])); | ||
it('should add multiple plugin paths', () => testPath(['foo', 'bar'])); |
Oops, something went wrong.