From 20f037fd15d82a01e2042c31ccfa95dba24e51bd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Feb 2019 11:43:40 +0100 Subject: [PATCH] chore: migrate jest-diff to TypeScript (#7824) --- CHANGELOG.md | 1 + packages/diff-sequences/src/index.ts | 2 +- .../jest-circus/src/formatNodeAssertErrors.js | 3 +- packages/jest-diff/package.json | 5 ++ .../{diff.test.js.snap => diff.test.ts.snap} | 0 .../__tests__/{diff.test.js => diff.test.ts} | 11 ++-- .../src/{constants.js => constants.ts} | 2 - .../src/{diffStrings.js => diffStrings.ts} | 51 ++++++++++--------- packages/jest-diff/src/{index.js => index.ts} | 17 +++---- packages/jest-diff/src/types.ts | 13 +++++ packages/jest-diff/tsconfig.json | 12 +++++ .../src/assertionErrorMessage.js | 3 +- yarn.lock | 5 ++ 13 files changed, 80 insertions(+), 45 deletions(-) rename packages/jest-diff/src/__tests__/__snapshots__/{diff.test.js.snap => diff.test.ts.snap} (100%) rename packages/jest-diff/src/__tests__/{diff.test.js => diff.test.ts} (99%) rename packages/jest-diff/src/{constants.js => constants.ts} (97%) rename packages/jest-diff/src/{diffStrings.js => diffStrings.ts} (92%) rename packages/jest-diff/src/{index.js => index.ts} (92%) create mode 100644 packages/jest-diff/src/types.ts create mode 100644 packages/jest-diff/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index db7f53290797..eed385502916 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820)) - `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818)) - `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822)) +- `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824)) ### Performance diff --git a/packages/diff-sequences/src/index.ts b/packages/diff-sequences/src/index.ts index 3a676e0575dc..5dbbc4c6bd65 100644 --- a/packages/diff-sequences/src/index.ts +++ b/packages/diff-sequences/src/index.ts @@ -65,7 +65,7 @@ type FoundSubsequence = ( ) => void; // Either original functions or wrapped to swap indexes if graph is transposed. -type Callbacks = { +export type Callbacks = { foundSubsequence: FoundSubsequence; isCommon: IsCommon; }; diff --git a/packages/jest-circus/src/formatNodeAssertErrors.js b/packages/jest-circus/src/formatNodeAssertErrors.js index 6434eee318a5..e7f685f852f7 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.js +++ b/packages/jest-circus/src/formatNodeAssertErrors.js @@ -7,7 +7,8 @@ * @flow strict-local */ -import type {DiffOptions} from 'jest-diff/src/diffStrings'; +// $FlowFixMe: Converted to TS. It's also not exported, but should be imported from `matcher-utils` +import type {DiffOptions} from 'jest-diff'; import type {Event, State} from 'types/Circus'; import {diff, printExpected, printReceived} from 'jest-matcher-utils'; diff --git a/packages/jest-diff/package.json b/packages/jest-diff/package.json index b705af90783d..08db396fd77a 100644 --- a/packages/jest-diff/package.json +++ b/packages/jest-diff/package.json @@ -8,12 +8,17 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { "chalk": "^2.0.1", "diff-sequences": "^24.0.0", "jest-get-type": "^24.0.0", "pretty-format": "^24.0.0" }, + "devDependencies": { + "@types/strip-ansi": "^3.0.0", + "strip-ansi": "^5.0.0" + }, "engines": { "node": ">= 6" }, diff --git a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap similarity index 100% rename from packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap rename to packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap diff --git a/packages/jest-diff/src/__tests__/diff.test.js b/packages/jest-diff/src/__tests__/diff.test.ts similarity index 99% rename from packages/jest-diff/src/__tests__/diff.test.js rename to packages/jest-diff/src/__tests__/diff.test.ts index 50df67c274f7..090813e61f70 100644 --- a/packages/jest-diff/src/__tests__/diff.test.js +++ b/packages/jest-diff/src/__tests__/diff.test.ts @@ -3,16 +3,17 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -const stripAnsi = require('strip-ansi'); -const diff = require('../'); +import stripAnsi from 'strip-ansi'; + +import diff from '../'; +import {DiffOptions} from '../types'; const NO_DIFF_MESSAGE = 'Compared values have no visual difference.'; -const stripped = (a, b, options) => stripAnsi(diff(a, b, options)); +const stripped = (a: unknown, b: unknown, options?: DiffOptions) => + stripAnsi(diff(a, b, options) || ''); const unexpanded = {expand: false}; const expanded = {expand: true}; diff --git a/packages/jest-diff/src/constants.js b/packages/jest-diff/src/constants.ts similarity index 97% rename from packages/jest-diff/src/constants.js rename to packages/jest-diff/src/constants.ts index 83a4b905929a..9f3f45e7c63c 100644 --- a/packages/jest-diff/src/constants.js +++ b/packages/jest-diff/src/constants.ts @@ -3,8 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ import chalk from 'chalk'; diff --git a/packages/jest-diff/src/diffStrings.js b/packages/jest-diff/src/diffStrings.ts similarity index 92% rename from packages/jest-diff/src/diffStrings.js rename to packages/jest-diff/src/diffStrings.ts index 474278d63fe4..b71259c40485 100644 --- a/packages/jest-diff/src/diffStrings.js +++ b/packages/jest-diff/src/diffStrings.ts @@ -3,28 +3,19 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -import chalk from 'chalk'; -import type {Chalk} from 'chalk'; -import diff from 'diff-sequences'; -import {NO_DIFF_MESSAGE} from './constants.js'; +import chalk, {Chalk} from 'chalk'; +import diff, {Callbacks} from 'diff-sequences'; +import {NO_DIFF_MESSAGE} from './constants'; +import {DiffOptions} from './types'; const DIFF_CONTEXT_DEFAULT = 5; -export type DiffOptions = {| - aAnnotation?: string, - bAnnotation?: string, - expand?: boolean, - contextLines?: number, -|}; - -type Original = {| - a: string, - b: string, -|}; +type Original = { + a: string; + b: string; +}; const fgPatchMark = chalk.yellow; const fgDelete = chalk.green; @@ -52,7 +43,7 @@ type Highlight = (line: string, bgColor: Chalk) => string; const getHighlightSpaces = (bothEdges: boolean): Highlight => bothEdges ? highlightLeadingTrailingSpaces : highlightTrailingSpaces; -const getAnnotation = (options: ?DiffOptions): string => +const getAnnotation = (options?: DiffOptions): string => fgDelete('- ' + ((options && options.aAnnotation) || 'Expected')) + '\n' + fgInsert('+ ' + ((options && options.bAnnotation) || 'Received')) + @@ -134,9 +125,10 @@ const diffExpand = ( aLinesIn: Array, bLinesIn: Array, ): string => { - const isCommon = (aIndex, bIndex) => aLinesUn[aIndex] === bLinesUn[bIndex]; + const isCommon: Callbacks['isCommon'] = (aIndex, bIndex) => + aLinesUn[aIndex] === bLinesUn[bIndex]; - const array = []; + const array: string[] = []; const put = (line: string) => { array.push(line); }; @@ -144,7 +136,11 @@ const diffExpand = ( let aStart = 0; let bStart = 0; - const foundSubsequence = (nCommon, aCommon, bCommon) => { + const foundSubsequence: Callbacks['foundSubsequence'] = ( + nCommon, + aCommon, + bCommon, + ) => { formatDelete(aStart, aCommon, aLinesUn, aLinesIn, put); formatInsert(bStart, bCommon, bLinesUn, bLinesIn, put); formatCommon(nCommon, aCommon, bCommon, aLinesIn, bLinesUn, bLinesIn, put); @@ -175,7 +171,7 @@ const createPatchMark = ( `@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`, ); -const getContextLines = (options: ?DiffOptions): number => +const getContextLines = (options?: DiffOptions): number => options && typeof options.contextLines === 'number' && options.contextLines >= 0 @@ -193,7 +189,8 @@ const diffNoExpand = ( bLinesIn: Array, nContextLines: number, ): string => { - const isCommon = (aIndex, bIndex) => aLinesUn[aIndex] === bLinesUn[bIndex]; + const isCommon: Callbacks['isCommon'] = (aIndex, bIndex) => + aLinesUn[aIndex] === bLinesUn[bIndex]; let iPatchMark = 0; // index of placeholder line for patch mark const array = ['']; @@ -215,7 +212,11 @@ const diffNoExpand = ( // Given the number of items and starting indexes of each common subsequence, // format any preceding change lines, and then common context lines. - const foundSubsequence = (nCommon, aStartCommon, bStartCommon) => { + const foundSubsequence: Callbacks['foundSubsequence'] = ( + nCommon, + aStartCommon, + bStartCommon, + ) => { const aEndCommon = aStartCommon + nCommon; const bEndCommon = bStartCommon + nCommon; isAtEnd = aEndCommon === aLength && bEndCommon === bLength; @@ -294,7 +295,7 @@ const diffNoExpand = ( export default ( a: string, b: string, - options: ?DiffOptions, + options?: DiffOptions, original?: Original, ): string => { if (a === b) { diff --git a/packages/jest-diff/src/index.js b/packages/jest-diff/src/index.ts similarity index 92% rename from packages/jest-diff/src/index.js rename to packages/jest-diff/src/index.ts index a52fa1c1b524..176a8cbd5d38 100644 --- a/packages/jest-diff/src/index.js +++ b/packages/jest-diff/src/index.ts @@ -3,17 +3,14 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -import type {DiffOptions} from './diffStrings'; - import prettyFormat from 'pretty-format'; import chalk from 'chalk'; import getType from 'jest-get-type'; import diffStrings from './diffStrings'; import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants'; +import {DiffOptions} from './types'; const { AsymmetricMatcher, @@ -45,7 +42,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0}; // Generate a string that will highlight the difference between two values // with green and red. (similar to how github does code diffing) -function diff(a: any, b: any, options: ?DiffOptions): ?string { +function diff(a: any, b: any, options?: DiffOptions): string | null { if (Object.is(a, b)) { return NO_DIFF_MESSAGE; } @@ -98,7 +95,7 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string { function comparePrimitive( a: number | boolean, b: number | boolean, - options: ?DiffOptions, + options?: DiffOptions, ) { return diffStrings( prettyFormat(a, FORMAT_OPTIONS), @@ -107,15 +104,15 @@ function comparePrimitive( ); } -function sortMap(map) { +function sortMap(map: Map) { return new Map(Array.from(map.entries()).sort()); } -function sortSet(set) { +function sortSet(set: Set) { return new Set(Array.from(set.values()).sort()); } -function compareObjects(a: Object, b: Object, options: ?DiffOptions) { +function compareObjects(a: Object, b: Object, options?: DiffOptions) { let diffMessage; let hasThrown = false; @@ -153,4 +150,4 @@ function compareObjects(a: Object, b: Object, options: ?DiffOptions) { return diffMessage; } -module.exports = diff; +export = diff; diff --git a/packages/jest-diff/src/types.ts b/packages/jest-diff/src/types.ts new file mode 100644 index 000000000000..66edf604d0c0 --- /dev/null +++ b/packages/jest-diff/src/types.ts @@ -0,0 +1,13 @@ +/** + * 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. + */ + +export type DiffOptions = { + aAnnotation?: string; + bAnnotation?: string; + expand?: boolean; + contextLines?: number; +}; diff --git a/packages/jest-diff/tsconfig.json b/packages/jest-diff/tsconfig.json new file mode 100644 index 000000000000..15ec4c0eff44 --- /dev/null +++ b/packages/jest-diff/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../diff-sequences"}, + {"path": "../jest-get-type"}, + {"path": "../pretty-format"} + ] +} diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.js b/packages/jest-jasmine2/src/assertionErrorMessage.js index 3e066ae8377d..dd83b4cee4a8 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.js +++ b/packages/jest-jasmine2/src/assertionErrorMessage.js @@ -7,7 +7,8 @@ * @flow */ -import type {DiffOptions} from 'jest-diff/src/diffStrings'; +// TODO: Converted to TS. It's also not exported, but should be imported from `matcher-utils` +import type {DiffOptions} from 'jest-diff'; import {diff, printReceived, printExpected} from 'jest-matcher-utils'; import chalk from 'chalk'; diff --git a/yarn.lock b/yarn.lock index 6b1c4ffc6978..093b3ce1e7d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1782,6 +1782,11 @@ resolved "https://registry.yarnpkg.com/@types/string-length/-/string-length-2.0.0.tgz#358ce3ff2e8c2310270ee192ddd6b79b64fed7b2" integrity sha512-xFwWZpIWcLsrcVEybzxmxQM/26Snj1gqxmVrelC3We3Nub7q70RCtqTBVQ7nL+315fAcw4BGSFpxvMkhLApKmQ== +"@types/strip-ansi@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/strip-ansi/-/strip-ansi-3.0.0.tgz#9b63d453a6b54aa849182207711a08be8eea48ae" + integrity sha1-m2PUU6a1SqhJGCIHcRoIvo7qSK4= + "@types/strip-bom@3.0.0": version "3.0.0" resolved "https://registry.yarnpkg.com/@types/strip-bom/-/strip-bom-3.0.0.tgz#14a8ec3956c2e81edb7520790aecf21c290aebd2"