-
-
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.
jest-snapshot: Distinguish empty string from external snapshot not wr…
…itten (#8880) * jest-snapshot: Distinguish empty string from external snapshot not written * Update CHANGELOG.md * Replace null with undefined * Add e2e test toMatchSnapshotWithStringSerializer
- Loading branch information
1 parent
45c57c5
commit 4482e71
Showing
6 changed files
with
92 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* 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 * as path from 'path'; | ||
import {cleanup, makeTemplate, writeFiles} from '../Utils'; | ||
import runJest from '../runJest'; | ||
|
||
const DIR = path.resolve( | ||
__dirname, | ||
'../to-match-snapshot-with-string-serializer', | ||
); | ||
const TESTS_DIR = path.resolve(DIR, '__tests__'); | ||
|
||
beforeEach(() => cleanup(TESTS_DIR)); | ||
afterAll(() => cleanup(TESTS_DIR)); | ||
|
||
test('empty external', () => { | ||
// Make sure empty string as expected value of external snapshot | ||
// is not confused with new snapshot not written because of --ci option. | ||
const filename = 'empty-external.test.js'; | ||
const template = makeTemplate( | ||
`test('string serializer', () => { expect($1).toMatchSnapshot(); })`, | ||
); | ||
|
||
{ | ||
writeFiles(TESTS_DIR, { | ||
[filename]: template(['""']), // empty string | ||
}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('1 snapshot written from 1 test suite.'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('Snapshots: 1 passed, 1 total'); | ||
expect(stderr).not.toMatch('1 snapshot written from 1 test suite.'); | ||
expect(status).toBe(0); | ||
} | ||
|
||
{ | ||
writeFiles(TESTS_DIR, { | ||
[filename]: template(['"non-empty"']), | ||
}); | ||
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); | ||
expect(stderr).toMatch('Snapshots: 1 failed, 1 total'); | ||
expect(stderr).not.toMatch('not written'); // not confused with --ci option | ||
expect(stderr).toMatch(/- Snapshot|Snapshot:/); // ordinary report | ||
expect(status).toBe(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,8 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node", | ||
"snapshotSerializers": [ | ||
"./serializers/string" | ||
] | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
e2e/to-match-snapshot-with-string-serializer/serializers/string.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,14 @@ | ||
/** | ||
* 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'; | ||
|
||
// Serialize string (especially empty) without enclosing punctuation. | ||
module.exports = { | ||
print: val => val, | ||
test: val => typeof val === '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
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