Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-config): set default snapshotFormat #13036

Merged
merged 3 commits into from Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036))

### Fixes

### Chore & Maintenance
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Expand Up @@ -1614,7 +1614,7 @@ The number of seconds after which a test is considered as slow and reported as s

### `snapshotFormat` \[object]

Default: `undefined`
Default: `{escapeString: false, printBasicPrototype: false}`

Allows overriding specific snapshot formatting options documented in the [pretty-format readme](https://www.npmjs.com/package/pretty-format#usage-with-options), with the exceptions of `compareKeys` and `plugins`. For example, this config would have the snapshot formatter not print a prefix for "Object" and "Array":

Expand Down
8 changes: 8 additions & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -58,6 +58,10 @@ exports[`--showConfig outputs config info and exits 1`] = `
"setupFilesAfterEnv": [],
"skipFilter": false,
"slowTestThreshold": 5,
"snapshotFormat": {
"escapeString": false,
"printBasicPrototype": false
},
"snapshotSerializers": [],
"testEnvironment": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-environment-node/build/index.js",
"testEnvironmentOptions": {},
Expand Down Expand Up @@ -122,6 +126,10 @@ exports[`--showConfig outputs config info and exits 1`] = `
"rootDir": "<<REPLACED_ROOT_DIR>>",
"runTestsByPath": false,
"skipFilter": false,
"snapshotFormat": {
"escapeString": false,
"printBasicPrototype": false
},
"testFailureExitCode": 1,
"testPathPattern": "",
"testSequencer": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-test-sequencer/build/index.js",
Expand Down
14 changes: 7 additions & 7 deletions e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap
Expand Up @@ -3,8 +3,8 @@
exports[`Snapshot serializers renders snapshot 1`] = `
Object {
"snapshot serializers works with array of strings in property matcher 1": "
Object {
"arrayOfStrings": Array [
{
"arrayOfStrings": [
"stream",
],
}
Expand All @@ -15,8 +15,8 @@ Object {
/>
",
"snapshot serializers works with expect.XXX within array in property matcher 1": "
Object {
"arrayOfStrings": Array [
{
"arrayOfStrings": [
Any<String>,
],
}
Expand All @@ -26,7 +26,7 @@ Object {
"snapshot serializers works with prepended plugins and default serializers 1": "
<div
aProp={
Object {
{
"a": 6,
}
}
Expand All @@ -36,7 +36,7 @@ Object {
"snapshot serializers works with prepended plugins from expect method called once 1": "
<div
aProp={
Object {
{
"a": 6,
}
}
Expand All @@ -46,7 +46,7 @@ Object {
"snapshot serializers works with prepended plugins from expect method called twice 1": "
<div
aProp={
Object {
{
"a": 6,
}
}
Expand Down
38 changes: 19 additions & 19 deletions e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap
Expand Up @@ -3,7 +3,7 @@
exports[`basic support: initial write 1`] = `
"test('inline snapshots', () =>
expect({apple: 'original value'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "original value",
}
\`));
Expand All @@ -13,7 +13,7 @@ exports[`basic support: initial write 1`] = `
exports[`basic support: snapshot mismatch 1`] = `
"test('inline snapshots', () =>
expect({apple: 'updated value'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "original value",
}
\`));"
Expand All @@ -22,7 +22,7 @@ expect({apple: 'updated value'}).toMatchInlineSnapshot(\`
exports[`basic support: snapshot passed 1`] = `
"test('inline snapshots', () =>
expect({apple: 'original value'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "original value",
}
\`));
Expand All @@ -32,7 +32,7 @@ exports[`basic support: snapshot passed 1`] = `
exports[`basic support: snapshot updated 1`] = `
"test('inline snapshots', () =>
expect({apple: 'updated value'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "updated value",
}
\`));
Expand Down Expand Up @@ -68,7 +68,7 @@ exports[`handles property matchers: initial write 1`] = `
expect({createdAt: new Date()}).toMatchInlineSnapshot(
{createdAt: expect.any(Date)},
\`
Object {
{
"createdAt": Any<Date>,
}
\`,
Expand All @@ -82,7 +82,7 @@ exports[`handles property matchers: snapshot failed 1`] = `
expect({createdAt: "string"}).toMatchInlineSnapshot(
{createdAt: expect.any(Date)},
\`
Object {
{
"createdAt": Any<Date>,
}
\`,
Expand All @@ -95,7 +95,7 @@ exports[`handles property matchers: snapshot passed 1`] = `
expect({createdAt: new Date()}).toMatchInlineSnapshot(
{createdAt: expect.any(Date)},
\`
Object {
{
"createdAt": Any<Date>,
}
\`,
Expand All @@ -109,7 +109,7 @@ exports[`handles property matchers: snapshot updated 1`] = `
expect({createdAt: 'string'}).toMatchInlineSnapshot(
{createdAt: expect.any(String)},
\`
Object {
{
"createdAt": Any<String>,
}
\`,
Expand All @@ -122,7 +122,7 @@ exports[`indentation is correct in the presences of existing snapshots, when the
"it('is true', () => {
expect(true).toMatchInlineSnapshot(\`true\`);
expect([1, 2, 3]).toMatchInlineSnapshot(\`
Array [
[
1,
2,
3,
Expand All @@ -135,12 +135,12 @@ exports[`indentation is correct in the presences of existing snapshots, when the
exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = `
"test('existing snapshot', () => {
expect({hello: 'world'}).toMatchInlineSnapshot(\`
Object {
{
"hello": "world",
}
\`);
expect({hello: 'world'}).toMatchInlineSnapshot(\`
Object {
{
"hello": "world",
}
\`);
Expand All @@ -160,22 +160,22 @@ expect.extend({
});
test('inline snapshots', () => {
expect({apple: 'value 1'}).toMatchCustomInlineSnapshot(\`
Object {
{
"apple": "value 1",
}
\`);
expect({apple: 'value 2'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "value 2",
}
\`);
expect({apple: 'value 3'}).toMatchCustomInlineSnapshot2(\`
Object {
{
"apple": "value 3",
}
\`);
expect({apple: 'value 4'}).toMatchInlineSnapshot(\`
Object {
{
"apple": "value 4",
}
\`);
Expand Down Expand Up @@ -251,15 +251,15 @@ test('inline snapshots', () => {
id: expect.any(Number),
},
\`
Object {
{
"createdAt": Any<Date>,
"id": Any<Number>,
"name": "LeBron James",
}
\`,
);
expect(user).toMatchUserInlineSnapshot(\`
Object {
{
"createdAt": Any<Date>,
"id": Any<Number>,
"name": "LeBron James",
Expand All @@ -278,7 +278,7 @@ expect.extend({
});
test('inline snapshots', () => {
expect({apple: 'original value'}).toMatchCustomInlineSnapshot(\`
Object {
{
"apple": "original value",
}
\`);
Expand All @@ -289,7 +289,7 @@ test('inline snapshots', () => {
exports[`writes snapshots with non-literals in expect(...) 1`] = `
"it('works with inline snapshots', () => {
expect({a: 1}).toMatchInlineSnapshot(\`
Object {
{
"a": 1,
}
\`);
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/failureDetailsProperty.test.ts
Expand Up @@ -215,7 +215,7 @@ test('that the failureDetails property is set', () => {
Array [
Object {
"matcherResult": Object {
"actual": "Object {
"actual": "{
"p1": "hello",
"p2": "world",
}",
Expand All @@ -230,7 +230,7 @@ test('that the failureDetails property is set', () => {
- Snapshot - 1
+ Received + 1

Object {
{
"p1": "hello",
- "p2": "sunshine",
+ "p2": "world",
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/snapshotMockFs.test.ts
Expand Up @@ -29,7 +29,7 @@ test('store snapshot even if fs is mocked', () => {

const content = require(snapshotFile);
expect(content['snapshot 1']).toBe(`
Object {
{
"foo": "bar",
}
`);
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/toMatchInlineSnapshot.test.ts
Expand Up @@ -364,7 +364,7 @@ test('indentation is correct in the presences of existing snapshots', () => {
const test = `
test('existing snapshot', () => {
expect({ hello: 'world' }).toMatchInlineSnapshot(\`
Object {
{
"hello": "world",
}
\`);
Expand Down
12 changes: 6 additions & 6 deletions examples/react-native/__tests__/__snapshots__/intro.test.js.snap
Expand Up @@ -3,7 +3,7 @@
exports[`renders correctly 1`] = `
<View
style={
Object {
{
"alignItems": "center",
"backgroundColor": "#F5FCFF",
"flex": 1,
Expand All @@ -13,7 +13,7 @@ exports[`renders correctly 1`] = `
>
<Text
style={
Object {
{
"fontSize": 20,
"margin": 10,
"textAlign": "center",
Expand All @@ -24,7 +24,7 @@ exports[`renders correctly 1`] = `
</Text>
<Text
style={
Object {
{
"color": "#333333",
"marginBottom": 5,
"textAlign": "center",
Expand All @@ -46,7 +46,7 @@ exports[`renders the ActivityIndicator component 1`] = `
exports[`renders the FlatList component 1`] = `
<RCTScrollView
data={
Array [
[
"apple",
"banana",
"kiwi",
Expand All @@ -65,8 +65,8 @@ exports[`renders the FlatList component 1`] = `
removeClippedSubviews={false}
renderItem={[Function]}
scrollEventThrottle={50}
stickyHeaderIndices={Array []}
viewabilityConfigCallbackPairs={Array []}
stickyHeaderIndices={[]}
viewabilityConfigCallbackPairs={[]}
>
<View>
<View
Expand Down
2 changes: 1 addition & 1 deletion jest.config.mjs
Expand Up @@ -33,7 +33,7 @@ export default {
],
projects: ['<rootDir>', '<rootDir>/examples/*/'],
snapshotFormat: {
escapeString: false,
printBasicPrototype: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer this, so 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

},
snapshotSerializers: [require.resolve('jest-serializer-ansi-escapes')],
testPathIgnorePatterns: [
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Expand Up @@ -72,6 +72,7 @@ const defaultOptions: Config.DefaultOptions = {
setupFilesAfterEnv: [],
skipFilter: false,
slowTestThreshold: 5,
snapshotFormat: {escapeString: false, printBasicPrototype: false},
snapshotSerializers: [],
testEnvironment: 'jest-environment-node',
testEnvironmentOptions: {},
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-config/src/normalize.ts
Expand Up @@ -909,6 +909,11 @@ export default async function normalize(
value = oldOptions[key];
break;
}
case 'snapshotFormat': {
value = {...DEFAULT_CONFIG.snapshotFormat, ...oldOptions[key]};

break;
}
case 'automock':
case 'cache':
case 'changedSince':
Expand Down Expand Up @@ -952,7 +957,6 @@ export default async function normalize(
case 'skipFilter':
case 'skipNodeResolution':
case 'slowTestThreshold':
case 'snapshotFormat':
case 'testEnvironment':
case 'testEnvironmentOptions':
case 'testFailureExitCode':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -190,6 +190,7 @@ export type DefaultOptions = {
setupFilesAfterEnv: Array<string>;
skipFilter: boolean;
slowTestThreshold: number;
snapshotFormat: SnapshotFormat;
snapshotSerializers: Array<string>;
testEnvironment: string;
testEnvironmentOptions: Record<string, unknown>;
Expand Down