-
-
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
361 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,47 @@ | ||
/** | ||
* 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 skipOnWindows = require('skipOnWindows'); | ||
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', () => { | ||
skipOnWindows.suite(); | ||
|
||
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(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
/** | ||
* 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'; | ||
|
||
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,12 @@ | ||
/** | ||
* 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 createPlugin = require('../utils').createPlugin; | ||
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,12 @@ | ||
/** | ||
* 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 createPlugin = require('../utils').createPlugin; | ||
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,16 @@ | ||
/** | ||
* 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'; | ||
|
||
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
Oops, something went wrong.