From 3adb67ba4b85134987b8cdb7f9a80e07776eeaca Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 24 Sep 2019 11:05:20 -0400 Subject: [PATCH] chore: Align snapshots in jest-matcher-utils (#8957) * chore: Align snapshots in jest-matcher-utils * Use new ConvertAnsi1 plugin in jest-diff and jest-matcher-utils * Add ConvertAnsi1.ts under plugins in pretty-format * Delete unneeded .ts file name extension in import statements * Add alignedAnsiStyleSerializer to test-utils * Delete ConvertAnsi1 from pretty-format * Fix prettier lint error * Add reference to test-utils in tsconfig.json files --- packages/jest-diff/src/__tests__/diff.test.ts | 43 +------- packages/jest-diff/tsconfig.json | 3 +- .../__snapshots__/index.test.ts.snap | 102 +++++++++--------- .../printDiffOrStringify.test.ts.snap | 48 ++++----- .../src/__tests__/index.test.ts | 16 +-- .../__tests__/printDiffOrStringify.test.ts | 3 + packages/jest-matcher-utils/tsconfig.json | 3 +- 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 +- 12 files changed, 174 insertions(+), 143 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 d75390fc9dc3..75b975ecfed0 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 {alignedAnsiStyleSerializer} from '@jest/test-utils'; 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(alignedAnsiStyleSerializer); describe('different types', () => { [ 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/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..681472a20080 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -7,6 +7,8 @@ */ import chalk from 'chalk'; +import prettyFormat = require('pretty-format'); +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import { MatcherHintOptions, diff, @@ -21,7 +23,9 @@ import { /* global BigInt */ const isBigIntDefined = typeof BigInt === 'function'; -describe('.stringify()', () => { +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); + +describe('stringify()', () => { [ [[], '[]'], [{}, '{}'], @@ -93,12 +97,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 +202,7 @@ describe('.ensureNumbers()', () => { }); }); -describe('.ensureNoExpected()', () => { +describe('ensureNoExpected()', () => { test('dont throw error when undefined', () => { expect(() => { // @ts-ignore @@ -251,7 +255,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..adaf7d7608ea 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -5,8 +5,11 @@ * LICENSE file in the root directory of this source tree. */ +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; import {EXPECTED_COLOR, INVERTED_COLOR, printDiffOrStringify} from '../index'; +expect.addSnapshotSerializer(alignedAnsiStyleSerializer); + describe('printDiffOrStringify', () => { const testDiffOrStringify = (expected: string, received: string): string => printDiffOrStringify(expected, received, 'Expected', 'Received', true); 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"} ] } 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==