From 0ecbf424b40faf54dcee401d211b43a60878e6e9 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:02:10 +0500 Subject: [PATCH 01/35] WIP - migration to ts (part 1) --- .../jest-snapshot/dts/natural-compare.d.ts | 4 + packages/jest-snapshot/package.json | 3 + .../jest-snapshot/src/{State.js => State.ts} | 52 ++++----- .../mock_serializer.test.ts.snap | 102 ++++++++++++++++++ .../snapshot_resolver.test.ts.snap | 18 ++++ ...shots.test.js => inline_snapshots.test.ts} | 47 ++------ .../{matcher.test.js => matcher.test.ts} | 5 +- ...alizer.test.js => mock_serializer.test.ts} | 0 .../{plugins.test.js => plugins.test.ts} | 2 +- ...lver.test.js => snapshot_resolver.test.ts} | 0 ..._matcher.test.js => throw_matcher.test.ts} | 5 +- .../{utils.test.js => utils.test.ts} | 4 +- .../jest-snapshot/src/{index.js => index.ts} | 43 ++++---- ...nline_snapshots.js => inline_snapshots.ts} | 53 +++++---- ...{mock_serializer.js => mock_serializer.ts} | 6 +- .../src/{plugins.js => plugins.ts} | 6 +- ...pshot_resolver.js => snapshot_resolver.ts} | 35 +++--- packages/jest-snapshot/src/types.ts | 9 ++ .../jest-snapshot/src/{utils.js => utils.ts} | 44 ++++---- packages/jest-snapshot/tsconfig.json | 7 ++ packages/pretty-format/src/index.ts | 7 +- 21 files changed, 298 insertions(+), 154 deletions(-) create mode 100644 packages/jest-snapshot/dts/natural-compare.d.ts rename packages/jest-snapshot/src/{State.js => State.ts} (89%) create mode 100644 packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.ts.snap create mode 100644 packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.ts.snap rename packages/jest-snapshot/src/__tests__/{inline_snapshots.test.js => inline_snapshots.test.ts} (83%) rename packages/jest-snapshot/src/__tests__/{matcher.test.js => matcher.test.ts} (81%) rename packages/jest-snapshot/src/__tests__/{mock_serializer.test.js => mock_serializer.test.ts} (100%) rename packages/jest-snapshot/src/__tests__/{plugins.test.js => plugins.test.ts} (96%) rename packages/jest-snapshot/src/__tests__/{snapshot_resolver.test.js => snapshot_resolver.test.ts} (100%) rename packages/jest-snapshot/src/__tests__/{throw_matcher.test.js => throw_matcher.test.ts} (94%) rename packages/jest-snapshot/src/__tests__/{utils.test.js => utils.test.ts} (98%) rename packages/jest-snapshot/src/{index.js => index.ts} (91%) rename packages/jest-snapshot/src/{inline_snapshots.js => inline_snapshots.ts} (81%) rename packages/jest-snapshot/src/{mock_serializer.js => mock_serializer.ts} (90%) rename packages/jest-snapshot/src/{plugins.js => plugins.ts} (88%) rename packages/jest-snapshot/src/{snapshot_resolver.js => snapshot_resolver.ts} (77%) create mode 100644 packages/jest-snapshot/src/types.ts rename packages/jest-snapshot/src/{utils.js => utils.ts} (86%) create mode 100644 packages/jest-snapshot/tsconfig.json diff --git a/packages/jest-snapshot/dts/natural-compare.d.ts b/packages/jest-snapshot/dts/natural-compare.d.ts new file mode 100644 index 000000000000..46f9107a7674 --- /dev/null +++ b/packages/jest-snapshot/dts/natural-compare.d.ts @@ -0,0 +1,4 @@ +declare module 'natural-compare' { + const NaturalCompare: (a: string, b: string) => number; + export default NaturalCompare; +} diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 43c27d8bbab0..267184d0b7c2 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -10,6 +10,7 @@ "main": "build/index.js", "dependencies": { "@babel/types": "^7.0.0", + "@jest/types": "^24.1.0", "chalk": "^2.0.1", "jest-diff": "^24.0.0", "jest-matcher-utils": "^24.0.0", @@ -23,6 +24,8 @@ "devDependencies": { "@types/mkdirp": "^0.5.2", "@types/semver": "^5.5.0", + "jest-haste-map": "^24.0.0", + "pretty-format": "^24.0.0", "prettier": "^1.13.4" }, "engines": { diff --git a/packages/jest-snapshot/src/State.js b/packages/jest-snapshot/src/State.ts similarity index 89% rename from packages/jest-snapshot/src/State.js rename to packages/jest-snapshot/src/State.ts index ed380afbc1fb..23efe169b8c7 100644 --- a/packages/jest-snapshot/src/State.js +++ b/packages/jest-snapshot/src/State.ts @@ -4,12 +4,12 @@ * 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 {Path, SnapshotUpdateState} from 'types/Config'; - import fs from 'fs'; +import {Config} from '@jest/types'; + import {getTopFrame, getStackTraceLines} from 'jest-message-util'; import { saveSnapshotFile, @@ -19,30 +19,30 @@ import { testNameToKey, unescape, } from './utils'; -import {saveInlineSnapshots, type InlineSnapshot} from './inline_snapshots'; +import {saveInlineSnapshots, InlineSnapshot} from './inline_snapshots'; -export type SnapshotStateOptions = {| - updateSnapshot: SnapshotUpdateState, - getPrettier: () => null | any, - getBabelTraverse: () => Function, - expand?: boolean, -|}; +export type SnapshotStateOptions = { + updateSnapshot: Config.SnapshotUpdateState; + getPrettier: () => null | any; + getBabelTraverse: () => Function; + expand?: boolean; +}; -export type SnapshotMatchOptions = {| - testName: string, - received: any, - key?: string, - inlineSnapshot?: string, - error?: Error, -|}; +export type SnapshotMatchOptions = { + testName: string; + received: any; + key?: string; + inlineSnapshot?: string; + error?: Error; +}; export default class SnapshotState { _counters: Map; _dirty: boolean; _index: number; - _updateSnapshot: SnapshotUpdateState; + _updateSnapshot: Config.SnapshotUpdateState; _snapshotData: {[key: string]: string}; - _snapshotPath: Path; + _snapshotPath: Config.Path; _inlineSnapshots: Array; _uncheckedKeys: Set; _getBabelTraverse: () => Function; @@ -53,7 +53,7 @@ export default class SnapshotState { unmatched: number; updated: number; - constructor(snapshotPath: Path, options: SnapshotStateOptions) { + constructor(snapshotPath: Config.Path, options: SnapshotStateOptions) { this._snapshotPath = snapshotPath; const {data, dirty} = getSnapshotData( this._snapshotPath, @@ -86,12 +86,12 @@ export default class SnapshotState { _addSnapshot( key: string, receivedSerialized: string, - options: {isInline: boolean, error?: Error}, + options: {isInline: boolean; error?: Error}, ) { this._dirty = true; if (options.isInline) { const error = options.error || new Error(); - const lines = getStackTraceLines(error.stack); + const lines = getStackTraceLines(error.stack || ''); const frame = getTopFrame(lines); if (!frame) { throw new Error( @@ -99,7 +99,11 @@ export default class SnapshotState { ); } this._inlineSnapshots.push({ - frame, + frame: { + column: frame.column, + file: frame.file as string, + line: frame.line, + }, snapshot: receivedSerialized, }); } else { @@ -251,7 +255,7 @@ export default class SnapshotState { } } - fail(testName: string, received: any, key?: string) { + fail(testName: string, _: any, key?: string) { this._counters.set(testName, (this._counters.get(testName) || 0) + 1); const count = Number(this._counters.get(testName)); diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.ts.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.ts.snap new file mode 100644 index 000000000000..f1fa6c9bd478 --- /dev/null +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.ts.snap @@ -0,0 +1,102 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`mock with 0 calls and default name 1`] = `[MockFunction]`; + +exports[`mock with 0 calls and default name in React element 1`] = ` + +`; + +exports[`mock with 0 calls and non-default name 1`] = `[MockFunction MyConstructor]`; + +exports[`mock with 1 calls and non-default name via new in object 1`] = ` +Object { + "fn": [MockFunction MyConstructor] { + "calls": Array [ + Array [ + Object { + "name": "some fine name", + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, +} +`; + +exports[`mock with 1 calls in React element 1`] = ` + +`; + +exports[`mock with 2 calls 1`] = ` +[MockFunction] { + "calls": Array [ + Array [], + Array [ + Object { + "foo": "bar", + }, + 42, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + Object { + "type": "return", + "value": undefined, + }, + ], +} +`; + +exports[`mock with 2 calls, 1 return, 1 throw 1`] = ` +[MockFunction] { + "calls": Array [ + Array [ + 2, + ], + Array [ + 3, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": 4, + }, + Object { + "type": "throw", + "value": [Error: Error Message!], + }, + ], +} +`; diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.ts.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.ts.snap new file mode 100644 index 000000000000..289fbf23b90d --- /dev/null +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.ts.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`malformed custom resolver in project config inconsistent functions throws 1`] = `"Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('foo/__tests__/bar.test.js')) === foo/__SPECS__/bar.test.js"`; + +exports[`malformed custom resolver in project config missing resolveSnapshotPath throws 1`] = ` +"Custom snapshot resolver must implement a \`resolveSnapshotPath\` as a function. +Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" +`; + +exports[`malformed custom resolver in project config missing resolveTestPath throws 1`] = ` +"Custom snapshot resolver must implement a \`resolveTestPath\` as a function. +Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" +`; + +exports[`malformed custom resolver in project config missing testPathForConsistencyCheck throws 1`] = ` +"Custom snapshot resolver must implement a \`testPathForConsistencyCheck\` as a string. +Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" +`; diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts similarity index 83% rename from packages/jest-snapshot/src/__tests__/inline_snapshots.test.js rename to packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index 63c744e73d0c..397a85e9b74b 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * */ jest.mock('fs'); @@ -23,40 +23,27 @@ const existsSync = fs.existsSync; const statSync = fs.statSync; const readdirSync = fs.readdirSync; beforeEach(() => { - // $FlowFixMe mock fs.writeFileSync = jest.fn(); - // $FlowFixMe mock fs.readFileSync = jest.fn(); - // $FlowFixMe mock fs.existsSync = jest.fn(() => true); - // $FlowFixMe mock fs.statSync = jest.fn(filePath => ({ isDirectory: () => !filePath.endsWith('.js'), })); - // $FlowFixMe mock fs.readdirSync = jest.fn(() => []); prettier.resolveConfig.sync.mockReset(); }); afterEach(() => { - // $FlowFixMe mock fs.writeFileSync = writeFileSync; - // $FlowFixMe mock fs.readFileSync = readFileSync; - // $FlowFixMe mock fs.existsSync = existsSync; - // $FlowFixMe mock fs.statSync = statSync; - // $FlowFixMe mock fs.readdirSync = readdirSync; }); test('saveInlineSnapshots() replaces empty function call with a template literal', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( - () => `expect(1).toMatchInlineSnapshot();\n`, - ): any); + fs.readFileSync = jest.fn(() => `expect(1).toMatchInlineSnapshot();\n`); saveInlineSnapshots( [ @@ -79,10 +66,7 @@ test.each([['babylon'], ['flow'], ['typescript']])( 'saveInlineSnapshots() replaces existing template literal - %s parser', parser => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( - () => 'expect(1).toMatchInlineSnapshot(`2`);\n', - ): any); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot(`2`);\n'); prettier.resolveConfig.sync.mockReturnValue({parser}); @@ -108,10 +92,9 @@ test.each([['babylon'], ['flow'], ['typescript']])( test('saveInlineSnapshots() replaces existing template literal with property matchers', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( + fs.readFileSync = jest.fn( () => 'expect(1).toMatchInlineSnapshot({}, `2`);\n', - ): any); + ); saveInlineSnapshots( [ @@ -132,10 +115,7 @@ test('saveInlineSnapshots() replaces existing template literal with property mat test('saveInlineSnapshots() throws if frame does not match', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( - () => 'expect(1).toMatchInlineSnapshot();\n', - ): any); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); const save = () => saveInlineSnapshots( @@ -154,10 +134,7 @@ test('saveInlineSnapshots() throws if frame does not match', () => { test('saveInlineSnapshots() throws if multiple calls to to the same location', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( - () => 'expect(1).toMatchInlineSnapshot();\n', - ): any); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); const frame = {column: 11, file: filename, line: 1}; const save = () => @@ -174,10 +151,7 @@ test('saveInlineSnapshots() throws if multiple calls to to the same location', ( test('saveInlineSnapshots() uses escaped backticks', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( - () => 'expect("`").toMatchInlineSnapshot();\n', - ): any); + fs.readFileSync = jest.fn(() => 'expect("`").toMatchInlineSnapshot();\n'); const frame = {column: 13, file: filename, line: 1}; saveInlineSnapshots([{frame, snapshot: '`'}], prettier, babelTraverse); @@ -190,10 +164,9 @@ test('saveInlineSnapshots() uses escaped backticks', () => { test('saveInlineSnapshots() works with non-literals in expect call', () => { const filename = path.join(__dirname, 'my.test.js'); - // $FlowFixMe mock - fs.readFileSync = (jest.fn( + fs.readFileSync = jest.fn( () => `expect({a: 'a'}).toMatchInlineSnapshot();\n`, - ): any); + ); prettier.resolveConfig.sync.mockReturnValue({ bracketSpacing: false, singleQuote: true, diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.js b/packages/jest-snapshot/src/__tests__/matcher.test.ts similarity index 81% rename from packages/jest-snapshot/src/__tests__/matcher.test.js rename to packages/jest-snapshot/src/__tests__/matcher.test.ts index e487a545dade..0bfe90e09fe8 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.js +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -13,7 +13,10 @@ it(`matcher returns matcher name, expected and actual values`, () => { const actual = 'a'; const expected = 'b'; const matcher = toMatchSnapshot.bind({ - snapshotState: {match: (testName, received) => ({actual, expected})}, + snapshotState: { + // @ts-ignore + match: (testName: string, received: any) => ({actual, expected}), // eslint-disable-line + }, }); const matcherResult = matcher({a: 1}); diff --git a/packages/jest-snapshot/src/__tests__/mock_serializer.test.js b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts similarity index 100% rename from packages/jest-snapshot/src/__tests__/mock_serializer.test.js rename to packages/jest-snapshot/src/__tests__/mock_serializer.test.ts diff --git a/packages/jest-snapshot/src/__tests__/plugins.test.js b/packages/jest-snapshot/src/__tests__/plugins.test.ts similarity index 96% rename from packages/jest-snapshot/src/__tests__/plugins.test.js rename to packages/jest-snapshot/src/__tests__/plugins.test.ts index 8f0a9629dc99..80e67a7bb743 100644 --- a/packages/jest-snapshot/src/__tests__/plugins.test.js +++ b/packages/jest-snapshot/src/__tests__/plugins.test.ts @@ -9,7 +9,7 @@ beforeEach(() => jest.resetModules()); -const testPath = names => { +const testPath = (names: string[]) => { const {addSerializer, getSerializers} = require('../plugins'); const prev = getSerializers(); const added = names.map(name => diff --git a/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.js b/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts similarity index 100% rename from packages/jest-snapshot/src/__tests__/snapshot_resolver.test.js rename to packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts diff --git a/packages/jest-snapshot/src/__tests__/throw_matcher.test.js b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts similarity index 94% rename from packages/jest-snapshot/src/__tests__/throw_matcher.test.js rename to packages/jest-snapshot/src/__tests__/throw_matcher.test.ts index d08d4e9877f9..a8517e3e0a0a 100644 --- a/packages/jest-snapshot/src/__tests__/throw_matcher.test.js +++ b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts @@ -5,11 +5,12 @@ * LICENSE file in the root directory of this source tree. * */ + 'use strict'; const {toThrowErrorMatchingSnapshot} = require('../'); -let matchFn; +let matchFn: jest.Mock; beforeEach(() => { matchFn = jest.fn(() => ({ @@ -33,7 +34,7 @@ it('throw matcher can take func', () => { }); describe('throw matcher from promise', () => { - let throwMatcher; + let throwMatcher: typeof toThrowErrorMatchingSnapshot; beforeEach(() => { throwMatcher = toThrowErrorMatchingSnapshot.bind({ diff --git a/packages/jest-snapshot/src/__tests__/utils.test.js b/packages/jest-snapshot/src/__tests__/utils.test.ts similarity index 98% rename from packages/jest-snapshot/src/__tests__/utils.test.js rename to packages/jest-snapshot/src/__tests__/utils.test.ts index d4d34a2d2131..e74fbc071ab7 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.js +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -177,8 +177,8 @@ test('escaping', () => { 'exports[`key`] = `"\'\\\\`;\n', ); - // eslint-disable-next-line no-unused-vars - const exports = {}; + // @ts-ignore + const exports = {}; // eslint-disable-line // eslint-disable-next-line no-eval const readData = eval('var exports = {}; ' + writtenData + ' exports'); expect(readData).toEqual({key: data}); diff --git a/packages/jest-snapshot/src/index.js b/packages/jest-snapshot/src/index.ts similarity index 91% rename from packages/jest-snapshot/src/index.js rename to packages/jest-snapshot/src/index.ts index 7c0cb0687303..0ea3ae924d34 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.ts @@ -4,17 +4,15 @@ * 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 {HasteFS} from 'types/HasteMap'; -import type {MatcherState} from 'types/Matchers'; -import type {Path, SnapshotUpdateState} from 'types/Config'; -import type {SnapshotResolver} from 'types/SnapshotResolver'; - import fs from 'fs'; +import {Config} from '@jest/types'; +import {FS as HasteFS} from 'jest-haste-map'; // eslint-disable-line import/no-extraneous-dependencies + import diff from 'jest-diff'; import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; +import {SnapshotResolver} from './types'; import { buildSnapshotResolver, isSnapshotPath, @@ -24,12 +22,14 @@ import SnapshotState from './State'; import {addSerializer, getSerializers} from './plugins'; import * as utils from './utils'; -const fileExists = (filePath: Path, hasteFS: HasteFS): boolean => +type Context = any & {dontThrow?: () => any}; + +const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean => hasteFS.exists(filePath) || fs.existsSync(filePath); const cleanup = ( hasteFS: HasteFS, - update: SnapshotUpdateState, + update: Config.SnapshotUpdateState, snapshotResolver: SnapshotResolver, ) => { const pattern = '\\.' + EXTENSION + '$'; @@ -51,6 +51,7 @@ const cleanup = ( }; const toMatchSnapshot = function( + this: Context, received: any, propertyMatchers?: any, testName?: string, @@ -70,6 +71,7 @@ const toMatchSnapshot = function( }; const toMatchInlineSnapshot = function( + this: Context, received: any, propertyMatchersOrInlineSnapshot?: any, inlineSnapshot?: string, @@ -95,11 +97,11 @@ const _toMatchSnapshot = ({ testName, inlineSnapshot, }: { - context: MatcherState & {dontThrow?: () => any}, - received: any, - propertyMatchers?: any, - testName?: string, - inlineSnapshot?: string, + context: Context; + received: any; + propertyMatchers?: any; + testName?: string; + inlineSnapshot?: string; }) => { context.dontThrow && context.dontThrow(); testName = typeof propertyMatchers === 'string' ? propertyMatchers : testName; @@ -168,7 +170,7 @@ const _toMatchSnapshot = ({ const {pass} = result; let {actual, expected} = result; - let report; + let report: () => string; if (pass) { return {message: () => '', pass: true}; } else if (!expected) { @@ -211,8 +213,10 @@ const _toMatchSnapshot = ({ }; const toThrowErrorMatchingSnapshot = function( + this: Context, received: any, testName?: string, + // @ts-ignore fromPromise: boolean, ) { return _toThrowErrorMatchingSnapshot({ @@ -224,6 +228,7 @@ const toThrowErrorMatchingSnapshot = function( }; const toThrowErrorMatchingInlineSnapshot = function( + this: Context, received: any, inlineSnapshot?: string, fromPromise?: boolean, @@ -243,11 +248,11 @@ const _toThrowErrorMatchingSnapshot = ({ fromPromise, inlineSnapshot, }: { - context: MatcherState & {dontThrow?: () => any}, - received: any, - testName?: string, - fromPromise?: boolean, - inlineSnapshot?: string, + context: Context; + received: any; + testName?: string; + fromPromise?: boolean; + inlineSnapshot?: string; }) => { context.dontThrow && context.dontThrow(); const {isNot} = context; diff --git a/packages/jest-snapshot/src/inline_snapshots.js b/packages/jest-snapshot/src/inline_snapshots.ts similarity index 81% rename from packages/jest-snapshot/src/inline_snapshots.js rename to packages/jest-snapshot/src/inline_snapshots.ts index cd171877a4a7..b22f7b638f5f 100644 --- a/packages/jest-snapshot/src/inline_snapshots.js +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -4,21 +4,25 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ import fs from 'fs'; -import semver from 'semver'; import path from 'path'; -import {templateElement, templateLiteral, file} from '@babel/types'; - -import type {Path} from 'types/Config'; +import semver from 'semver'; +import { + templateElement, + templateLiteral, + file, + CallExpression, +} from '@babel/types'; + +import {Config} from '@jest/types'; import {escapeBacktickString} from './utils'; -export type InlineSnapshot = {| - snapshot: string, - frame: {line: number, column: number, file: string}, -|}; +export type InlineSnapshot = { + snapshot: string; + frame: {line?: number; column?: number; file: string}; +}; export const saveInlineSnapshots = ( snapshots: InlineSnapshot[], @@ -54,7 +58,7 @@ export const saveInlineSnapshots = ( const saveSnapshotsForFile = ( snapshots: Array, - sourceFilePath: Path, + sourceFilePath: Config.Path, prettier: any, babelTraverse: Function, ) => { @@ -86,16 +90,19 @@ const saveSnapshotsForFile = ( } }; -const groupSnapshotsBy = (createKey: InlineSnapshot => string) => ( - snapshots: Array, -) => - snapshots.reduce((object, inlineSnapshot) => { - const key = createKey(inlineSnapshot); - return {...object, [key]: (object[key] || []).concat(inlineSnapshot)}; - }, {}); - -const groupSnapshotsByFrame = groupSnapshotsBy( - ({frame: {line, column}}) => `${line}:${column - 1}`, +const groupSnapshotsBy = ( + createKey: (inlineSnapshot: InlineSnapshot) => string, +) => (snapshots: Array) => + snapshots.reduce<{[key: string]: InlineSnapshot[]}>( + (object, inlineSnapshot) => { + const key = createKey(inlineSnapshot); + return {...object, [key]: (object[key] || []).concat(inlineSnapshot)}; + }, + {}, + ); + +const groupSnapshotsByFrame = groupSnapshotsBy(({frame: {line, column}}) => + line && column ? `${line}:${column - 1}` : '', ); const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file); @@ -105,7 +112,7 @@ const createParser = ( babelTraverse: Function, ) => ( text: string, - parsers: {[key: string]: (string) => any}, + parsers: {[key: string]: (arg0: string) => any}, options: any, ) => { // Workaround for https://github.com/prettier/prettier/issues/3150 @@ -122,7 +129,7 @@ const createParser = ( } babelTraverse(ast, { - CallExpression({node: {arguments: args, callee}}) { + CallExpression({node: {arguments: args, callee}}: {node: CallExpression}) { if ( callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier' @@ -167,7 +174,7 @@ const createParser = ( return ast; }; -const simpleDetectParser = (filePath: Path) => { +const simpleDetectParser = (filePath: Config.Path) => { const extname = path.extname(filePath); if (/tsx?$/.test(extname)) { return 'typescript'; diff --git a/packages/jest-snapshot/src/mock_serializer.js b/packages/jest-snapshot/src/mock_serializer.ts similarity index 90% rename from packages/jest-snapshot/src/mock_serializer.js rename to packages/jest-snapshot/src/mock_serializer.ts index 410c6d9f2a9b..6efa394b8d33 100644 --- a/packages/jest-snapshot/src/mock_serializer.js +++ b/packages/jest-snapshot/src/mock_serializer.ts @@ -4,10 +4,10 @@ * 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 {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; +import {Config, NewPlugin, Printer, Refs} from 'pretty-format'; export const serialize = ( val: any, @@ -46,4 +46,4 @@ export const serialize = ( export const test = (val: any) => val && !!val._isMockFunction; -export default ({serialize, test}: NewPlugin); +export default {serialize, test} as NewPlugin; diff --git a/packages/jest-snapshot/src/plugins.js b/packages/jest-snapshot/src/plugins.ts similarity index 88% rename from packages/jest-snapshot/src/plugins.js rename to packages/jest-snapshot/src/plugins.ts index e445ce59c2f0..96dc3c58df26 100644 --- a/packages/jest-snapshot/src/plugins.js +++ b/packages/jest-snapshot/src/plugins.ts @@ -4,12 +4,10 @@ * 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 {Plugin} from 'types/PrettyFormat'; - -import prettyFormat from 'pretty-format'; +import prettyFormat, {Plugin} from 'pretty-format'; import jestMockSerializer from './mock_serializer'; const { diff --git a/packages/jest-snapshot/src/snapshot_resolver.js b/packages/jest-snapshot/src/snapshot_resolver.ts similarity index 77% rename from packages/jest-snapshot/src/snapshot_resolver.js rename to packages/jest-snapshot/src/snapshot_resolver.ts index c8c8829aa19f..28ac08ae3874 100644 --- a/packages/jest-snapshot/src/snapshot_resolver.js +++ b/packages/jest-snapshot/src/snapshot_resolver.ts @@ -1,9 +1,10 @@ // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. -import type {ProjectConfig, Path} from 'types/Config'; -import type {SnapshotResolver} from 'types/SnapshotResolver'; -import chalk from 'chalk'; import path from 'path'; +import {Config} from '@jest/types'; +import chalk from 'chalk'; + +import {SnapshotResolver} from './types'; export const EXTENSION = 'snap'; export const DOT_EXTENSION = '.' + EXTENSION; @@ -11,32 +12,35 @@ export const DOT_EXTENSION = '.' + EXTENSION; export const isSnapshotPath = (path: string): boolean => path.endsWith(DOT_EXTENSION); -const cache: Map = new Map(); +const cache: Map = new Map(); export const buildSnapshotResolver = ( - config: ProjectConfig, + config: Config.ProjectConfig, ): SnapshotResolver => { const key = config.rootDir; if (!cache.has(key)) { cache.set(key, createSnapshotResolver(config.snapshotResolver)); } + // @ts-ignore return cache.get(key); }; -function createSnapshotResolver(snapshotResolverPath: ?Path): SnapshotResolver { +function createSnapshotResolver( + snapshotResolverPath?: Config.Path | null, +): SnapshotResolver { return typeof snapshotResolverPath === 'string' ? createCustomSnapshotResolver(snapshotResolverPath) : createDefaultSnapshotResolver(); } -function createDefaultSnapshotResolver() { +function createDefaultSnapshotResolver(): SnapshotResolver { return { - resolveSnapshotPath: (testPath: Path) => + resolveSnapshotPath: (testPath: Config.Path) => path.join( path.join(path.dirname(testPath), '__snapshots__'), path.basename(testPath) + DOT_EXTENSION, ), - resolveTestPath: (snapshotPath: Path) => + resolveTestPath: (snapshotPath: Config.Path) => path.resolve( path.dirname(snapshotPath), '..', @@ -52,24 +56,25 @@ function createDefaultSnapshotResolver() { } function createCustomSnapshotResolver( - snapshotResolverPath: Path, + snapshotResolverPath: Config.Path, ): SnapshotResolver { - const custom = (require(snapshotResolverPath): SnapshotResolver); + const custom: SnapshotResolver = require(snapshotResolverPath); - [ + const keys: [keyof SnapshotResolver, string][] = [ ['resolveSnapshotPath', 'function'], ['resolveTestPath', 'function'], ['testPathForConsistencyCheck', 'string'], - ].forEach(([propName, requiredType]) => { + ]; + keys.forEach(([propName, requiredType]) => { if (typeof custom[propName] !== requiredType) { throw new TypeError(mustImplement(propName, requiredType)); } }); const customResolver = { - resolveSnapshotPath: testPath => + resolveSnapshotPath: (testPath: Config.Path) => custom.resolveSnapshotPath(testPath, DOT_EXTENSION), - resolveTestPath: snapshotPath => + resolveTestPath: (snapshotPath: Config.Path) => custom.resolveTestPath(snapshotPath, DOT_EXTENSION), testPathForConsistencyCheck: custom.testPathForConsistencyCheck, }; diff --git a/packages/jest-snapshot/src/types.ts b/packages/jest-snapshot/src/types.ts new file mode 100644 index 000000000000..8cc5975cadeb --- /dev/null +++ b/packages/jest-snapshot/src/types.ts @@ -0,0 +1,9 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + +import {Config} from '@jest/types'; + +export type SnapshotResolver = { + testPathForConsistencyCheck: string; + resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path; + resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path; +}; diff --git a/packages/jest-snapshot/src/utils.js b/packages/jest-snapshot/src/utils.ts similarity index 86% rename from packages/jest-snapshot/src/utils.js rename to packages/jest-snapshot/src/utils.ts index 61b812ebdf68..7bbd9cacfb00 100644 --- a/packages/jest-snapshot/src/utils.js +++ b/packages/jest-snapshot/src/utils.ts @@ -4,18 +4,16 @@ * 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 {Path, SnapshotUpdateState} from 'types/Config'; - -import {getSerializers} from './plugins'; -import chalk from 'chalk'; import fs from 'fs'; +import path from 'path'; import mkdirp from 'mkdirp'; import naturalCompare from 'natural-compare'; -import path from 'path'; +import chalk from 'chalk'; +import {Config} from '@jest/types'; import prettyFormat from 'pretty-format'; +import {getSerializers} from './plugins'; export const SNAPSHOT_VERSION = '1'; const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/; @@ -76,14 +74,14 @@ const validateSnapshotVersion = (snapshotContents: string) => { return null; }; -function isObject(item) { +function isObject(item: any): boolean { return item && typeof item === 'object' && !Array.isArray(item); } -export const testNameToKey = (testName: string, count: number) => +export const testNameToKey = (testName: string, count: number): string => testName + ' ' + count; -export const keyToTestName = (key: string) => { +export const keyToTestName = (key: string): string => { if (!/ \d+$/.test(key)) { throw new Error('Snapshot keys must end with a number.'); } @@ -92,9 +90,12 @@ export const keyToTestName = (key: string) => { }; export const getSnapshotData = ( - snapshotPath: Path, - update: SnapshotUpdateState, -) => { + snapshotPath: Config.Path, + update: Config.SnapshotUpdateState, +): { + data: any; + dirty: boolean; +} => { const data = Object.create(null); let snapshotContents = ''; let dirty = false; @@ -104,7 +105,6 @@ export const getSnapshotData = ( snapshotContents = fs.readFileSync(snapshotPath, 'utf8'); // eslint-disable-next-line no-new-func const populate = new Function('exports', snapshotContents); - // $FlowFixMe populate(data); } catch (e) {} } @@ -120,15 +120,15 @@ export const getSnapshotData = ( dirty = true; } - return ({data, dirty}: {data: any, dirty: boolean}); + return {data, dirty}; }; // Extra line breaks at the beginning and at the end of the snapshot are useful // to make the content of the snapshot easier to read -const addExtraLineBreaks = string => +const addExtraLineBreaks = (string: string): string => string.includes('\n') ? `\n${string}\n` : string; -export const serialize = (data: any): string => +export const serialize = (data: string): string => addExtraLineBreaks( normalizeNewlines( prettyFormat(data, { @@ -140,25 +140,25 @@ export const serialize = (data: any): string => ); // unescape double quotes -export const unescape = (data: any): string => data.replace(/\\(")/g, '$1'); +export const unescape = (data: string): string => data.replace(/\\(")/g, '$1'); -export const escapeBacktickString = (str: string) => +export const escapeBacktickString = (str: string): string => str.replace(/`|\\|\${/g, '\\$&'); -const printBacktickString = (str: string) => +const printBacktickString = (str: string): string => '`' + escapeBacktickString(str) + '`'; -export const ensureDirectoryExists = (filePath: Path) => { +export const ensureDirectoryExists = (filePath: Config.Path) => { try { mkdirp.sync(path.join(path.dirname(filePath)), '777'); } catch (e) {} }; -const normalizeNewlines = string => string.replace(/\r\n|\r/g, '\n'); +const normalizeNewlines = (string: string) => string.replace(/\r\n|\r/g, '\n'); export const saveSnapshotFile = ( snapshotData: {[key: string]: string}, - snapshotPath: Path, + snapshotPath: Config.Path, ) => { const snapshots = Object.keys(snapshotData) .sort(naturalCompare) diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json new file mode 100644 index 000000000000..7bb06bce6d20 --- /dev/null +++ b/packages/jest-snapshot/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + } +} diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 3a496046fdff..720b993debed 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -16,8 +16,11 @@ import { Plugins, Refs, Theme, + Printer, } from './types'; +export * from './types'; + import { printIteratorEntries, printIteratorValues, @@ -520,4 +523,6 @@ prettyFormat.plugins = { ReactTestComponent, }; -export = prettyFormat; +export {Config, NewPlugin, Printer, Refs}; + +export default prettyFormat; From 7a181dca945300dd5e3bd44c640136273231c269 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:02:55 +0500 Subject: [PATCH 02/35] Remove old snapshots --- .../mock_serializer.test.js.snap | 102 ------------------ .../snapshot_resolver.test.js.snap | 18 ---- 2 files changed, 120 deletions(-) delete mode 100644 packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap delete mode 100644 packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.js.snap diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap deleted file mode 100644 index f1fa6c9bd478..000000000000 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap +++ /dev/null @@ -1,102 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`mock with 0 calls and default name 1`] = `[MockFunction]`; - -exports[`mock with 0 calls and default name in React element 1`] = ` - -`; - -exports[`mock with 0 calls and non-default name 1`] = `[MockFunction MyConstructor]`; - -exports[`mock with 1 calls and non-default name via new in object 1`] = ` -Object { - "fn": [MockFunction MyConstructor] { - "calls": Array [ - Array [ - Object { - "name": "some fine name", - }, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": undefined, - }, - ], - }, -} -`; - -exports[`mock with 1 calls in React element 1`] = ` - -`; - -exports[`mock with 2 calls 1`] = ` -[MockFunction] { - "calls": Array [ - Array [], - Array [ - Object { - "foo": "bar", - }, - 42, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": undefined, - }, - Object { - "type": "return", - "value": undefined, - }, - ], -} -`; - -exports[`mock with 2 calls, 1 return, 1 throw 1`] = ` -[MockFunction] { - "calls": Array [ - Array [ - 2, - ], - Array [ - 3, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": 4, - }, - Object { - "type": "throw", - "value": [Error: Error Message!], - }, - ], -} -`; diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.js.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.js.snap deleted file mode 100644 index 289fbf23b90d..000000000000 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/snapshot_resolver.test.js.snap +++ /dev/null @@ -1,18 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`malformed custom resolver in project config inconsistent functions throws 1`] = `"Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('foo/__tests__/bar.test.js')) === foo/__SPECS__/bar.test.js"`; - -exports[`malformed custom resolver in project config missing resolveSnapshotPath throws 1`] = ` -"Custom snapshot resolver must implement a \`resolveSnapshotPath\` as a function. -Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" -`; - -exports[`malformed custom resolver in project config missing resolveTestPath throws 1`] = ` -"Custom snapshot resolver must implement a \`resolveTestPath\` as a function. -Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" -`; - -exports[`malformed custom resolver in project config missing testPathForConsistencyCheck throws 1`] = ` -"Custom snapshot resolver must implement a \`testPathForConsistencyCheck\` as a string. -Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver" -`; From f12de382e054615c69befd58160429304e039d67 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:19:20 +0500 Subject: [PATCH 03/35] Use MatcherState type from expect (not implemented yet) --- packages/jest-snapshot/package.json | 3 ++- packages/jest-snapshot/src/index.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 267184d0b7c2..f89bd6e7ab88 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -26,7 +26,8 @@ "@types/semver": "^5.5.0", "jest-haste-map": "^24.0.0", "pretty-format": "^24.0.0", - "prettier": "^1.13.4" + "prettier": "^1.13.4", + "expect": "^24.1.0" }, "engines": { "node": ">= 6" diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 0ea3ae924d34..b1cf473c36a1 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -12,6 +12,7 @@ import {FS as HasteFS} from 'jest-haste-map'; // eslint-disable-line import/no-e import diff from 'jest-diff'; import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; +import {MatcherState} from 'expect'; import {SnapshotResolver} from './types'; import { buildSnapshotResolver, @@ -22,7 +23,7 @@ import SnapshotState from './State'; import {addSerializer, getSerializers} from './plugins'; import * as utils from './utils'; -type Context = any & {dontThrow?: () => any}; +type Context = MatcherState & {dontThrow?: () => any}; const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean => hasteFS.exists(filePath) || fs.existsSync(filePath); From 1c70f2381559dc5b2fa3f6934a498a6c1b72bfc2 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:28:40 +0500 Subject: [PATCH 04/35] Handle when line and column are zero --- packages/jest-snapshot/src/inline_snapshots.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index b22f7b638f5f..86c56e96d36a 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -102,7 +102,9 @@ const groupSnapshotsBy = ( ); const groupSnapshotsByFrame = groupSnapshotsBy(({frame: {line, column}}) => - line && column ? `${line}:${column - 1}` : '', + typeof line === 'number' && typeof column === 'number' + ? `${line}:${column - 1}` + : '', ); const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file); From 934b93ce953269ea4e28503c46a9b9f6ea3b8df2 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:31:50 +0500 Subject: [PATCH 05/35] Remove extra exports --- packages/pretty-format/src/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 720b993debed..d9e54274b650 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -16,7 +16,6 @@ import { Plugins, Refs, Theme, - Printer, } from './types'; export * from './types'; @@ -523,6 +522,4 @@ prettyFormat.plugins = { ReactTestComponent, }; -export {Config, NewPlugin, Printer, Refs}; - export default prettyFormat; From b94d602e552f1306a2a1fbea657cd89999e6bd23 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 10:42:49 +0500 Subject: [PATCH 06/35] Minor tweaks --- packages/jest-snapshot/package.json | 4 +- packages/jest-snapshot/src/State.ts | 114 +++++++++--------- packages/jest-snapshot/src/index.ts | 5 +- packages/jest-snapshot/src/mock_serializer.ts | 4 +- packages/jest-snapshot/src/types.ts | 2 + packages/jest-snapshot/src/utils.ts | 5 +- packages/jest-snapshot/tsconfig.json | 13 +- 7 files changed, 82 insertions(+), 65 deletions(-) diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index f89bd6e7ab88..7f47d62ec39d 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -24,10 +24,10 @@ "devDependencies": { "@types/mkdirp": "^0.5.2", "@types/semver": "^5.5.0", + "expect": "^24.1.0", "jest-haste-map": "^24.0.0", - "pretty-format": "^24.0.0", "prettier": "^1.13.4", - "expect": "^24.1.0" + "pretty-format": "^24.0.0" }, "engines": { "node": ">= 6" diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 23efe169b8c7..38d9ae38bc13 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -20,6 +20,7 @@ import { unescape, } from './utils'; import {saveInlineSnapshots, InlineSnapshot} from './inline_snapshots'; +import {SnapshotData} from './types'; export type SnapshotStateOptions = { updateSnapshot: Config.SnapshotUpdateState; @@ -37,16 +38,17 @@ export type SnapshotMatchOptions = { }; export default class SnapshotState { - _counters: Map; - _dirty: boolean; - _index: number; - _updateSnapshot: Config.SnapshotUpdateState; - _snapshotData: {[key: string]: string}; - _snapshotPath: Config.Path; - _inlineSnapshots: Array; - _uncheckedKeys: Set; - _getBabelTraverse: () => Function; - _getPrettier: () => null | any; + private counters: Map; + private dirty: boolean; + private index: number; + private updateSnapshot: Config.SnapshotUpdateState; + private snapshotData: SnapshotData; + private snapshotPath: Config.Path; + private inlineSnapshots: Array; + private uncheckedKeys: Set; + private getBabelTraverse: () => Function; + private getPrettier: () => null | any; + added: number; expand: boolean; matched: number; @@ -54,31 +56,31 @@ export default class SnapshotState { updated: number; constructor(snapshotPath: Config.Path, options: SnapshotStateOptions) { - this._snapshotPath = snapshotPath; + this.snapshotPath = snapshotPath; const {data, dirty} = getSnapshotData( - this._snapshotPath, + this.snapshotPath, options.updateSnapshot, ); - this._snapshotData = data; - this._dirty = dirty; - this._getBabelTraverse = options.getBabelTraverse; - this._getPrettier = options.getPrettier; - this._inlineSnapshots = []; - this._uncheckedKeys = new Set(Object.keys(this._snapshotData)); - this._counters = new Map(); - this._index = 0; + this.snapshotData = data; + this.dirty = dirty; + this.getBabelTraverse = options.getBabelTraverse; + this.getPrettier = options.getPrettier; + this.inlineSnapshots = []; + this.uncheckedKeys = new Set(Object.keys(this.snapshotData)); + this.counters = new Map(); + this.index = 0; this.expand = options.expand || false; this.added = 0; this.matched = 0; this.unmatched = 0; - this._updateSnapshot = options.updateSnapshot; + this.updateSnapshot = options.updateSnapshot; this.updated = 0; } markSnapshotsAsCheckedForTest(testName: string) { - this._uncheckedKeys.forEach(uncheckedKey => { + this.uncheckedKeys.forEach(uncheckedKey => { if (keyToTestName(uncheckedKey) === testName) { - this._uncheckedKeys.delete(uncheckedKey); + this.uncheckedKeys.delete(uncheckedKey); } }); } @@ -88,7 +90,7 @@ export default class SnapshotState { receivedSerialized: string, options: {isInline: boolean; error?: Error}, ) { - this._dirty = true; + this.dirty = true; if (options.isInline) { const error = options.error || new Error(); const lines = getStackTraceLines(error.stack || ''); @@ -98,7 +100,7 @@ export default class SnapshotState { "Jest: Couldn't infer stack frame for inline snapshot.", ); } - this._inlineSnapshots.push({ + this.inlineSnapshots.push({ frame: { column: frame.column, file: frame.file as string, @@ -107,13 +109,13 @@ export default class SnapshotState { snapshot: receivedSerialized, }); } else { - this._snapshotData[key] = receivedSerialized; + this.snapshotData[key] = receivedSerialized; } } save() { - const hasExternalSnapshots = Object.keys(this._snapshotData).length; - const hasInlineSnapshots = this._inlineSnapshots.length; + const hasExternalSnapshots = Object.keys(this.snapshotData).length; + const hasInlineSnapshots = this.inlineSnapshots.length; const isEmpty = !hasExternalSnapshots && !hasInlineSnapshots; const status = { @@ -121,19 +123,19 @@ export default class SnapshotState { saved: false, }; - if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) { + if ((this.dirty || this.uncheckedKeys.size) && !isEmpty) { if (hasExternalSnapshots) { - saveSnapshotFile(this._snapshotData, this._snapshotPath); + saveSnapshotFile(this.snapshotData, this.snapshotPath); } if (hasInlineSnapshots) { - const prettier = this._getPrettier(); // Load lazily - const babelTraverse = this._getBabelTraverse(); // Load lazily - saveInlineSnapshots(this._inlineSnapshots, prettier, babelTraverse); + const prettier = this.getPrettier(); // Load lazily + const babelTraverse = this.getBabelTraverse(); // Load lazily + saveInlineSnapshots(this.inlineSnapshots, prettier, babelTraverse); } status.saved = true; - } else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) { - if (this._updateSnapshot === 'all') { - fs.unlinkSync(this._snapshotPath); + } else if (!hasExternalSnapshots && fs.existsSync(this.snapshotPath)) { + if (this.updateSnapshot === 'all') { + fs.unlinkSync(this.snapshotPath); } status.deleted = true; } @@ -142,18 +144,18 @@ export default class SnapshotState { } getUncheckedCount(): number { - return this._uncheckedKeys.size || 0; + return this.uncheckedKeys.size || 0; } getUncheckedKeys(): Array { - return Array.from(this._uncheckedKeys); + return Array.from(this.uncheckedKeys); } removeUncheckedKeys(): void { - if (this._updateSnapshot === 'all' && this._uncheckedKeys.size) { - this._dirty = true; - this._uncheckedKeys.forEach(key => delete this._snapshotData[key]); - this._uncheckedKeys.clear(); + if (this.updateSnapshot === 'all' && this.uncheckedKeys.size) { + this.dirty = true; + this.uncheckedKeys.forEach(key => delete this.snapshotData[key]); + this.uncheckedKeys.clear(); } } @@ -164,8 +166,8 @@ export default class SnapshotState { inlineSnapshot, error, }: SnapshotMatchOptions) { - this._counters.set(testName, (this._counters.get(testName) || 0) + 1); - const count = Number(this._counters.get(testName)); + this.counters.set(testName, (this.counters.get(testName) || 0) + 1); + const count = Number(this.counters.get(testName)); const isInline = inlineSnapshot !== undefined; if (!key) { @@ -175,17 +177,17 @@ export default class SnapshotState { // Do not mark the snapshot as "checked" if the snapshot is inline and // there's an external snapshot. This way the external snapshot can be // removed with `--updateSnapshot`. - if (!(isInline && this._snapshotData[key])) { - this._uncheckedKeys.delete(key); + if (!(isInline && this.snapshotData[key])) { + this.uncheckedKeys.delete(key); } const receivedSerialized = serialize(received); - const expected = isInline ? inlineSnapshot : this._snapshotData[key]; + const expected = isInline ? inlineSnapshot : this.snapshotData[key]; const pass = expected === receivedSerialized; const hasSnapshot = isInline ? inlineSnapshot !== '' - : this._snapshotData[key] !== undefined; - const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath); + : this.snapshotData[key] !== undefined; + const snapshotIsPersisted = isInline || fs.existsSync(this.snapshotPath); if (pass && !isInline) { // Executing a snapshot file as JavaScript and writing the strings back @@ -194,7 +196,7 @@ export default class SnapshotState { // generated formatted string. // Note that this is only relevant when a snapshot is added and the dirty // flag is set. - this._snapshotData[key] = receivedSerialized; + this.snapshotData[key] = receivedSerialized; } // These are the conditions on when to write snapshots: @@ -205,11 +207,11 @@ export default class SnapshotState { // * The update flag is set to 'none'. // * There's no snapshot file or a file without this snapshot on a CI environment. if ( - (hasSnapshot && this._updateSnapshot === 'all') || + (hasSnapshot && this.updateSnapshot === 'all') || ((!hasSnapshot || !snapshotIsPersisted) && - (this._updateSnapshot === 'new' || this._updateSnapshot === 'all')) + (this.updateSnapshot === 'new' || this.updateSnapshot === 'all')) ) { - if (this._updateSnapshot === 'all') { + if (this.updateSnapshot === 'all') { if (!pass) { if (hasSnapshot) { this.updated++; @@ -256,14 +258,14 @@ export default class SnapshotState { } fail(testName: string, _: any, key?: string) { - this._counters.set(testName, (this._counters.get(testName) || 0) + 1); - const count = Number(this._counters.get(testName)); + this.counters.set(testName, (this.counters.get(testName) || 0) + 1); + const count = Number(this.counters.get(testName)); if (!key) { key = testNameToKey(testName, count); } - this._uncheckedKeys.delete(key); + this.uncheckedKeys.delete(key); this.unmatched++; return key; } diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index b1cf473c36a1..04de8418024f 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -7,12 +7,11 @@ */ import fs from 'fs'; -import {Config} from '@jest/types'; +import {Config, MatcherState} from '@jest/types'; import {FS as HasteFS} from 'jest-haste-map'; // eslint-disable-line import/no-extraneous-dependencies import diff from 'jest-diff'; import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; -import {MatcherState} from 'expect'; import {SnapshotResolver} from './types'; import { buildSnapshotResolver, @@ -55,7 +54,7 @@ const toMatchSnapshot = function( this: Context, received: any, propertyMatchers?: any, - testName?: string, + testName?: Config.Path, ) { if (arguments.length === 3 && !propertyMatchers) { throw new Error( diff --git a/packages/jest-snapshot/src/mock_serializer.ts b/packages/jest-snapshot/src/mock_serializer.ts index 6efa394b8d33..a38145c6c7c2 100644 --- a/packages/jest-snapshot/src/mock_serializer.ts +++ b/packages/jest-snapshot/src/mock_serializer.ts @@ -46,4 +46,6 @@ export const serialize = ( export const test = (val: any) => val && !!val._isMockFunction; -export default {serialize, test} as NewPlugin; +const plugin: NewPlugin = {serialize, test}; + +export default plugin; diff --git a/packages/jest-snapshot/src/types.ts b/packages/jest-snapshot/src/types.ts index 8cc5975cadeb..7f5b8a4d2bcc 100644 --- a/packages/jest-snapshot/src/types.ts +++ b/packages/jest-snapshot/src/types.ts @@ -7,3 +7,5 @@ export type SnapshotResolver = { resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path; resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path; }; + +export type SnapshotData = {[key: string]: string}; diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 7bbd9cacfb00..2802f3091252 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -14,6 +14,7 @@ import chalk from 'chalk'; import {Config} from '@jest/types'; import prettyFormat from 'pretty-format'; import {getSerializers} from './plugins'; +import {SnapshotData} from './types'; export const SNAPSHOT_VERSION = '1'; const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/; @@ -74,7 +75,7 @@ const validateSnapshotVersion = (snapshotContents: string) => { return null; }; -function isObject(item: any): boolean { +function isObject(item: unknown): boolean { return item && typeof item === 'object' && !Array.isArray(item); } @@ -93,7 +94,7 @@ export const getSnapshotData = ( snapshotPath: Config.Path, update: Config.SnapshotUpdateState, ): { - data: any; + data: SnapshotData; dirty: boolean; } => { const data = Object.create(null); diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index 7bb06bce6d20..8dbd51cf07fd 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -3,5 +3,16 @@ "compilerOptions": { "rootDir": "src", "outDir": "build" - } + }, + "references": [ + { + "path": "../jest-types" + }, + { + "path": "../jest-haste-map" + }, + { + "path": "../pretty-format" + } + ] } From 7dc5065ab1b301ef177f8421ae0ebff4ec241480 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Feb 2019 14:22:24 +0500 Subject: [PATCH 07/35] Small tweaks Co-Authored-By: doniyor2109 --- packages/jest-snapshot/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 2802f3091252..b56d683a349e 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -79,7 +79,7 @@ function isObject(item: unknown): boolean { return item && typeof item === 'object' && !Array.isArray(item); } -export const testNameToKey = (testName: string, count: number): string => +export const testNameToKey = (testName: Config.Path, count: number): string => testName + ' ' + count; export const keyToTestName = (key: string): string => { From b6522be6dc2067ff6156bc4f5573ebc621731fe2 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 14:22:57 +0500 Subject: [PATCH 08/35] Small tweaks --- packages/jest-snapshot/src/State.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 38d9ae38bc13..53690b057497 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * */ import fs from 'fs'; From 27837ec10e22c3995e6006fe0ecd427610a43d72 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 14:25:46 +0500 Subject: [PATCH 09/35] Revert underscores --- packages/jest-snapshot/src/State.ts | 112 ++++++++++++++-------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 53690b057497..e97fb040ff21 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -37,16 +37,16 @@ export type SnapshotMatchOptions = { }; export default class SnapshotState { - private counters: Map; - private dirty: boolean; - private index: number; - private updateSnapshot: Config.SnapshotUpdateState; - private snapshotData: SnapshotData; - private snapshotPath: Config.Path; - private inlineSnapshots: Array; - private uncheckedKeys: Set; - private getBabelTraverse: () => Function; - private getPrettier: () => null | any; + private _counters: Map; + private _dirty: boolean; + private _index: number; + private _updateSnapshot: Config.SnapshotUpdateState; + private _snapshotData: SnapshotData; + private _snapshotPath: Config.Path; + private _inlineSnapshots: Array; + private _uncheckedKeys: Set; + private _getBabelTraverse: () => Function; + private _getPrettier: () => null | any; added: number; expand: boolean; @@ -55,31 +55,31 @@ export default class SnapshotState { updated: number; constructor(snapshotPath: Config.Path, options: SnapshotStateOptions) { - this.snapshotPath = snapshotPath; + this._snapshotPath = snapshotPath; const {data, dirty} = getSnapshotData( - this.snapshotPath, + this._snapshotPath, options.updateSnapshot, ); - this.snapshotData = data; - this.dirty = dirty; - this.getBabelTraverse = options.getBabelTraverse; - this.getPrettier = options.getPrettier; - this.inlineSnapshots = []; - this.uncheckedKeys = new Set(Object.keys(this.snapshotData)); - this.counters = new Map(); - this.index = 0; + this._snapshotData = data; + this._dirty = dirty; + this._getBabelTraverse = options.getBabelTraverse; + this._getPrettier = options.getPrettier; + this._inlineSnapshots = []; + this._uncheckedKeys = new Set(Object.keys(this._snapshotData)); + this._counters = new Map(); + this._index = 0; this.expand = options.expand || false; this.added = 0; this.matched = 0; this.unmatched = 0; - this.updateSnapshot = options.updateSnapshot; + this._updateSnapshot = options.updateSnapshot; this.updated = 0; } markSnapshotsAsCheckedForTest(testName: string) { - this.uncheckedKeys.forEach(uncheckedKey => { + this._uncheckedKeys.forEach(uncheckedKey => { if (keyToTestName(uncheckedKey) === testName) { - this.uncheckedKeys.delete(uncheckedKey); + this._uncheckedKeys.delete(uncheckedKey); } }); } @@ -89,7 +89,7 @@ export default class SnapshotState { receivedSerialized: string, options: {isInline: boolean; error?: Error}, ) { - this.dirty = true; + this._dirty = true; if (options.isInline) { const error = options.error || new Error(); const lines = getStackTraceLines(error.stack || ''); @@ -99,7 +99,7 @@ export default class SnapshotState { "Jest: Couldn't infer stack frame for inline snapshot.", ); } - this.inlineSnapshots.push({ + this._inlineSnapshots.push({ frame: { column: frame.column, file: frame.file as string, @@ -108,13 +108,13 @@ export default class SnapshotState { snapshot: receivedSerialized, }); } else { - this.snapshotData[key] = receivedSerialized; + this._snapshotData[key] = receivedSerialized; } } save() { - const hasExternalSnapshots = Object.keys(this.snapshotData).length; - const hasInlineSnapshots = this.inlineSnapshots.length; + const hasExternalSnapshots = Object.keys(this._snapshotData).length; + const hasInlineSnapshots = this._inlineSnapshots.length; const isEmpty = !hasExternalSnapshots && !hasInlineSnapshots; const status = { @@ -122,19 +122,19 @@ export default class SnapshotState { saved: false, }; - if ((this.dirty || this.uncheckedKeys.size) && !isEmpty) { + if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) { if (hasExternalSnapshots) { - saveSnapshotFile(this.snapshotData, this.snapshotPath); + saveSnapshotFile(this._snapshotData, this._snapshotPath); } if (hasInlineSnapshots) { - const prettier = this.getPrettier(); // Load lazily - const babelTraverse = this.getBabelTraverse(); // Load lazily - saveInlineSnapshots(this.inlineSnapshots, prettier, babelTraverse); + const prettier = this._getPrettier(); // Load lazily + const babelTraverse = this._getBabelTraverse(); // Load lazily + saveInlineSnapshots(this._inlineSnapshots, prettier, babelTraverse); } status.saved = true; - } else if (!hasExternalSnapshots && fs.existsSync(this.snapshotPath)) { - if (this.updateSnapshot === 'all') { - fs.unlinkSync(this.snapshotPath); + } else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) { + if (this._updateSnapshot === 'all') { + fs.unlinkSync(this._snapshotPath); } status.deleted = true; } @@ -143,18 +143,18 @@ export default class SnapshotState { } getUncheckedCount(): number { - return this.uncheckedKeys.size || 0; + return this._uncheckedKeys.size || 0; } getUncheckedKeys(): Array { - return Array.from(this.uncheckedKeys); + return Array.from(this._uncheckedKeys); } removeUncheckedKeys(): void { - if (this.updateSnapshot === 'all' && this.uncheckedKeys.size) { - this.dirty = true; - this.uncheckedKeys.forEach(key => delete this.snapshotData[key]); - this.uncheckedKeys.clear(); + if (this._updateSnapshot === 'all' && this._uncheckedKeys.size) { + this._dirty = true; + this._uncheckedKeys.forEach(key => delete this._snapshotData[key]); + this._uncheckedKeys.clear(); } } @@ -165,8 +165,8 @@ export default class SnapshotState { inlineSnapshot, error, }: SnapshotMatchOptions) { - this.counters.set(testName, (this.counters.get(testName) || 0) + 1); - const count = Number(this.counters.get(testName)); + this._counters.set(testName, (this._counters.get(testName) || 0) + 1); + const count = Number(this._counters.get(testName)); const isInline = inlineSnapshot !== undefined; if (!key) { @@ -176,17 +176,17 @@ export default class SnapshotState { // Do not mark the snapshot as "checked" if the snapshot is inline and // there's an external snapshot. This way the external snapshot can be // removed with `--updateSnapshot`. - if (!(isInline && this.snapshotData[key])) { - this.uncheckedKeys.delete(key); + if (!(isInline && this._snapshotData[key])) { + this._uncheckedKeys.delete(key); } const receivedSerialized = serialize(received); - const expected = isInline ? inlineSnapshot : this.snapshotData[key]; + const expected = isInline ? inlineSnapshot : this._snapshotData[key]; const pass = expected === receivedSerialized; const hasSnapshot = isInline ? inlineSnapshot !== '' - : this.snapshotData[key] !== undefined; - const snapshotIsPersisted = isInline || fs.existsSync(this.snapshotPath); + : this._snapshotData[key] !== undefined; + const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath); if (pass && !isInline) { // Executing a snapshot file as JavaScript and writing the strings back @@ -195,7 +195,7 @@ export default class SnapshotState { // generated formatted string. // Note that this is only relevant when a snapshot is added and the dirty // flag is set. - this.snapshotData[key] = receivedSerialized; + this._snapshotData[key] = receivedSerialized; } // These are the conditions on when to write snapshots: @@ -206,11 +206,11 @@ export default class SnapshotState { // * The update flag is set to 'none'. // * There's no snapshot file or a file without this snapshot on a CI environment. if ( - (hasSnapshot && this.updateSnapshot === 'all') || + (hasSnapshot && this._updateSnapshot === 'all') || ((!hasSnapshot || !snapshotIsPersisted) && - (this.updateSnapshot === 'new' || this.updateSnapshot === 'all')) + (this._updateSnapshot === 'new' || this._updateSnapshot === 'all')) ) { - if (this.updateSnapshot === 'all') { + if (this._updateSnapshot === 'all') { if (!pass) { if (hasSnapshot) { this.updated++; @@ -257,14 +257,14 @@ export default class SnapshotState { } fail(testName: string, _: any, key?: string) { - this.counters.set(testName, (this.counters.get(testName) || 0) + 1); - const count = Number(this.counters.get(testName)); + this._counters.set(testName, (this._counters.get(testName) || 0) + 1); + const count = Number(this._counters.get(testName)); if (!key) { key = testNameToKey(testName, count); } - this.uncheckedKeys.delete(key); + this._uncheckedKeys.delete(key); this.unmatched++; return key; } From c7991d821fb761a98c783b3bed82ffa4913dfc15 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 14:27:09 +0500 Subject: [PATCH 10/35] Private methods --- packages/jest-snapshot/src/State.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index e97fb040ff21..f7f386ea80c9 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -84,7 +84,7 @@ export default class SnapshotState { }); } - _addSnapshot( + private _addSnapshot( key: string, receivedSerialized: string, options: {isInline: boolean; error?: Error}, From 6cd97aa38113975010cdd1bdfb5e32b519d22993 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 17:41:02 +0500 Subject: [PATCH 11/35] Minor tweaks --- packages/jest-snapshot/src/State.ts | 1 + packages/jest-snapshot/src/mock_serializer.ts | 10 +- packages/jest-snapshot/src/plugins.ts | 8 +- packages/jest-types/src/PrettyFormat.ts | 117 ++++++++++++++++++ packages/jest-types/src/index.ts | 11 +- packages/pretty-format/package.json | 1 + packages/pretty-format/src/index.ts | 4 +- packages/pretty-format/src/types.ts | 111 +---------------- packages/pretty-format/tsconfig.json | 4 + 9 files changed, 146 insertions(+), 121 deletions(-) create mode 100644 packages/jest-types/src/PrettyFormat.ts diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index f7f386ea80c9..77eaf1972bbc 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -39,6 +39,7 @@ export type SnapshotMatchOptions = { export default class SnapshotState { private _counters: Map; private _dirty: boolean; + // @ts-ignore private _index: number; private _updateSnapshot: Config.SnapshotUpdateState; private _snapshotData: SnapshotData; diff --git a/packages/jest-snapshot/src/mock_serializer.ts b/packages/jest-snapshot/src/mock_serializer.ts index a38145c6c7c2..9ad41676245a 100644 --- a/packages/jest-snapshot/src/mock_serializer.ts +++ b/packages/jest-snapshot/src/mock_serializer.ts @@ -7,15 +7,15 @@ * */ -import {Config, NewPlugin, Printer, Refs} from 'pretty-format'; +import {PrettyFormat} from '@jest/types'; export const serialize = ( val: any, - config: Config, + config: PrettyFormat.Config, indentation: string, depth: number, - refs: Refs, - printer: Printer, + refs: PrettyFormat.Refs, + printer: PrettyFormat.Printer, ): string => { // Serialize a non-default name, even if config.printFunctionName is false. const name = val.getMockName(); @@ -46,6 +46,6 @@ export const serialize = ( export const test = (val: any) => val && !!val._isMockFunction; -const plugin: NewPlugin = {serialize, test}; +const plugin: PrettyFormat.NewPlugin = {serialize, test}; export default plugin; diff --git a/packages/jest-snapshot/src/plugins.ts b/packages/jest-snapshot/src/plugins.ts index 96dc3c58df26..f02664ec83ad 100644 --- a/packages/jest-snapshot/src/plugins.ts +++ b/packages/jest-snapshot/src/plugins.ts @@ -7,7 +7,9 @@ * */ -import prettyFormat, {Plugin} from 'pretty-format'; +import prettyFormat from 'pretty-format'; +import {PrettyFormat} from '@jest/types'; + import jestMockSerializer from './mock_serializer'; const { @@ -19,7 +21,7 @@ const { AsymmetricMatcher, } = prettyFormat.plugins; -let PLUGINS: Array = [ +let PLUGINS: Array = [ ReactTestComponent, ReactElement, DOMElement, @@ -30,7 +32,7 @@ let PLUGINS: Array = [ ]; // Prepend to list so the last added is the first tested. -export const addSerializer = (plugin: Plugin) => { +export const addSerializer = (plugin: PrettyFormat.Plugin) => { PLUGINS = [plugin].concat(PLUGINS); }; diff --git a/packages/jest-types/src/PrettyFormat.ts b/packages/jest-types/src/PrettyFormat.ts new file mode 100644 index 000000000000..89d06d5bd418 --- /dev/null +++ b/packages/jest-types/src/PrettyFormat.ts @@ -0,0 +1,117 @@ +/** + * 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 Colors = { + comment: {close: string; open: string}; + content: {close: string; open: string}; + prop: {close: string; open: string}; + tag: {close: string; open: string}; + value: {close: string; open: string}; +}; +type Indent = (arg0: string) => string; +export type Refs = Array; +type Print = (arg0: any) => string; + +export type Theme = { + comment: string; + content: string; + prop: string; + tag: string; + value: string; +}; + +type ThemeReceived = { + comment?: string; + content?: string; + prop?: string; + tag?: string; + value?: string; +}; + +export type Options = { + callToJSON: boolean; + escapeRegex: boolean; + escapeString: boolean; + highlight: boolean; + indent: number; + maxDepth: number; + min: boolean; + plugins: Plugins; + printFunctionName: boolean; + theme: Theme; +}; + +export type OptionsReceived = { + callToJSON?: boolean; + escapeRegex?: boolean; + escapeString?: boolean; + highlight?: boolean; + indent?: number; + maxDepth?: number; + min?: boolean; + plugins?: Plugins; + printFunctionName?: boolean; + theme?: ThemeReceived; +}; + +export type Config = { + callToJSON: boolean; + colors: Colors; + escapeRegex: boolean; + escapeString: boolean; + indent: string; + maxDepth: number; + min: boolean; + plugins: Plugins; + printFunctionName: boolean; + spacingInner: string; + spacingOuter: string; +}; + +export type Printer = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + hasCalledToJSON?: boolean, +) => string; + +type Test = (arg0: any) => boolean; + +export type NewPlugin = { + serialize: ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + ) => string; + test: Test; +}; + +type PluginOptions = { + edgeSpacing: string; + min: boolean; + spacing: string; +}; + +type OldPlugin = { + print: ( + val: any, + print: Print, + indent: Indent, + options: PluginOptions, + colors: Colors, + ) => string; + test: Test; +}; + +export type Plugin = NewPlugin | OldPlugin; + +export type Plugins = Array; diff --git a/packages/jest-types/src/index.ts b/packages/jest-types/src/index.ts index 769fb97b881b..5655021bc624 100644 --- a/packages/jest-types/src/index.ts +++ b/packages/jest-types/src/index.ts @@ -11,5 +11,14 @@ import * as SourceMaps from './SourceMaps'; import * as TestResult from './TestResult'; import * as Mocks from './Mocks'; import * as Transform from './Transform'; +import * as PrettyFormat from './PrettyFormat'; -export {Config, Console, SourceMaps, TestResult, Mocks, Transform}; +export { + Config, + Console, + SourceMaps, + TestResult, + Mocks, + Transform, + PrettyFormat, +}; diff --git a/packages/pretty-format/package.json b/packages/pretty-format/package.json index ae89d57ddda0..a1d531fed9ec 100644 --- a/packages/pretty-format/package.json +++ b/packages/pretty-format/package.json @@ -21,6 +21,7 @@ "@types/ansi-styles": "^3.2.1", "@types/react": "*", "@types/react-test-renderer": "*", + "@jest/types": "^24.1.0", "immutable": "4.0.0-rc.9", "react": "*", "react-dom": "*", diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index d9e54274b650..3a496046fdff 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -18,8 +18,6 @@ import { Theme, } from './types'; -export * from './types'; - import { printIteratorEntries, printIteratorValues, @@ -522,4 +520,4 @@ prettyFormat.plugins = { ReactTestComponent, }; -export default prettyFormat; +export = prettyFormat; diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 89d06d5bd418..c2586b1b96f9 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -5,113 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -export type Colors = { - comment: {close: string; open: string}; - content: {close: string; open: string}; - prop: {close: string; open: string}; - tag: {close: string; open: string}; - value: {close: string; open: string}; -}; -type Indent = (arg0: string) => string; -export type Refs = Array; -type Print = (arg0: any) => string; +import {PrettyFormat} from '@jest/types'; -export type Theme = { - comment: string; - content: string; - prop: string; - tag: string; - value: string; -}; - -type ThemeReceived = { - comment?: string; - content?: string; - prop?: string; - tag?: string; - value?: string; -}; - -export type Options = { - callToJSON: boolean; - escapeRegex: boolean; - escapeString: boolean; - highlight: boolean; - indent: number; - maxDepth: number; - min: boolean; - plugins: Plugins; - printFunctionName: boolean; - theme: Theme; -}; - -export type OptionsReceived = { - callToJSON?: boolean; - escapeRegex?: boolean; - escapeString?: boolean; - highlight?: boolean; - indent?: number; - maxDepth?: number; - min?: boolean; - plugins?: Plugins; - printFunctionName?: boolean; - theme?: ThemeReceived; -}; - -export type Config = { - callToJSON: boolean; - colors: Colors; - escapeRegex: boolean; - escapeString: boolean; - indent: string; - maxDepth: number; - min: boolean; - plugins: Plugins; - printFunctionName: boolean; - spacingInner: string; - spacingOuter: string; -}; - -export type Printer = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - hasCalledToJSON?: boolean, -) => string; - -type Test = (arg0: any) => boolean; - -export type NewPlugin = { - serialize: ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, - ) => string; - test: Test; -}; - -type PluginOptions = { - edgeSpacing: string; - min: boolean; - spacing: string; -}; - -type OldPlugin = { - print: ( - val: any, - print: Print, - indent: Indent, - options: PluginOptions, - colors: Colors, - ) => string; - test: Test; -}; - -export type Plugin = NewPlugin | OldPlugin; - -export type Plugins = Array; +export = PrettyFormat; diff --git a/packages/pretty-format/tsconfig.json b/packages/pretty-format/tsconfig.json index 7bb06bce6d20..67f35375a868 100644 --- a/packages/pretty-format/tsconfig.json +++ b/packages/pretty-format/tsconfig.json @@ -3,5 +3,9 @@ "compilerOptions": { "rootDir": "src", "outDir": "build" + }, + "references": [{ + "path": "../jest-types" } + ] } From cdb6096c2f5884ef8ecbdeaf7439568dadec36fe Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Feb 2019 17:43:03 +0500 Subject: [PATCH 12/35] Meaningful name Co-Authored-By: doniyor2109 --- packages/jest-snapshot/src/inline_snapshots.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index 86c56e96d36a..0d04a07f8153 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -114,7 +114,7 @@ const createParser = ( babelTraverse: Function, ) => ( text: string, - parsers: {[key: string]: (arg0: string) => any}, + parsers: {[key: string]: (text: string) => any}, options: any, ) => { // Workaround for https://github.com/prettier/prettier/issues/3150 From 5706125baea4b8b2da65d2092df4aabc46166d11 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 17:47:30 +0500 Subject: [PATCH 13/35] Remove extra ts-ignore --- packages/jest-snapshot/src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 04de8418024f..97cce46edc98 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -215,8 +215,7 @@ const _toMatchSnapshot = ({ const toThrowErrorMatchingSnapshot = function( this: Context, received: any, - testName?: string, - // @ts-ignore + testName: string | undefined, fromPromise: boolean, ) { return _toThrowErrorMatchingSnapshot({ From 835974dbb4714bcd0ec4dc3bd0f4d50188853d1d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Feb 2019 17:49:26 +0500 Subject: [PATCH 14/35] Small change Co-Authored-By: doniyor2109 --- packages/jest-snapshot/src/__tests__/throw_matcher.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts index a8517e3e0a0a..f08c1b1490b8 100644 --- a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts @@ -8,7 +8,7 @@ 'use strict'; -const {toThrowErrorMatchingSnapshot} = require('../'); +import {toThrowErrorMatchingSnapshot} = from '../'; let matchFn: jest.Mock; From e6b14dc966c5bf93ac5e244c030923df51506510 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:14:35 +0500 Subject: [PATCH 15/35] Migrate tests to es modules --- packages/jest-snapshot/package.json | 2 +- .../src/__tests__/inline_snapshots.test.ts | 46 ++++++++++++------- .../src/__tests__/matcher.test.ts | 5 +- .../src/__tests__/mock_serializer.test.ts | 1 - .../src/__tests__/plugins.test.ts | 1 - .../src/__tests__/snapshot_resolver.test.ts | 17 ++++--- .../src/__tests__/throw_matcher.test.ts | 14 ++++-- .../jest-snapshot/src/__tests__/utils.test.ts | 26 +++++++---- packages/jest-snapshot/src/index.ts | 2 +- packages/jest-snapshot/tsconfig.json | 4 +- packages/pretty-format/package.json | 2 +- packages/pretty-format/src/types.ts | 11 ++++- yarn.lock | 5 ++ 13 files changed, 88 insertions(+), 48 deletions(-) diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 7f47d62ec39d..d660cb81c167 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -23,8 +23,8 @@ }, "devDependencies": { "@types/mkdirp": "^0.5.2", + "@types/prettier": "^1.16.1", "@types/semver": "^5.5.0", - "expect": "^24.1.0", "jest-haste-map": "^24.0.0", "prettier": "^1.13.4", "pretty-format": "^24.0.0" diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index 397a85e9b74b..da4a60c2cd98 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -10,12 +10,12 @@ jest.mock('fs'); jest.mock('prettier'); -const fs = require('fs'); -const path = require('path'); -const prettier = require('prettier'); -const babelTraverse = require('@babel/traverse').default; +import fs from 'fs'; +import path from 'path'; +import prettier from 'prettier'; +import babelTraverse from '@babel/traverse'; -const {saveInlineSnapshots} = require('../inline_snapshots'); +import {saveInlineSnapshots} from '../inline_snapshots'; const writeFileSync = fs.writeFileSync; const readFileSync = fs.readFileSync; @@ -26,12 +26,12 @@ beforeEach(() => { fs.writeFileSync = jest.fn(); fs.readFileSync = jest.fn(); fs.existsSync = jest.fn(() => true); - fs.statSync = jest.fn(filePath => ({ + (fs.statSync as jest.Mock).mockImplementation(filePath => ({ isDirectory: () => !filePath.endsWith('.js'), })); fs.readdirSync = jest.fn(() => []); - prettier.resolveConfig.sync.mockReset(); + (prettier.resolveConfig.sync as jest.Mock).mockReset(); }); afterEach(() => { fs.writeFileSync = writeFileSync; @@ -43,7 +43,9 @@ afterEach(() => { test('saveInlineSnapshots() replaces empty function call with a template literal', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn(() => `expect(1).toMatchInlineSnapshot();\n`); + (fs.readFileSync as jest.Mock).mockImplementation( + () => `expect(1).toMatchInlineSnapshot();\n`, + ); saveInlineSnapshots( [ @@ -66,9 +68,11 @@ test.each([['babylon'], ['flow'], ['typescript']])( 'saveInlineSnapshots() replaces existing template literal - %s parser', parser => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot(`2`);\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'expect(1).toMatchInlineSnapshot(`2`);\n', + ); - prettier.resolveConfig.sync.mockReturnValue({parser}); + (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({parser}); saveInlineSnapshots( [ @@ -81,7 +85,9 @@ test.each([['babylon'], ['flow'], ['typescript']])( babelTraverse, ); - expect(prettier.resolveConfig.sync.mock.results[0].value).toEqual({parser}); + expect( + (prettier.resolveConfig.sync as jest.Mock).mock.results[0].value, + ).toEqual({parser}); expect(fs.writeFileSync).toHaveBeenCalledWith( filename, @@ -92,7 +98,7 @@ test.each([['babylon'], ['flow'], ['typescript']])( test('saveInlineSnapshots() replaces existing template literal with property matchers', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn( + (fs.readFileSync as jest.Mock).mockImplementation( () => 'expect(1).toMatchInlineSnapshot({}, `2`);\n', ); @@ -115,7 +121,9 @@ test('saveInlineSnapshots() replaces existing template literal with property mat test('saveInlineSnapshots() throws if frame does not match', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'expect(1).toMatchInlineSnapshot();\n', + ); const save = () => saveInlineSnapshots( @@ -134,7 +142,9 @@ test('saveInlineSnapshots() throws if frame does not match', () => { test('saveInlineSnapshots() throws if multiple calls to to the same location', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'expect(1).toMatchInlineSnapshot();\n', + ); const frame = {column: 11, file: filename, line: 1}; const save = () => @@ -151,7 +161,9 @@ test('saveInlineSnapshots() throws if multiple calls to to the same location', ( test('saveInlineSnapshots() uses escaped backticks', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn(() => 'expect("`").toMatchInlineSnapshot();\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'expect("`").toMatchInlineSnapshot();\n', + ); const frame = {column: 13, file: filename, line: 1}; saveInlineSnapshots([{frame, snapshot: '`'}], prettier, babelTraverse); @@ -164,10 +176,10 @@ test('saveInlineSnapshots() uses escaped backticks', () => { test('saveInlineSnapshots() works with non-literals in expect call', () => { const filename = path.join(__dirname, 'my.test.js'); - fs.readFileSync = jest.fn( + (fs.readFileSync as jest.Mock).mockImplementation( () => `expect({a: 'a'}).toMatchInlineSnapshot();\n`, ); - prettier.resolveConfig.sync.mockReturnValue({ + (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({ bracketSpacing: false, singleQuote: true, }); diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.ts b/packages/jest-snapshot/src/__tests__/matcher.test.ts index 0bfe90e09fe8..30f67f0ce7f6 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -5,9 +5,10 @@ * LICENSE file in the root directory of this source tree. * */ -'use strict'; -const {toMatchSnapshot} = require('../'); +import jestSnapshot = require('../'); + +const {toMatchSnapshot} = jestSnapshot; it(`matcher returns matcher name, expected and actual values`, () => { const actual = 'a'; diff --git a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts index 702898f65a50..c4431a21b81e 100644 --- a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts +++ b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. * */ -'use strict'; import prettyFormat from 'pretty-format'; diff --git a/packages/jest-snapshot/src/__tests__/plugins.test.ts b/packages/jest-snapshot/src/__tests__/plugins.test.ts index 80e67a7bb743..f91f745cb54e 100644 --- a/packages/jest-snapshot/src/__tests__/plugins.test.ts +++ b/packages/jest-snapshot/src/__tests__/plugins.test.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. * */ -'use strict'; beforeEach(() => jest.resetModules()); diff --git a/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts b/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts index fd53291ef0ca..e7b93987ecaf 100644 --- a/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts +++ b/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts @@ -1,14 +1,17 @@ // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. -const path = require('path'); -const {buildSnapshotResolver} = require('../snapshot_resolver'); +import path from 'path'; +import {Config} from '@jest/types'; +import {SnapshotResolver} from '../types'; + +import {buildSnapshotResolver} from '../snapshot_resolver'; describe('defaults', () => { - let snapshotResolver; + let snapshotResolver: SnapshotResolver; const projectConfig = { rootDir: 'default', // snapshotResolver: null, - }; + } as Config.ProjectConfig; beforeEach(() => { snapshotResolver = buildSnapshotResolver(projectConfig); @@ -32,7 +35,7 @@ describe('defaults', () => { }); describe('custom resolver in project config', () => { - let snapshotResolver; + let snapshotResolver: SnapshotResolver; const customSnapshotResolverFile = path.join( __dirname, 'fixtures', @@ -41,7 +44,7 @@ describe('custom resolver in project config', () => { const projectConfig = { rootDir: 'custom1', snapshotResolver: customSnapshotResolverFile, - }; + } as Config.ProjectConfig; beforeEach(() => { snapshotResolver = buildSnapshotResolver(projectConfig); @@ -78,7 +81,7 @@ describe('malformed custom resolver in project config', () => { return { rootDir: 'missing-resolveSnapshotPath', snapshotResolver: customSnapshotResolverFile, - }; + } as Config.ProjectConfig; }; it('missing resolveSnapshotPath throws ', () => { diff --git a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts index f08c1b1490b8..ad8156506121 100644 --- a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts @@ -6,9 +6,9 @@ * */ -'use strict'; +import jestSnapshot from '../'; -import {toThrowErrorMatchingSnapshot} = from '../'; +const {toThrowErrorMatchingSnapshot} = jestSnapshot; let matchFn: jest.Mock; @@ -24,9 +24,13 @@ it('throw matcher can take func', () => { snapshotState: {match: matchFn}, }); - throwMatcher(() => { - throw new Error('coconut'); - }); + throwMatcher( + () => { + throw new Error('coconut'); + }, + undefined, + false, + ); expect(matchFn).toHaveBeenCalledWith( expect.objectContaining({received: 'coconut', testName: ''}), diff --git a/packages/jest-snapshot/src/__tests__/utils.test.ts b/packages/jest-snapshot/src/__tests__/utils.test.ts index e74fbc071ab7..20a169540c5b 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.ts +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -7,9 +7,9 @@ jest.mock('fs'); -const fs = require('fs'); -const path = require('path'); -const chalk = require('chalk'); +import fs from 'fs'; +import path from 'path'; +import chalk from 'chalk'; const { getSnapshotData, @@ -80,7 +80,9 @@ test('saveSnapshotFile() works with \r', () => { test('getSnapshotData() throws when no snapshot version', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn(() => 'exports[`myKey`] = `
\n
`;\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'exports[`myKey`] = `
\n
`;\n', + ); const update = 'none'; expect(() => getSnapshotData(filename, update)).toThrowError( @@ -95,7 +97,7 @@ test('getSnapshotData() throws when no snapshot version', () => { test('getSnapshotData() throws for older snapshot version', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn( + (fs.readFileSync as jest.Mock).mockImplementation( () => `// Jest Snapshot v0.99, ${SNAPSHOT_GUIDE_LINK}\n\n` + 'exports[`myKey`] = `
\n
`;\n', @@ -118,7 +120,7 @@ test('getSnapshotData() throws for older snapshot version', () => { test('getSnapshotData() throws for newer snapshot version', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn( + (fs.readFileSync as jest.Mock).mockImplementation( () => `// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}\n\n` + 'exports[`myKey`] = `
\n
`;\n', @@ -141,7 +143,9 @@ test('getSnapshotData() throws for newer snapshot version', () => { test('getSnapshotData() does not throw for when updating', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn(() => 'exports[`myKey`] = `
\n
`;\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'exports[`myKey`] = `
\n
`;\n', + ); const update = 'all'; expect(() => getSnapshotData(filename, update)).not.toThrow(); @@ -149,7 +153,9 @@ test('getSnapshotData() does not throw for when updating', () => { test('getSnapshotData() marks invalid snapshot dirty when updating', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn(() => 'exports[`myKey`] = `
\n
`;\n'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => 'exports[`myKey`] = `
\n
`;\n', + ); const update = 'all'; expect(getSnapshotData(filename, update)).toMatchObject({dirty: true}); @@ -157,7 +163,7 @@ test('getSnapshotData() marks invalid snapshot dirty when updating', () => { test('getSnapshotData() marks valid snapshot not dirty when updating', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); - fs.readFileSync = jest.fn( + (fs.readFileSync as jest.Mock).mockImplementation( () => `// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}\n\n` + 'exports[`myKey`] = `
\n
`;\n', @@ -171,7 +177,7 @@ test('escaping', () => { const filename = path.join(__dirname, 'escaping.snap'); const data = '"\'\\'; saveSnapshotFile({key: data}, filename); - const writtenData = fs.writeFileSync.mock.calls[0][1]; + const writtenData = (fs.writeFileSync as jest.Mock).mock.calls[0][1]; expect(writtenData).toBe( `// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` + 'exports[`key`] = `"\'\\\\`;\n', diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 97cce46edc98..04e7fb35b723 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -295,7 +295,7 @@ const _toThrowErrorMatchingSnapshot = ({ }); }; -module.exports = { +export = { EXTENSION, SnapshotState, addSerializer, diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index 8dbd51cf07fd..72c436a484e5 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -2,7 +2,9 @@ "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": "src", - "outDir": "build" + "outDir": "build", + "importHelpers": true, + "allowSyntheticDefaultImports": true }, "references": [ { diff --git a/packages/pretty-format/package.json b/packages/pretty-format/package.json index a1d531fed9ec..19e48d61bc92 100644 --- a/packages/pretty-format/package.json +++ b/packages/pretty-format/package.json @@ -17,11 +17,11 @@ "ansi-styles": "^3.2.0" }, "devDependencies": { + "@jest/types": "^24.1.0", "@types/ansi-regex": "^4.0.0", "@types/ansi-styles": "^3.2.1", "@types/react": "*", "@types/react-test-renderer": "*", - "@jest/types": "^24.1.0", "immutable": "4.0.0-rc.9", "react": "*", "react-dom": "*", diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index c2586b1b96f9..71f14783ed5f 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -7,4 +7,13 @@ import {PrettyFormat} from '@jest/types'; -export = PrettyFormat; +export type Colors = PrettyFormat.Colors; +export type Config = PrettyFormat.Config; +export type Options = PrettyFormat.Options; +export type OptionsReceived = PrettyFormat.OptionsReceived; +export type NewPlugin = PrettyFormat.NewPlugin; +export type Plugin = PrettyFormat.Plugin; +export type Plugins = PrettyFormat.Plugins; +export type Refs = PrettyFormat.Refs; +export type Theme = PrettyFormat.Theme; +export type Printer = PrettyFormat.Printer; diff --git a/yarn.lock b/yarn.lock index 111752599f80..d4654928afa9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1693,6 +1693,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.24.tgz#b13564af612a22a20b5d95ca40f1bffb3af315cf" integrity sha512-GWWbvt+z9G5otRBW8rssOFgRY87J9N/qbhqfjMZ+gUuL6zoL+Hm6gP/8qQBG4jjimqdaNLCehcVapZ/Fs2WjCQ== +"@types/prettier@^1.16.1": + version "1.16.1" + resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.16.1.tgz#328d1c9b54402e44119398bcb6a31b7bbd606d59" + integrity sha512-db6pZL5QY3JrlCHBhYQzYDci0xnoDuxfseUuguLRr3JNk+bnCfpkK6p8quiUDyO8A0vbpBKkk59Fw125etrNeA== + "@types/prompts@^1.2.0": version "1.2.0" resolved "https://registry.yarnpkg.com/@types/prompts/-/prompts-1.2.0.tgz#891e73f735ad5e82e8adae3a99424128e105fb62" From d0f37890cb3d3d7fcfbfdf1f21a4cb55a49b4832 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:17:05 +0500 Subject: [PATCH 16/35] Small tweak --- packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index da4a60c2cd98..40740901c1c6 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * */ jest.mock('fs'); From 81cec4aadec4e54f5088a6fab01ac80d64f41685 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:19:45 +0500 Subject: [PATCH 17/35] Eslint fixes --- packages/pretty-format/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 71f14783ed5f..3c224adfa280 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {PrettyFormat} from '@jest/types'; +import {PrettyFormat} from '@jest/types'; // eslint-disable-line import/no-extraneous-dependencies export type Colors = PrettyFormat.Colors; export type Config = PrettyFormat.Config; From 9c16d91c38e9ce2340326553a465966689940208 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:24:53 +0500 Subject: [PATCH 18/35] Fix `import =` is not supported in test --- packages/jest-snapshot/src/__tests__/matcher.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.ts b/packages/jest-snapshot/src/__tests__/matcher.test.ts index 30f67f0ce7f6..fdb8195f6b0a 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -6,7 +6,7 @@ * */ -import jestSnapshot = require('../'); +const jestSnapshot = require('../'); const {toMatchSnapshot} = jestSnapshot; From 36d32f392083d9cae612c710baf94a0a32e0dd0f Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:36:31 +0500 Subject: [PATCH 19/35] Remove extra dep --- packages/jest-snapshot/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index d660cb81c167..188a31f423b0 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -26,8 +26,7 @@ "@types/prettier": "^1.16.1", "@types/semver": "^5.5.0", "jest-haste-map": "^24.0.0", - "prettier": "^1.13.4", - "pretty-format": "^24.0.0" + "prettier": "^1.13.4" }, "engines": { "node": ">= 6" From 1fb31949bad4d9c2f97a0bc246a709b1fa84788c Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 20:56:55 +0500 Subject: [PATCH 20/35] Add Frame type to jest-message-util --- packages/jest-message-util/src/index.ts | 7 +++++-- packages/jest-message-util/src/types.ts | 5 +++++ packages/jest-snapshot/src/State.ts | 6 +----- packages/jest-snapshot/src/inline_snapshots.ts | 3 ++- packages/jest-snapshot/tsconfig.json | 3 +++ 5 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 packages/jest-message-util/src/types.ts diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index 4e1b969f5d23..eea3cebe83f8 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -13,6 +13,9 @@ import micromatch from 'micromatch'; import slash from 'slash'; import {codeFrameColumns} from '@babel/code-frame'; import StackUtils from 'stack-utils'; +import {Frame} from './types'; + +export * from './types'; type Path = Config.Path; type AssertionResult = TestResult.AssertionResult; @@ -227,7 +230,7 @@ export const getStackTraceLines = ( options: StackTraceOptions = {noStackTrace: false}, ) => removeInternalStackEntries(stack.split(/\n/), options); -export const getTopFrame = (lines: string[]) => { +export const getTopFrame = (lines: string[]): Frame | null => { for (const line of lines) { if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_JEST_PACKAGES)) { continue; @@ -236,7 +239,7 @@ export const getTopFrame = (lines: string[]) => { const parsedFrame = stackUtils.parseLine(line.trim()); if (parsedFrame && parsedFrame.file) { - return parsedFrame; + return parsedFrame as Frame; } } diff --git a/packages/jest-message-util/src/types.ts b/packages/jest-message-util/src/types.ts new file mode 100644 index 000000000000..6903f1792ccc --- /dev/null +++ b/packages/jest-message-util/src/types.ts @@ -0,0 +1,5 @@ +import {StackData} from 'stack-utils'; + +export interface Frame extends StackData { + file: string; +} diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 77eaf1972bbc..3b59452e12f3 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -101,11 +101,7 @@ export default class SnapshotState { ); } this._inlineSnapshots.push({ - frame: { - column: frame.column, - file: frame.file as string, - line: frame.line, - }, + frame, snapshot: receivedSerialized, }); } else { diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index 0d04a07f8153..f78d87c90dbe 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -15,13 +15,14 @@ import { file, CallExpression, } from '@babel/types'; +import {Frame} from 'jest-message-util'; import {Config} from '@jest/types'; import {escapeBacktickString} from './utils'; export type InlineSnapshot = { snapshot: string; - frame: {line?: number; column?: number; file: string}; + frame: Frame; }; export const saveInlineSnapshots = ( diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index 72c436a484e5..076156dd6bc1 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -15,6 +15,9 @@ }, { "path": "../pretty-format" + }, + { + "path": "../jest-message-util" } ] } From 84ae5ca62e7c71e80b6a1eb9f5a8ca8f93c493a2 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 21:14:02 +0500 Subject: [PATCH 21/35] Minor tweaks --- packages/jest-snapshot/tsconfig.json | 9 +++++++++ packages/pretty-format/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index 076156dd6bc1..f990e711c5d6 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -18,6 +18,15 @@ }, { "path": "../jest-message-util" + }, + { + "path": "../jest-diff" + }, + { + "path": "../jest-matcher-utils" + }, + { + "path": "../jest-resolve" } ] } diff --git a/packages/pretty-format/package.json b/packages/pretty-format/package.json index 19e48d61bc92..ef976df12eef 100644 --- a/packages/pretty-format/package.json +++ b/packages/pretty-format/package.json @@ -13,11 +13,11 @@ "browser": "build-es5/index.js", "author": "James Kyle ", "dependencies": { + "@jest/types": "^24.1.0", "ansi-regex": "^4.0.0", "ansi-styles": "^3.2.0" }, "devDependencies": { - "@jest/types": "^24.1.0", "@types/ansi-regex": "^4.0.0", "@types/ansi-styles": "^3.2.1", "@types/react": "*", From 4a14bc4ea40136c003b917915b8df88ea7ae0d34 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Feb 2019 21:34:06 +0500 Subject: [PATCH 22/35] Better types Co-Authored-By: doniyor2109 --- packages/jest-snapshot/src/plugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/plugins.ts b/packages/jest-snapshot/src/plugins.ts index f02664ec83ad..4beac35daaec 100644 --- a/packages/jest-snapshot/src/plugins.ts +++ b/packages/jest-snapshot/src/plugins.ts @@ -21,7 +21,7 @@ const { AsymmetricMatcher, } = prettyFormat.plugins; -let PLUGINS: Array = [ +let PLUGINS: PrettyFormat.Plugins = [ ReactTestComponent, ReactElement, DOMElement, From caf22680dac5ee48bf937d28d8398f1d06bdfcb8 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 21:38:36 +0500 Subject: [PATCH 23/35] Minor tweaks --- packages/jest-snapshot/dts/natural-compare.d.ts | 1 + packages/jest-snapshot/package.json | 3 +++ packages/jest-snapshot/src/index.ts | 2 +- packages/jest-snapshot/src/snapshot_resolver.ts | 3 +-- packages/jest-snapshot/src/types.ts | 8 +++++++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/jest-snapshot/dts/natural-compare.d.ts b/packages/jest-snapshot/dts/natural-compare.d.ts index 46f9107a7674..98b7ab4260a5 100644 --- a/packages/jest-snapshot/dts/natural-compare.d.ts +++ b/packages/jest-snapshot/dts/natural-compare.d.ts @@ -1,3 +1,4 @@ +// TODO Replace with @types/natural-compare when it is merged https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33084 declare module 'natural-compare' { const NaturalCompare: (a: string, b: string) => number; export default NaturalCompare; diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 188a31f423b0..2e7cd024f5bf 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -21,6 +21,9 @@ "pretty-format": "^24.0.0", "semver": "^5.5.0" }, + "peerDependencies": { + "jest-haste-map": "^24.0.0" + }, "devDependencies": { "@types/mkdirp": "^0.5.2", "@types/prettier": "^1.16.1", diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 04e7fb35b723..761a2f8236eb 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -8,7 +8,7 @@ import fs from 'fs'; import {Config, MatcherState} from '@jest/types'; -import {FS as HasteFS} from 'jest-haste-map'; // eslint-disable-line import/no-extraneous-dependencies +import {FS as HasteFS} from 'jest-haste-map'; import diff from 'jest-diff'; import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; diff --git a/packages/jest-snapshot/src/snapshot_resolver.ts b/packages/jest-snapshot/src/snapshot_resolver.ts index 28ac08ae3874..3c379ae735cf 100644 --- a/packages/jest-snapshot/src/snapshot_resolver.ts +++ b/packages/jest-snapshot/src/snapshot_resolver.ts @@ -20,8 +20,7 @@ export const buildSnapshotResolver = ( if (!cache.has(key)) { cache.set(key, createSnapshotResolver(config.snapshotResolver)); } - // @ts-ignore - return cache.get(key); + return cache.get(key)!; }; function createSnapshotResolver( diff --git a/packages/jest-snapshot/src/types.ts b/packages/jest-snapshot/src/types.ts index 7f5b8a4d2bcc..9e511d26aaed 100644 --- a/packages/jest-snapshot/src/types.ts +++ b/packages/jest-snapshot/src/types.ts @@ -1,4 +1,10 @@ -// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. +/** + * 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 {Config} from '@jest/types'; From 120243489c2ffbe5ec3174269d5ac2d4f347213e Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 21:41:02 +0500 Subject: [PATCH 24/35] Fix Frame types in tests --- .../src/__tests__/inline_snapshots.test.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index 40740901c1c6..a122a5f2eaa5 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -13,6 +13,7 @@ import fs from 'fs'; import path from 'path'; import prettier from 'prettier'; import babelTraverse from '@babel/traverse'; +import {Frame} from 'jest-message-util'; import {saveInlineSnapshots} from '../inline_snapshots'; @@ -49,7 +50,7 @@ test('saveInlineSnapshots() replaces empty function call with a template literal saveInlineSnapshots( [ { - frame: {column: 11, file: filename, line: 1}, + frame: {column: 11, file: filename, line: 1} as Frame, snapshot: `1`, }, ], @@ -76,7 +77,7 @@ test.each([['babylon'], ['flow'], ['typescript']])( saveInlineSnapshots( [ { - frame: {column: 11, file: filename, line: 1}, + frame: {column: 11, file: filename, line: 1} as Frame, snapshot: `1`, }, ], @@ -104,7 +105,7 @@ test('saveInlineSnapshots() replaces existing template literal with property mat saveInlineSnapshots( [ { - frame: {column: 11, file: filename, line: 1}, + frame: {column: 11, file: filename, line: 1} as Frame, snapshot: `1`, }, ], @@ -128,7 +129,7 @@ test('saveInlineSnapshots() throws if frame does not match', () => { saveInlineSnapshots( [ { - frame: {column: 2 /* incorrect */, file: filename, line: 1}, + frame: {column: 2 /* incorrect */, file: filename, line: 1} as Frame, snapshot: `1`, }, ], @@ -145,7 +146,7 @@ test('saveInlineSnapshots() throws if multiple calls to to the same location', ( () => 'expect(1).toMatchInlineSnapshot();\n', ); - const frame = {column: 11, file: filename, line: 1}; + const frame = {column: 11, file: filename, line: 1} as Frame; const save = () => saveInlineSnapshots( [{frame, snapshot: `1`}, {frame, snapshot: `2`}], @@ -164,7 +165,7 @@ test('saveInlineSnapshots() uses escaped backticks', () => { () => 'expect("`").toMatchInlineSnapshot();\n', ); - const frame = {column: 13, file: filename, line: 1}; + const frame = {column: 13, file: filename, line: 1} as Frame; saveInlineSnapshots([{frame, snapshot: '`'}], prettier, babelTraverse); expect(fs.writeFileSync).toHaveBeenCalledWith( @@ -186,7 +187,7 @@ test('saveInlineSnapshots() works with non-literals in expect call', () => { saveInlineSnapshots( [ { - frame: {column: 18, file: filename, line: 1}, + frame: {column: 18, file: filename, line: 1} as Frame, snapshot: `{a: 'a'}`, }, ], From cc59272456d4c550b9b6359a5a96ef85cc95f11b Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 21:42:45 +0500 Subject: [PATCH 25/35] Resolve unused vars --- packages/jest-snapshot/src/__tests__/matcher.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.ts b/packages/jest-snapshot/src/__tests__/matcher.test.ts index fdb8195f6b0a..d89a9ae0b7a0 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -15,8 +15,7 @@ it(`matcher returns matcher name, expected and actual values`, () => { const expected = 'b'; const matcher = toMatchSnapshot.bind({ snapshotState: { - // @ts-ignore - match: (testName: string, received: any) => ({actual, expected}), // eslint-disable-line + match: (_testName: string, _received: any) => ({actual, expected}), }, }); From 3c906c3daa06611fb03634bb5202d9f2f9eab99d Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Fri, 15 Feb 2019 21:46:12 +0500 Subject: [PATCH 26/35] es imports --- packages/jest-snapshot/src/__tests__/matcher.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.ts b/packages/jest-snapshot/src/__tests__/matcher.test.ts index d89a9ae0b7a0..e670aa09e3f4 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -6,7 +6,7 @@ * */ -const jestSnapshot = require('../'); +import jestSnapshot from '../'; const {toMatchSnapshot} = jestSnapshot; From 9bd4eb068ca2ef3c0137b49a2fce580db5517e94 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Sat, 16 Feb 2019 11:56:09 +0500 Subject: [PATCH 27/35] Added package.json types --- packages/jest-snapshot/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 2e7cd024f5bf..1d888e644ad8 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -8,6 +8,7 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { "@babel/types": "^7.0.0", "@jest/types": "^24.1.0", From 3de88c4ce8f4ecca4b68a577f2e7d65c6069dd55 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Sat, 16 Feb 2019 11:59:38 +0500 Subject: [PATCH 28/35] Added @types/natural-comapre --- packages/jest-snapshot/dts/natural-compare.d.ts | 5 ----- packages/jest-snapshot/package.json | 1 + yarn.lock | 5 +++++ 3 files changed, 6 insertions(+), 5 deletions(-) delete mode 100644 packages/jest-snapshot/dts/natural-compare.d.ts diff --git a/packages/jest-snapshot/dts/natural-compare.d.ts b/packages/jest-snapshot/dts/natural-compare.d.ts deleted file mode 100644 index 98b7ab4260a5..000000000000 --- a/packages/jest-snapshot/dts/natural-compare.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -// TODO Replace with @types/natural-compare when it is merged https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33084 -declare module 'natural-compare' { - const NaturalCompare: (a: string, b: string) => number; - export default NaturalCompare; -} diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 1d888e644ad8..ace12765991e 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -12,6 +12,7 @@ "dependencies": { "@babel/types": "^7.0.0", "@jest/types": "^24.1.0", + "@types/natural-compare": "^1.4.0", "chalk": "^2.0.1", "jest-diff": "^24.0.0", "jest-matcher-utils": "^24.0.0", diff --git a/yarn.lock b/yarn.lock index d4654928afa9..ad4ee6feafe1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1681,6 +1681,11 @@ dependencies: "@types/node" "*" +"@types/natural-compare@^1.4.0": + version "1.4.0" + resolved "https://registry.yarnpkg.com/@types/natural-compare/-/natural-compare-1.4.0.tgz#b3c54f7edc339758d573c5dcac7808c58cf8c31e" + integrity sha512-bNtBj6AF1F90jp54KRPOrYfilGNfPr2kpaUN7rMJjauAtfGBXzT/T/REZN6jb4qUs9FTxU37kir3Nrn5WsTUDw== + "@types/node-notifier@^0.0.28": version "0.0.28" resolved "https://registry.yarnpkg.com/@types/node-notifier/-/node-notifier-0.0.28.tgz#86ba3d3aa8d918352cc3191d88de328b20dc93c1" From 186194e13a4ac7a4c6b30dbbd39e3d4c0253092a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:05:06 +0100 Subject: [PATCH 29/35] fix type import --- packages/jest-snapshot/package.json | 2 +- packages/jest-snapshot/src/index.ts | 7 +++-- packages/jest-snapshot/tsconfig.json | 32 ++++++----------------- packages/jest-types/src/Matchers.ts | 39 ++++++++++++++++++++++++++++ packages/jest-types/src/index.ts | 2 ++ 5 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 packages/jest-types/src/Matchers.ts diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index ace12765991e..2f42a9bbc86b 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -12,7 +12,6 @@ "dependencies": { "@babel/types": "^7.0.0", "@jest/types": "^24.1.0", - "@types/natural-compare": "^1.4.0", "chalk": "^2.0.1", "jest-diff": "^24.0.0", "jest-matcher-utils": "^24.0.0", @@ -28,6 +27,7 @@ }, "devDependencies": { "@types/mkdirp": "^0.5.2", + "@types/natural-compare": "^1.4.0", "@types/prettier": "^1.16.1", "@types/semver": "^5.5.0", "jest-haste-map": "^24.0.0", diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 761a2f8236eb..5f92cd661d43 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -7,7 +7,7 @@ */ import fs from 'fs'; -import {Config, MatcherState} from '@jest/types'; +import {Config, Matchers} from '@jest/types'; import {FS as HasteFS} from 'jest-haste-map'; import diff from 'jest-diff'; @@ -22,7 +22,10 @@ import SnapshotState from './State'; import {addSerializer, getSerializers} from './plugins'; import * as utils from './utils'; -type Context = MatcherState & {dontThrow?: () => any}; +type Context = Matchers.MatcherState & { + snapshotState: SnapshotState; + dontThrow?: () => any; +}; const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean => hasteFS.exists(filePath) || fs.existsSync(filePath); diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index f990e711c5d6..2ed3fb0d58dc 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -2,31 +2,15 @@ "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": "src", - "outDir": "build", - "importHelpers": true, - "allowSyntheticDefaultImports": true + "outDir": "build" }, "references": [ - { - "path": "../jest-types" - }, - { - "path": "../jest-haste-map" - }, - { - "path": "../pretty-format" - }, - { - "path": "../jest-message-util" - }, - { - "path": "../jest-diff" - }, - { - "path": "../jest-matcher-utils" - }, - { - "path": "../jest-resolve" - } + {"path": "../jest-diff"}, + {"path": "../jest-haste-map"}, + {"path": "../jest-matcher-utils"}, + {"path": "../jest-message-util"}, + {"path": "../jest-resolve"}, + {"path": "../jest-types"}, + {"path": "../pretty-format"} ] } diff --git a/packages/jest-types/src/Matchers.ts b/packages/jest-types/src/Matchers.ts new file mode 100644 index 000000000000..82314de44b01 --- /dev/null +++ b/packages/jest-types/src/Matchers.ts @@ -0,0 +1,39 @@ +/** + * 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. + */ + +// TODO: Move this to `expect` when it's migrated + +import {Path} from './Config'; + +type Tester = (a: any, b: any) => boolean | undefined; + +export type MatcherState = { + assertionCalls: number; + currentTestName?: string; + error?: Error; + equals: ( + a: unknown, + b: unknown, + customTesters?: Array, + strictCheck?: boolean, + ) => boolean; + expand?: boolean; + expectedAssertionsNumber?: number; + isExpectingAssertions?: boolean; + isNot: boolean; + promise: string; + suppressedErrors: Array; + testPath?: Path; + // This is output from `jest-matcher-utils` plus iterableEquality, subsetEquality + // Type it correctly when moving it to `expect` + utils: { + printExpected: (value: unknown) => string; + printReceived: (value: unknown) => string; + iterableEquality: Tester; + subsetEquality: Tester; + }; +}; diff --git a/packages/jest-types/src/index.ts b/packages/jest-types/src/index.ts index 5655021bc624..5f72f2e75feb 100644 --- a/packages/jest-types/src/index.ts +++ b/packages/jest-types/src/index.ts @@ -12,6 +12,7 @@ import * as TestResult from './TestResult'; import * as Mocks from './Mocks'; import * as Transform from './Transform'; import * as PrettyFormat from './PrettyFormat'; +import * as Matchers from './Matchers'; export { Config, @@ -21,4 +22,5 @@ export { Mocks, Transform, PrettyFormat, + Matchers, }; From 9288a1d0fa4c3142f980a51974afac7c5816036d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:08:56 +0100 Subject: [PATCH 30/35] remove unused eslint supression comment --- packages/pretty-format/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 3c224adfa280..71f14783ed5f 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {PrettyFormat} from '@jest/types'; // eslint-disable-line import/no-extraneous-dependencies +import {PrettyFormat} from '@jest/types'; export type Colors = PrettyFormat.Colors; export type Config = PrettyFormat.Config; From 19aa286fad954e23c38bf91d7bf9abd89d6b87e7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:13:34 +0100 Subject: [PATCH 31/35] chore: add missing copyright header --- packages/jest-message-util/src/types.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/jest-message-util/src/types.ts b/packages/jest-message-util/src/types.ts index 6903f1792ccc..d8c9530b9b35 100644 --- a/packages/jest-message-util/src/types.ts +++ b/packages/jest-message-util/src/types.ts @@ -1,3 +1,10 @@ +/** + * 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 {StackData} from 'stack-utils'; export interface Frame extends StackData { From d9e931496e251dae12cd5db21cfdc98259ed6bac Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:35:02 +0100 Subject: [PATCH 32/35] tweak some types --- packages/jest-snapshot/src/State.ts | 1 - .../src/__tests__/inline_snapshots.test.ts | 1 - .../src/__tests__/matcher.test.ts | 1 - .../src/__tests__/mock_serializer.test.ts | 1 - .../src/__tests__/plugins.test.ts | 1 - .../src/__tests__/snapshot_resolver.test.ts | 3 +-- .../src/__tests__/throw_matcher.test.ts | 1 - .../jest-snapshot/src/__tests__/utils.test.ts | 6 +++--- packages/jest-snapshot/src/index.ts | 4 +--- .../jest-snapshot/src/inline_snapshots.ts | 1 - packages/jest-snapshot/src/mock_serializer.ts | 19 +++++++++---------- packages/jest-snapshot/src/plugins.ts | 2 -- .../jest-snapshot/src/snapshot_resolver.ts | 13 +++++++++++-- packages/jest-snapshot/src/types.ts | 9 --------- packages/jest-snapshot/src/utils.ts | 1 - packages/jest-types/src/Matchers.ts | 1 + 16 files changed, 26 insertions(+), 39 deletions(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 3b59452e12f3..75f666f6840b 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import fs from 'fs'; diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index a122a5f2eaa5..a7b7ad418eeb 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ jest.mock('fs'); diff --git a/packages/jest-snapshot/src/__tests__/matcher.test.ts b/packages/jest-snapshot/src/__tests__/matcher.test.ts index e670aa09e3f4..5bbfe1226577 100644 --- a/packages/jest-snapshot/src/__tests__/matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/matcher.test.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import jestSnapshot from '../'; diff --git a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts index c4431a21b81e..b859a9671a30 100644 --- a/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts +++ b/packages/jest-snapshot/src/__tests__/mock_serializer.test.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import prettyFormat from 'pretty-format'; diff --git a/packages/jest-snapshot/src/__tests__/plugins.test.ts b/packages/jest-snapshot/src/__tests__/plugins.test.ts index f91f745cb54e..feb47a2ea591 100644 --- a/packages/jest-snapshot/src/__tests__/plugins.test.ts +++ b/packages/jest-snapshot/src/__tests__/plugins.test.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ beforeEach(() => jest.resetModules()); diff --git a/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts b/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts index e7b93987ecaf..5afee9699182 100644 --- a/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts +++ b/packages/jest-snapshot/src/__tests__/snapshot_resolver.test.ts @@ -2,9 +2,8 @@ import path from 'path'; import {Config} from '@jest/types'; -import {SnapshotResolver} from '../types'; -import {buildSnapshotResolver} from '../snapshot_resolver'; +import {buildSnapshotResolver, SnapshotResolver} from '../snapshot_resolver'; describe('defaults', () => { let snapshotResolver: SnapshotResolver; diff --git a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts index ad8156506121..bbe8b9d477db 100644 --- a/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts +++ b/packages/jest-snapshot/src/__tests__/throw_matcher.test.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import jestSnapshot from '../'; diff --git a/packages/jest-snapshot/src/__tests__/utils.test.ts b/packages/jest-snapshot/src/__tests__/utils.test.ts index 20a169540c5b..981070691777 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.ts +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -11,17 +11,17 @@ import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; -const { +import { + deepMerge, getSnapshotData, keyToTestName, saveSnapshotFile, serialize, testNameToKey, - deepMerge, SNAPSHOT_GUIDE_LINK, SNAPSHOT_VERSION, SNAPSHOT_VERSION_WARNING, -} = require('../utils'); +} from '../utils'; const writeFileSync = fs.writeFileSync; const readFileSync = fs.readFileSync; diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 5f92cd661d43..bc22a7e4b9d9 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import fs from 'fs'; @@ -12,10 +11,10 @@ import {FS as HasteFS} from 'jest-haste-map'; import diff from 'jest-diff'; import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; -import {SnapshotResolver} from './types'; import { buildSnapshotResolver, isSnapshotPath, + SnapshotResolver, EXTENSION, } from './snapshot_resolver'; import SnapshotState from './State'; @@ -24,7 +23,6 @@ import * as utils from './utils'; type Context = Matchers.MatcherState & { snapshotState: SnapshotState; - dontThrow?: () => any; }; const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean => diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index f78d87c90dbe..9468c1c4ec43 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import fs from 'fs'; diff --git a/packages/jest-snapshot/src/mock_serializer.ts b/packages/jest-snapshot/src/mock_serializer.ts index 9ad41676245a..fbfaf7a1448c 100644 --- a/packages/jest-snapshot/src/mock_serializer.ts +++ b/packages/jest-snapshot/src/mock_serializer.ts @@ -3,19 +3,17 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * */ import {PrettyFormat} from '@jest/types'; -export const serialize = ( - val: any, - config: PrettyFormat.Config, - indentation: string, - depth: number, - refs: PrettyFormat.Refs, - printer: PrettyFormat.Printer, +export const serialize: PrettyFormat.NewPlugin['serialize'] = ( + val, + config, + indentation, + depth, + refs, + printer, ): string => { // Serialize a non-default name, even if config.printFunctionName is false. const name = val.getMockName(); @@ -44,7 +42,8 @@ export const serialize = ( return '[MockFunction' + nameString + ']' + callsString; }; -export const test = (val: any) => val && !!val._isMockFunction; +export const test: PrettyFormat.NewPlugin['test'] = val => + val && !!val._isMockFunction; const plugin: PrettyFormat.NewPlugin = {serialize, test}; diff --git a/packages/jest-snapshot/src/plugins.ts b/packages/jest-snapshot/src/plugins.ts index 4beac35daaec..ebf50c0eb651 100644 --- a/packages/jest-snapshot/src/plugins.ts +++ b/packages/jest-snapshot/src/plugins.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. - * - * */ import prettyFormat from 'pretty-format'; diff --git a/packages/jest-snapshot/src/snapshot_resolver.ts b/packages/jest-snapshot/src/snapshot_resolver.ts index 3c379ae735cf..824fb5307dfa 100644 --- a/packages/jest-snapshot/src/snapshot_resolver.ts +++ b/packages/jest-snapshot/src/snapshot_resolver.ts @@ -1,10 +1,19 @@ -// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. +/** + * 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 path from 'path'; import {Config} from '@jest/types'; import chalk from 'chalk'; -import {SnapshotResolver} from './types'; +export type SnapshotResolver = { + testPathForConsistencyCheck: string; + resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path; + resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path; +}; export const EXTENSION = 'snap'; export const DOT_EXTENSION = '.' + EXTENSION; diff --git a/packages/jest-snapshot/src/types.ts b/packages/jest-snapshot/src/types.ts index 9e511d26aaed..439a6d013ed1 100644 --- a/packages/jest-snapshot/src/types.ts +++ b/packages/jest-snapshot/src/types.ts @@ -3,15 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ -import {Config} from '@jest/types'; - -export type SnapshotResolver = { - testPathForConsistencyCheck: string; - resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path; - resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path; -}; - export type SnapshotData = {[key: string]: string}; diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index b56d683a349e..992fa72926d0 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -3,7 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * */ import fs from 'fs'; diff --git a/packages/jest-types/src/Matchers.ts b/packages/jest-types/src/Matchers.ts index 82314de44b01..e4e419e0ae23 100644 --- a/packages/jest-types/src/Matchers.ts +++ b/packages/jest-types/src/Matchers.ts @@ -14,6 +14,7 @@ type Tester = (a: any, b: any) => boolean | undefined; export type MatcherState = { assertionCalls: number; currentTestName?: string; + dontThrow?: () => void; error?: Error; equals: ( a: unknown, From 7590af99aad00145846178dc60a17c65a295540c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:42:35 +0100 Subject: [PATCH 33/35] explicit export --- packages/jest-message-util/src/index.ts | 2 +- packages/jest-snapshot/src/State.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index eea3cebe83f8..f1c6ad699215 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -15,7 +15,7 @@ import {codeFrameColumns} from '@babel/code-frame'; import StackUtils from 'stack-utils'; import {Frame} from './types'; -export * from './types'; +export {Frame} from './types'; type Path = Config.Path; type AssertionResult = TestResult.AssertionResult; diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 75f666f6840b..c6fd06953b73 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -252,7 +252,7 @@ export default class SnapshotState { } } - fail(testName: string, _: any, key?: string) { + fail(testName: string, _received: any, key?: string) { this._counters.set(testName, (this._counters.get(testName) || 0) + 1); const count = Number(this._counters.get(testName)); From b915c81edd8e0a61af26abd36652ca2b87c9ca54 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:50:55 +0100 Subject: [PATCH 34/35] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bfdfa66e605..88cc0354d2c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - `[babel-jest]`: Migrate to TypeScript ([#7862](https://github.com/facebook/jest/pull/7862)) - `[jest-resolve]`: Migrate to TypeScript ([#7871](https://github.com/facebook/jest/pull/7871)) - `[@jest/reporter]`: New package extracted from `jest-cli` ([#7902](https://github.com/facebook/jest/pull/7902)) +- `[jest-snapshot]`: Migrate to TypeScript ([#7899](https://github.com/facebook/jest/pull/7899)) ### Performance From 2a8795eb6d0f8930cb687e1fd0e21ff9f3d71721 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 16 Feb 2019 12:58:33 +0100 Subject: [PATCH 35/35] prettify tsconfig --- packages/pretty-format/tsconfig.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/pretty-format/tsconfig.json b/packages/pretty-format/tsconfig.json index 67f35375a868..3046cb6b9b6a 100644 --- a/packages/pretty-format/tsconfig.json +++ b/packages/pretty-format/tsconfig.json @@ -4,8 +4,5 @@ "rootDir": "src", "outDir": "build" }, - "references": [{ - "path": "../jest-types" - } - ] + "references": [{"path": "../jest-types"}] }