Skip to content

Commit

Permalink
chore: migrate jest-diff to TypeScript (jestjs#7824)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and captain-yossarian committed Jul 18, 2019
1 parent c835f0e commit 2b993ac
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/diff-sequences/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-circus/src/formatNodeAssertErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) +
Expand Down Expand Up @@ -134,17 +125,22 @@ const diffExpand = (
aLinesIn: Array<string>,
bLinesIn: Array<string>,
): 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);
};

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);
Expand Down Expand Up @@ -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
Expand All @@ -193,7 +189,8 @@ const diffNoExpand = (
bLinesIn: Array<string>,
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 = [''];
Expand All @@ -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;
Expand Down Expand Up @@ -294,7 +295,7 @@ const diffNoExpand = (
export default (
a: string,
b: string,
options: ?DiffOptions,
options?: DiffOptions,
original?: Original,
): string => {
if (a === b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
Expand All @@ -107,15 +104,15 @@ function comparePrimitive(
);
}

function sortMap(map) {
function sortMap(map: Map<unknown, unknown>) {
return new Map(Array.from(map.entries()).sort());
}

function sortSet(set) {
function sortSet(set: Set<unknown>) {
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;

Expand Down Expand Up @@ -153,4 +150,4 @@ function compareObjects(a: Object, b: Object, options: ?DiffOptions) {
return diffMessage;
}

module.exports = diff;
export = diff;
13 changes: 13 additions & 0 deletions packages/jest-diff/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
12 changes: 12 additions & 0 deletions packages/jest-diff/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../diff-sequences"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"}
]
}
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/src/assertionErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2b993ac

Please sign in to comment.