From 8e8e2da4a35587b252a80efd7cb7ce17cac1937d Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 16 Sep 2019 14:14:56 -0400 Subject: [PATCH 1/8] chore: Align snapshots in jest-matcher-utils --- .../__snapshots__/index.test.ts.snap | 102 +++++++++--------- .../printDiffOrStringify.test.ts.snap | 48 ++++----- .../src/__tests__/index.test.ts | 44 ++++++-- .../__tests__/printDiffOrStringify.test.ts | 36 +++++++ 4 files changed, 147 insertions(+), 83 deletions(-) diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap index 63845c6c1b4a..c930a2254670 100644 --- a/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/jest-matcher-utils/src/__tests__/__snapshots__/index.test.ts.snap @@ -1,106 +1,102 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`.ensureNoExpected() throws error when expected is not undefined with matcherName 1`] = ` -"expect(received)[.not].toBeDefined() +exports[`ensureNoExpected() throws error when expected is not undefined with matcherName 1`] = ` +expect(received)[.not].toBeDefined() -Matcher error: this matcher must not have an expected argument +Matcher error: this matcher must not have an expected argument Expected has type: object -Expected has value: {\\"a\\": 1}" +Expected has value: {"a": 1} `; -exports[`.ensureNoExpected() throws error when expected is not undefined with matcherName and options 1`] = ` -"expect(received).not.toBeDefined() +exports[`ensureNoExpected() throws error when expected is not undefined with matcherName and options 1`] = ` +expect(received).not.toBeDefined() -Matcher error: this matcher must not have an expected argument +Matcher error: this matcher must not have an expected argument Expected has type: object -Expected has value: {\\"a\\": 1}" +Expected has value: {"a": 1} `; -exports[`.ensureNumbers() throws error when expected is not a number (backward compatibility) 1`] = ` -"expect(received)[.not].toBeCloseTo(expected) +exports[`ensureNumbers() throws error when expected is not a number (backward compatibility) 1`] = ` +expect(received)[.not].toBeCloseTo(expected) -Matcher error: expected value must be a number or bigint +Matcher error: expected value must be a number or bigint Expected has type: string -Expected has value: \\"not_a_number\\"" +Expected has value: "not_a_number" `; -exports[`.ensureNumbers() throws error when received is not a number (backward compatibility) 1`] = ` -"expect(received)[.not].toBeCloseTo(expected) +exports[`ensureNumbers() throws error when received is not a number (backward compatibility) 1`] = ` +expect(received)[.not].toBeCloseTo(expected) -Matcher error: received value must be a number or bigint +Matcher error: received value must be a number or bigint Received has type: string -Received has value: \\"not_a_number\\"" +Received has value: "not_a_number" `; -exports[`.ensureNumbers() with options promise empty isNot false received 1`] = ` -"expect(received).toBeCloseTo(expected, precision) +exports[`ensureNumbers() with options promise empty isNot false received 1`] = ` +expect(received).toBeCloseTo(expected, precision) -Matcher error: received value must be a number or bigint +Matcher error: received value must be a number or bigint Received has type: string -Received has value: \\"\\"" +Received has value: "" `; -exports[`.ensureNumbers() with options promise empty isNot true expected 1`] = ` -"expect(received).not.toBeCloseTo(expected) +exports[`ensureNumbers() with options promise empty isNot true expected 1`] = ` +expect(received).not.toBeCloseTo(expected) -Matcher error: expected value must be a number or bigint +Matcher error: expected value must be a number or bigint -Expected has value: undefined" +Expected has value: undefined `; -exports[`.ensureNumbers() with options promise rejects isNot false expected 1`] = ` -"expect(received).rejects.toBeCloseTo(expected) +exports[`ensureNumbers() with options promise rejects isNot false expected 1`] = ` +expect(received).rejects.toBeCloseTo(expected) -Matcher error: expected value must be a number or bigint +Matcher error: expected value must be a number or bigint Expected has type: string -Expected has value: \\"0\\"" +Expected has value: "0" `; -exports[`.ensureNumbers() with options promise rejects isNot true received 1`] = ` -"expect(received).rejects.not.toBeCloseTo(expected) +exports[`ensureNumbers() with options promise rejects isNot true received 1`] = ` +expect(received).rejects.not.toBeCloseTo(expected) -Matcher error: received value must be a number or bigint +Matcher error: received value must be a number or bigint Received has type: symbol -Received has value: Symbol(0.1)" +Received has value: Symbol(0.1) `; -exports[`.ensureNumbers() with options promise resolves isNot false received 1`] = ` -"expect(received).resolves.toBeCloseTo(expected) +exports[`ensureNumbers() with options promise resolves isNot false received 1`] = ` +expect(received).resolves.toBeCloseTo(expected) -Matcher error: received value must be a number or bigint +Matcher error: received value must be a number or bigint Received has type: boolean -Received has value: false" +Received has value: false `; -exports[`.ensureNumbers() with options promise resolves isNot true expected 1`] = ` -"expect(received).resolves.not.toBeCloseTo(expected) +exports[`ensureNumbers() with options promise resolves isNot true expected 1`] = ` +expect(received).resolves.not.toBeCloseTo(expected) -Matcher error: expected value must be a number or bigint +Matcher error: expected value must be a number or bigint -Expected has value: null" +Expected has value: null `; -exports[`.stringify() reduces maxDepth if stringifying very large objects 1`] = `"{\\"a\\": 1, \\"b\\": [Object]}"`; +exports[`stringify() toJSON errors when comparing two objects 1`] = ` +expect(received).toEqual(expected) // deep equality -exports[`.stringify() reduces maxDepth if stringifying very large objects 2`] = `"{\\"a\\": 1, \\"b\\": {\\"0\\": \\"test\\", \\"1\\": \\"test\\", \\"2\\": \\"test\\", \\"3\\": \\"test\\", \\"4\\": \\"test\\", \\"5\\": \\"test\\", \\"6\\": \\"test\\", \\"7\\": \\"test\\", \\"8\\": \\"test\\", \\"9\\": \\"test\\"}}"`; +- Expected ++ Received -exports[`.stringify() toJSON errors when comparing two objects 1`] = ` -"expect(received).toEqual(expected) // deep equality - -- Expected -+ Received - - Object { -- \\"b\\": 1, -+ \\"a\\": 1, - \\"toJSON\\": [Function toJSON], - }" + Object { +- "b": 1, ++ "a": 1, + "toJSON": [Function toJSON], + } `; diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap index 753d46e62662..a7b07ed744fe 100644 --- a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap +++ b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap @@ -1,44 +1,44 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`printDiffOrStringify expected and received are multi line with trailing spaces 1`] = ` -"- Expected -+ Received - -- delete -+ insert -- common expected common -+ common received common -- prev -+ next " +- Expected ++ Received + +- delete ++ insert +- common expected common ++ common received common +- prev ++ next `; exports[`printDiffOrStringify expected and received are single line with multiple changes 1`] = ` -"Expected: \\"delete common expected common prev\\" -Received: \\"insert common received common next\\"" +Expected: "delete common expected common prev" +Received: "insert common received common next" `; exports[`printDiffOrStringify expected is empty and received is single line 1`] = ` -"Expected: \\"\\" -Received: \\"single line\\"" +Expected: "" +Received: "single line" `; exports[`printDiffOrStringify expected is multi line and received is empty 1`] = ` -"Expected: \\"multi -line\\" -Received: \\"\\"" +Expected: "multi +line" +Received: "" `; exports[`printDiffOrStringify has no common after clean up chaff multiline 1`] = ` -"- Expected -+ Received +- Expected ++ Received -- delete -- two -+ insert -+ 2" +- delete +- two ++ insert ++ 2 `; exports[`printDiffOrStringify has no common after clean up chaff one-line 1`] = ` -"Expected: \\"delete\\" -Received: \\"insert\\"" +Expected: "delete" +Received: "insert" `; diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index 8ab83984bdfb..2a47ee80a1d3 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -6,7 +6,10 @@ * */ +import ansiRegex = require('ansi-regex'); +import * as style from 'ansi-styles'; import chalk from 'chalk'; +import prettyFormat = require('pretty-format'); import { MatcherHintOptions, diff, @@ -21,7 +24,36 @@ import { /* global BigInt */ const isBigIntDefined = typeof BigInt === 'function'; -describe('.stringify()', () => { +expect.addSnapshotSerializer({ + serialize(val: string): string { + return val.replace(ansiRegex(), match => { + switch (match) { + case style.bold.open: + return ''; + case style.dim.open: + return ''; + case style.green.open: + return ''; + case style.red.open: + return ''; + + case style.bold.close: + case style.dim.close: + case style.green.close: + case style.red.close: + return ''; + + default: + return match; + } + }); + }, + test(val: any): val is string { + return typeof val === 'string'; + }, +}); + +describe('stringify()', () => { [ [[], '[]'], [{}, '{}'], @@ -93,12 +125,12 @@ describe('.stringify()', () => { small.b[i] = 'test'; } - expect(stringify(big)).toMatchSnapshot(); - expect(stringify(small)).toMatchSnapshot(); + expect(stringify(big)).toBe(prettyFormat(big, {maxDepth: 1, min: true})); + expect(stringify(small)).toBe(prettyFormat(small, {min: true})); }); }); -describe('.ensureNumbers()', () => { +describe('ensureNumbers()', () => { test('dont throw error when variables are numbers', () => { expect(() => { // @ts-ignore @@ -198,7 +230,7 @@ describe('.ensureNumbers()', () => { }); }); -describe('.ensureNoExpected()', () => { +describe('ensureNoExpected()', () => { test('dont throw error when undefined', () => { expect(() => { // @ts-ignore @@ -251,7 +283,7 @@ describe('diff', () => { } }); -describe('.pluralize()', () => { +describe('pluralize()', () => { test('one', () => expect(pluralize('apple', 1)).toEqual('one apple')); test('two', () => expect(pluralize('apple', 2)).toEqual('two apples')); test('20', () => expect(pluralize('apple', 20)).toEqual('20 apples')); diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index a8e985dd5329..18b934a6f768 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,8 +5,44 @@ * LICENSE file in the root directory of this source tree. */ +import ansiRegex = require('ansi-regex'); +import * as style from 'ansi-styles'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; +expect.addSnapshotSerializer({ + serialize(val: string): string { + return val.replace(ansiRegex(), match => { + switch (match) { + case style.inverse.open: + return ''; + case style.inverse.close: + return ''; + + case style.dim.open: + return ''; + case style.green.open: + return ''; + case style.red.open: + return ''; + case style.bgYellow.open: + return ''; + + case style.dim.close: + case style.green.close: + case style.red.close: + case style.bgYellow.close: + return ''; + + default: + return match; + } + }); + }, + test(val: any): val is string { + return typeof val === 'string'; + }, +}); + describe('printDiffOrStringify', () => { const testDiffOrStringify = (expected: string, received: string): string => printDiffOrStringify(expected, received, 'Expected', 'Received', true); From 5a00b5a8840988cfa4144bae20f627a4ab4c6283 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 21 Sep 2019 16:04:04 -0400 Subject: [PATCH 2/8] Use new ConvertAnsi1 plugin in jest-diff and jest-matcher-utils --- packages/jest-diff/src/__tests__/diff.test.ts | 43 +------------------ .../src/__tests__/index.test.ts | 32 +------------- .../__tests__/printDiffOrStringify.test.ts | 37 +--------------- 3 files changed, 6 insertions(+), 106 deletions(-) diff --git a/packages/jest-diff/src/__tests__/diff.test.ts b/packages/jest-diff/src/__tests__/diff.test.ts index d75390fc9dc3..9c6f408a57c2 100644 --- a/packages/jest-diff/src/__tests__/diff.test.ts +++ b/packages/jest-diff/src/__tests__/diff.test.ts @@ -5,10 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import ansiRegex = require('ansi-regex'); -import * as style from 'ansi-styles'; import chalk from 'chalk'; import stripAnsi from 'strip-ansi'; +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; import diff from '../'; import {diffStringsUnified} from '../printDiffs'; @@ -46,45 +45,7 @@ const expanded = {expand: true}; const elementSymbol = Symbol.for('react.element'); -expect.addSnapshotSerializer({ - serialize(val: string): string { - return val.replace(ansiRegex(), match => { - switch (match) { - case style.inverse.open: - return ''; - case style.inverse.close: - return ''; - - case style.bold.open: - return ''; - case style.dim.open: - return ''; - case style.green.open: - return ''; - case style.red.open: - return ''; - case style.yellow.open: - return ''; - case style.bgYellow.open: - return ''; - - case style.bold.close: - case style.dim.close: - case style.green.close: - case style.red.close: - case style.yellow.close: - case style.bgYellow.close: - return ''; - - default: - return match; - } - }); - }, - test(val: any): val is string { - return typeof val === 'string'; - }, -}); +expect.addSnapshotSerializer(convertAnsi1); describe('different types', () => { [ diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index 2a47ee80a1d3..64592f657805 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -6,10 +6,9 @@ * */ -import ansiRegex = require('ansi-regex'); -import * as style from 'ansi-styles'; import chalk from 'chalk'; import prettyFormat = require('pretty-format'); +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; import { MatcherHintOptions, diff, @@ -24,34 +23,7 @@ import { /* global BigInt */ const isBigIntDefined = typeof BigInt === 'function'; -expect.addSnapshotSerializer({ - serialize(val: string): string { - return val.replace(ansiRegex(), match => { - switch (match) { - case style.bold.open: - return ''; - case style.dim.open: - return ''; - case style.green.open: - return ''; - case style.red.open: - return ''; - - case style.bold.close: - case style.dim.close: - case style.green.close: - case style.red.close: - return ''; - - default: - return match; - } - }); - }, - test(val: any): val is string { - return typeof val === 'string'; - }, -}); +expect.addSnapshotSerializer(convertAnsi1); describe('stringify()', () => { [ diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 18b934a6f768..411fc16764cc 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,43 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import ansiRegex = require('ansi-regex'); -import * as style from 'ansi-styles'; +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; -expect.addSnapshotSerializer({ - serialize(val: string): string { - return val.replace(ansiRegex(), match => { - switch (match) { - case style.inverse.open: - return ''; - case style.inverse.close: - return ''; - - case style.dim.open: - return ''; - case style.green.open: - return ''; - case style.red.open: - return ''; - case style.bgYellow.open: - return ''; - - case style.dim.close: - case style.green.close: - case style.red.close: - case style.bgYellow.close: - return ''; - - default: - return match; - } - }); - }, - test(val: any): val is string { - return typeof val === 'string'; - }, -}); +expect.addSnapshotSerializer(convertAnsi1); describe('printDiffOrStringify', () => { const testDiffOrStringify = (expected: string, received: string): string => From adf65bfb1c58b28f730fa2905a9b2c324c7515ba Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 21 Sep 2019 16:05:50 -0400 Subject: [PATCH 3/8] Add ConvertAnsi1.ts under plugins in pretty-format --- .../pretty-format/src/plugins/ConvertAnsi1.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 packages/pretty-format/src/plugins/ConvertAnsi1.ts diff --git a/packages/pretty-format/src/plugins/ConvertAnsi1.ts b/packages/pretty-format/src/plugins/ConvertAnsi1.ts new file mode 100644 index 000000000000..69698eec667b --- /dev/null +++ b/packages/pretty-format/src/plugins/ConvertAnsi1.ts @@ -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 ansiRegex = require('ansi-regex'); +import * as style from 'ansi-styles'; +import {NewPlugin} from '../types'; + +// Not published, for test suites of certain packages in Jest monorepo: +// serialize aligned snapshots of diffs and reports when matcher fails. +const plugin: NewPlugin = { + serialize(val: string): string { + // Return the string itself, not escaped nor enclosed in double quote marks. + return val.replace(ansiRegex(), match => { + switch (match) { + case style.inverse.open: + return ''; + case style.inverse.close: + return ''; + + case style.bold.open: + return ''; + case style.dim.open: + return ''; + case style.green.open: + return ''; + case style.red.open: + return ''; + case style.yellow.open: + return ''; + case style.bgYellow.open: + return ''; + + case style.bold.close: + case style.dim.close: + case style.green.close: + case style.red.close: + case style.yellow.close: + case style.bgYellow.close: + return ''; + + default: + return match; // unexpected escape sequence + } + }); + }, + test(val: any): val is string { + return typeof val === 'string'; + }, +}; + +export default plugin; From 9ee4cfaaa37850be70cd915eeda9c5283fc05bb7 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 21 Sep 2019 16:36:19 -0400 Subject: [PATCH 4/8] Delete unneeded .ts file name extension in import statements --- packages/jest-diff/src/__tests__/diff.test.ts | 2 +- packages/jest-matcher-utils/src/__tests__/index.test.ts | 2 +- .../src/__tests__/printDiffOrStringify.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-diff/src/__tests__/diff.test.ts b/packages/jest-diff/src/__tests__/diff.test.ts index 9c6f408a57c2..374b1870082a 100644 --- a/packages/jest-diff/src/__tests__/diff.test.ts +++ b/packages/jest-diff/src/__tests__/diff.test.ts @@ -7,7 +7,7 @@ import chalk from 'chalk'; import stripAnsi from 'strip-ansi'; -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; import diff from '../'; import {diffStringsUnified} from '../printDiffs'; diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index 64592f657805..83b4ba0607ff 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -8,7 +8,7 @@ import chalk from 'chalk'; import prettyFormat = require('pretty-format'); -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; import { MatcherHintOptions, diff, diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 411fc16764cc..94a829cdd494 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1.ts'; +import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; expect.addSnapshotSerializer(convertAnsi1); From 1aa689436591004ecab2f656d0de4bd33763e243 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 23 Sep 2019 12:47:17 -0400 Subject: [PATCH 5/8] Add alignedAnsiStyleSerializer to test-utils --- packages/jest-diff/src/__tests__/diff.test.ts | 4 +- .../src/__tests__/index.test.ts | 4 +- .../__tests__/printDiffOrStringify.test.ts | 4 +- packages/test-utils/README.md | 35 +++++++------ packages/test-utils/package.json | 10 +++- .../src/alignedAnsiStyleSerializer.ts | 50 +++++++++++++++++++ packages/test-utils/src/index.ts | 2 + yarn.lock | 2 +- 8 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 packages/test-utils/src/alignedAnsiStyleSerializer.ts diff --git a/packages/jest-diff/src/__tests__/diff.test.ts b/packages/jest-diff/src/__tests__/diff.test.ts index 374b1870082a..75b975ecfed0 100644 --- a/packages/jest-diff/src/__tests__/diff.test.ts +++ b/packages/jest-diff/src/__tests__/diff.test.ts @@ -7,7 +7,7 @@ import chalk from 'chalk'; import stripAnsi from 'strip-ansi'; -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import diff from '../'; import {diffStringsUnified} from '../printDiffs'; @@ -45,7 +45,7 @@ const expanded = {expand: true}; const elementSymbol = Symbol.for('react.element'); -expect.addSnapshotSerializer(convertAnsi1); +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); describe('different types', () => { [ diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index 83b4ba0607ff..681472a20080 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -8,7 +8,7 @@ import chalk from 'chalk'; import prettyFormat = require('pretty-format'); -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import { MatcherHintOptions, diff, @@ -23,7 +23,7 @@ import { /* global BigInt */ const isBigIntDefined = typeof BigInt === 'function'; -expect.addSnapshotSerializer(convertAnsi1); +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); describe('stringify()', () => { [ diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 94a829cdd494..23510f056752 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,10 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import convertAnsi1 from '../../../pretty-format/src/plugins/ConvertAnsi1'; + import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; -expect.addSnapshotSerializer(convertAnsi1); +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); describe('printDiffOrStringify', () => { const testDiffOrStringify = (expected: string, received: string): string => diff --git a/packages/test-utils/README.md b/packages/test-utils/README.md index f9003764d794..738b2da08c43 100644 --- a/packages/test-utils/README.md +++ b/packages/test-utils/README.md @@ -1,25 +1,30 @@ -# babel-jest +# test-utils -[Babel](https://github.com/babel/babel) [jest](https://github.com/facebook/jest) plugin +Private package exports utilities for multiple end-to-end tests or packages. -## Usage +## alignedAnsiStyleSerializer -If you are already using `jest-cli`, just add `babel-jest` and it will automatically compile JavaScript code using Babel. +Display colors concisely and clearly so we can review changes quickly and confidently: -```bash -yarn add --dev babel-jest @babel/core -``` +- reports when matchers fail +- annotation and comparison lines from differences -If you would like to write your own preprocessor, uninstall and delete babel-jest and set the [config.transform](https://jestjs.io/docs/configuration#transform-object-string-string) option to your preprocessor. +Raw snapshot avoids distracting `\\` or `\"` escape sequences from default string serialization. -## Setup +Tag names have consistent length to align columns like `Expected` and `Received` or comparison lines: -_Note: this step is only required if you are using `babel-jest` with additional code preprocessors._ +| | style | +| --: | :--------- | +| `b` | `bold` | +| `d` | `dim` | +| `g` | `green` | +| `i` | `inverse` | +| `r` | `red` | +| `y` | `yellow` | +| `Y` | `bgYellow` | -To explicitly define `babel-jest` as a transformer for your JavaScript code, map _.js_ files to the `babel-jest` module. +```js +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; -```json -"transform": { - "^.+\\.jsx?$": "babel-jest" -}, +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); ``` diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 5e0ac3ca25f3..10a6a68664a5 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -4,5 +4,13 @@ "private": true, "license": "MIT", "main": "build/index.js", - "types": "build/index.d.ts" + "types": "build/index.d.ts", + "dependencies": { + "ansi-regex": "^4.1.0", + "ansi-styles": "^4.1.0" + }, + "devDependencies": { + "@types/ansi-regex": "^4.0.0", + "@types/ansi-styles": "^3.2.1" + } } diff --git a/packages/test-utils/src/alignedAnsiStyleSerializer.ts b/packages/test-utils/src/alignedAnsiStyleSerializer.ts new file mode 100644 index 000000000000..99ea8900d842 --- /dev/null +++ b/packages/test-utils/src/alignedAnsiStyleSerializer.ts @@ -0,0 +1,50 @@ +/** + * 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 ansiRegex = require('ansi-regex'); +import * as style from 'ansi-styles'; + +export const alignedAnsiStyleSerializer = { + serialize(val: string): string { + // Return the string itself, not escaped nor enclosed in double quote marks. + return val.replace(ansiRegex(), match => { + switch (match) { + case style.inverse.open: + return ''; + case style.inverse.close: + return ''; + + case style.bold.open: + return ''; + case style.dim.open: + return ''; + case style.green.open: + return ''; + case style.red.open: + return ''; + case style.yellow.open: + return ''; + case style.bgYellow.open: + return ''; + + case style.bold.close: + case style.dim.close: + case style.green.close: + case style.red.close: + case style.yellow.close: + case style.bgYellow.close: + return ''; + + default: + return match; // unexpected escape sequence + } + }); + }, + test(val: any): val is string { + return typeof val === 'string'; + }, +}; diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 0eb78d2fc053..971dc5bede6f 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +export {alignedAnsiStyleSerializer} from './alignedAnsiStyleSerializer'; + export { isJestCircusRun, skipSuiteOnJasmine, diff --git a/yarn.lock b/yarn.lock index 088ebbe5df8e..efb2690bd15d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2617,7 +2617,7 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1: dependencies: color-convert "^1.9.0" -ansi-styles@^4.0.0: +ansi-styles@^4.0.0, ansi-styles@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.1.0.tgz#d3ba8047b818293eaaa7978321dd61bff9842cfc" integrity sha512-Qts4KCLKG+waHc9C4m07weIY8qyeixoS0h6RnbsNVD6Fw+pEZGW3vTyObL3WXpE09Mq4Oi7/lBEyLmOiLtlYWQ== From 34cba99029973c36b5f327faff5437fccc0876e0 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 23 Sep 2019 12:49:11 -0400 Subject: [PATCH 6/8] Delete ConvertAnsi1 from pretty-format --- .../pretty-format/src/plugins/ConvertAnsi1.ts | 55 ------------------- 1 file changed, 55 deletions(-) delete mode 100644 packages/pretty-format/src/plugins/ConvertAnsi1.ts diff --git a/packages/pretty-format/src/plugins/ConvertAnsi1.ts b/packages/pretty-format/src/plugins/ConvertAnsi1.ts deleted file mode 100644 index 69698eec667b..000000000000 --- a/packages/pretty-format/src/plugins/ConvertAnsi1.ts +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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 ansiRegex = require('ansi-regex'); -import * as style from 'ansi-styles'; -import {NewPlugin} from '../types'; - -// Not published, for test suites of certain packages in Jest monorepo: -// serialize aligned snapshots of diffs and reports when matcher fails. -const plugin: NewPlugin = { - serialize(val: string): string { - // Return the string itself, not escaped nor enclosed in double quote marks. - return val.replace(ansiRegex(), match => { - switch (match) { - case style.inverse.open: - return ''; - case style.inverse.close: - return ''; - - case style.bold.open: - return ''; - case style.dim.open: - return ''; - case style.green.open: - return ''; - case style.red.open: - return ''; - case style.yellow.open: - return ''; - case style.bgYellow.open: - return ''; - - case style.bold.close: - case style.dim.close: - case style.green.close: - case style.red.close: - case style.yellow.close: - case style.bgYellow.close: - return ''; - - default: - return match; // unexpected escape sequence - } - }); - }, - test(val: any): val is string { - return typeof val === 'string'; - }, -}; - -export default plugin; From 3e85317f275a08ab1bd33d5bd462ab692e529dae Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 23 Sep 2019 12:50:25 -0400 Subject: [PATCH 7/8] Fix prettier lint error --- .../src/__tests__/printDiffOrStringify.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 23510f056752..adaf7d7608ea 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ - import {alignedAnsiStyleSerializer} from '@jest/test-utils'; +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); From c55f280c90520d676afc466d19bc86a8790e5342 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 23 Sep 2019 16:54:07 -0400 Subject: [PATCH 8/8] Add reference to test-utils in tsconfig.json files --- packages/jest-diff/tsconfig.json | 3 ++- packages/jest-matcher-utils/tsconfig.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-diff/tsconfig.json b/packages/jest-diff/tsconfig.json index 15ec4c0eff44..0e6330874c83 100644 --- a/packages/jest-diff/tsconfig.json +++ b/packages/jest-diff/tsconfig.json @@ -7,6 +7,7 @@ "references": [ {"path": "../diff-sequences"}, {"path": "../jest-get-type"}, - {"path": "../pretty-format"} + {"path": "../pretty-format"}, + {"path": "../test-utils"} ] } diff --git a/packages/jest-matcher-utils/tsconfig.json b/packages/jest-matcher-utils/tsconfig.json index 2248b5ff7fa1..febd499d97ae 100644 --- a/packages/jest-matcher-utils/tsconfig.json +++ b/packages/jest-matcher-utils/tsconfig.json @@ -7,6 +7,7 @@ "references": [ {"path": "../jest-diff"}, {"path": "../jest-get-type"}, - {"path": "../pretty-format"} + {"path": "../pretty-format"}, + {"path": "../test-utils"} ] }