From b17a65a276cf6f7e3d8e4be1a4b2bc6149735c14 Mon Sep 17 00:00:00 2001 From: cpojer Date: Fri, 28 Apr 2017 01:52:01 +0100 Subject: [PATCH 1/2] Fix snapshot serializer require, restructure pretty-format. --- .flowconfig | 1 + .../__snapshots__/failures-test.js.snap | 4 ++-- packages/jest-diff/src/index.js | 22 ++++++++++--------- packages/jest-jasmine2/src/index.js | 1 + .../jest-jasmine2/src/setup-jest-globals.js | 12 +++++++--- packages/jest-matcher-utils/src/index.js | 20 ++++++++--------- packages/jest-snapshot/src/plugins.js | 20 ++++++++--------- packages/pretty-format/README.md | 4 ++-- packages/pretty-format/src/index.js | 17 +++++++++++++- .../src/plugins/AsymmetricMatcher.js | 2 +- .../pretty-format/src/plugins/ConvertAnsi.js | 2 +- .../pretty-format/src/plugins/HTMLElement.js | 2 +- .../src/plugins/ImmutableList.js | 2 +- .../pretty-format/src/plugins/ImmutableMap.js | 2 +- .../src/plugins/ImmutableOrderedMap.js | 2 +- .../src/plugins/ImmutableOrderedSet.js | 2 +- .../pretty-format/src/plugins/ImmutableSet.js | 2 +- .../src/plugins/ImmutableStack.js | 2 +- .../src/plugins/ReactTestComponent.js | 2 +- .../src/plugins/lib/printImmutable.js | 2 +- .../src/types.js => types/PrettyFormat.js | 0 21 files changed, 74 insertions(+), 49 deletions(-) rename packages/pretty-format/src/types.js => types/PrettyFormat.js (100%) diff --git a/.flowconfig b/.flowconfig index 95a31507963b..1c9a4a8d2fa1 100644 --- a/.flowconfig +++ b/.flowconfig @@ -7,6 +7,7 @@ .*/dangerfile.js [options] +module.name_mapper='^pretty-format$' -> '/packages/pretty-format/src/index.js' module.name_mapper='^types/\(.*\)$' -> '/types/\1.js' module.name_mapper='\(jest-[^/]*\)' -> '/packages/\1/src/index.js' diff --git a/integration_tests/__tests__/__snapshots__/failures-test.js.snap b/integration_tests/__tests__/__snapshots__/failures-test.js.snap index f488a79d1701..511b1fda427f 100644 --- a/integration_tests/__tests__/__snapshots__/failures-test.js.snap +++ b/integration_tests/__tests__/__snapshots__/failures-test.js.snap @@ -70,7 +70,7 @@ Object { Expected two assertions to be called but only received one assertion call. - at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:62:21) + at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:64:21) ● .assertions() › throws on redeclare of assertion count @@ -87,7 +87,7 @@ Object { Expected zero assertions to be called but only received one assertion call. - at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:62:21) + at addAssertionErrors (../../packages/jest-jasmine2/build/setup-jest-globals.js:64:21) .assertions() ✕ throws diff --git a/packages/jest-diff/src/index.js b/packages/jest-diff/src/index.js index 04baf0a10099..73a957f65fb3 100644 --- a/packages/jest-diff/src/index.js +++ b/packages/jest-diff/src/index.js @@ -12,11 +12,13 @@ import type {DiffOptions} from './diffStrings'; -const ReactElementPlugin = require('pretty-format/build/plugins/ReactElement'); -const ReactTestComponentPlugin = require('pretty-format/build/plugins/ReactTestComponent'); -const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher'); -const HTMLElementPlugin = require('pretty-format/build/plugins/HTMLElement'); -const ImmutablePlugins = require('pretty-format/build/plugins/ImmutablePlugins'); +const { + ReactElement, + ReactTestComponent, + AsymmetricMatcher, + HTMLElement, + Immutable, +} = require('pretty-format').plugins; const chalk = require('chalk'); const diffStrings = require('./diffStrings'); @@ -26,11 +28,11 @@ const prettyFormat = require('pretty-format'); const {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} = require('./constants'); const PLUGINS = [ - ReactTestComponentPlugin, - ReactElementPlugin, - AsymmetricMatcherPlugin, - HTMLElementPlugin, -].concat(ImmutablePlugins); + ReactTestComponent, + ReactElement, + AsymmetricMatcher, + HTMLElement, +].concat(Immutable); const FORMAT_OPTIONS = { plugins: PLUGINS, }; diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index bc8ef96d59fd..b23589f4ec8f 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -80,6 +80,7 @@ function jasmine2( )({ config, globalConfig, + localRequire: runtime.requireModule.bind(runtime), testPath, }); diff --git a/packages/jest-jasmine2/src/setup-jest-globals.js b/packages/jest-jasmine2/src/setup-jest-globals.js index 23c9ea2c0f1c..f26de34028c7 100644 --- a/packages/jest-jasmine2/src/setup-jest-globals.js +++ b/packages/jest-jasmine2/src/setup-jest-globals.js @@ -11,6 +11,7 @@ 'use strict'; import type {GlobalConfig, Path, ProjectConfig} from 'types/Config'; +import type {Plugin} from 'types/PrettyFormat'; const {getState, setState} = require('jest-matchers'); const {initializeSnapshotState, addSerializer} = require('jest-snapshot'); @@ -24,6 +25,7 @@ const { export type SetupOptions = {| config: ProjectConfig, globalConfig: GlobalConfig, + localRequire: (moduleName: string) => Plugin, testPath: Path, |}; @@ -104,12 +106,16 @@ const patchJasmine = () => { })(global.jasmine.Spec); }; -module.exports = ({config, globalConfig, testPath}: SetupOptions) => { +module.exports = ({ + config, + globalConfig, + localRequire, + testPath, +}: SetupOptions) => { // Jest tests snapshotSerializers in order preceding built-in serializers. // Therefore, add in reverse because the last added is the first tested. config.snapshotSerializers.concat().reverse().forEach(path => { - // $FlowFixMe - addSerializer(require(path)); + addSerializer(localRequire(path)); }); setState({testPath}); patchJasmine(); diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index 0e6efd8e14bc..7ceca1f9964b 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -12,16 +12,16 @@ const chalk = require('chalk'); const prettyFormat = require('pretty-format'); -const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher'); -const ReactElementPlugin = require('pretty-format/build/plugins/ReactElement'); -const HTMLElementPlugin = require('pretty-format/build/plugins/HTMLElement'); -const ImmutablePlugins = require('pretty-format/build/plugins/ImmutablePlugins'); - -const PLUGINS = [ - AsymmetricMatcherPlugin, - ReactElementPlugin, - HTMLElementPlugin, -].concat(ImmutablePlugins); +const { + AsymmetricMatcher, + ReactElement, + HTMLElement, + Immutable, +} = require('pretty-format').plugins; + +const PLUGINS = [AsymmetricMatcher, ReactElement, HTMLElement].concat( + Immutable, +); export type ValueType = | 'array' diff --git a/packages/jest-snapshot/src/plugins.js b/packages/jest-snapshot/src/plugins.js index 252cd82dc02b..22349f605d8c 100644 --- a/packages/jest-snapshot/src/plugins.js +++ b/packages/jest-snapshot/src/plugins.js @@ -9,19 +9,19 @@ */ 'use strict'; -const ReactElementPlugin = require('pretty-format/build/plugins/ReactElement'); -const ReactTestComponentPlugin = require('pretty-format/build/plugins/ReactTestComponent'); -const HTMLElementPlugin = require('pretty-format/build/plugins/HTMLElement'); -const ImmutablePlugins = require('pretty-format/build/plugins/ImmutablePlugins'); +import type {Plugin} from 'types/PrettyFormat'; -let PLUGINS = [ - ReactElementPlugin, - ReactTestComponentPlugin, - HTMLElementPlugin, -].concat(ImmutablePlugins); +const { + HTMLElement, + Immutable, + ReactElement, + ReactTestComponent, +} = require('pretty-format').plugins; + +let PLUGINS = [HTMLElement, ReactElement, ReactTestComponent].concat(Immutable); // Prepend to list so the last added is the first tested. -exports.addSerializer = (plugin: any) => { +exports.addSerializer = (plugin: Plugin) => { PLUGINS = [plugin].concat(PLUGINS); }; diff --git a/packages/pretty-format/README.md b/packages/pretty-format/README.md index 570adc113448..bec96afb4ca9 100755 --- a/packages/pretty-format/README.md +++ b/packages/pretty-format/README.md @@ -117,8 +117,8 @@ prettyFormat(obj, { ```js const prettyFormat = require('pretty-format'); -const reactTestPlugin = require('pretty-format/build/plugins/ReactTestComponent'); -const reactElementPlugin = require('pretty-format/build/plugins/ReactElement'); +const reactTestPlugin = require('pretty-format').plugins.ReactTestComponent; +const reactElementPlugin = require('pretty-format').plugins.ReactElement; const React = require('react'); const renderer = require('react-test-renderer'); diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index 952634b70ec7..d2ad8a6730e8 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -11,7 +11,13 @@ 'use strict'; -import type {Colors, Refs, StringOrNull, Plugins, Options} from './types.js'; +import type { + Colors, + Refs, + StringOrNull, + Plugins, + Options, +} from 'types/PrettyFormat'; const style = require('ansi-styles'); @@ -949,4 +955,13 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string { ); } +prettyFormat.plugins = { + AsymmetricMatcher: require('./plugins/AsymmetricMatcher'), + ConvertAnsi: require('./plugins/ConvertAnsi'), + HTMLElement: require('./plugins/HTMLElement'), + Immutable: require('./plugins/ImmutablePlugins'), + ReactElement: require('./plugins/ReactElement'), + ReactTestComponent: require('./plugins/ReactTestComponent'), +}; + module.exports = prettyFormat; diff --git a/packages/pretty-format/src/plugins/AsymmetricMatcher.js b/packages/pretty-format/src/plugins/AsymmetricMatcher.js index 839d97dbd246..766627cd1702 100644 --- a/packages/pretty-format/src/plugins/AsymmetricMatcher.js +++ b/packages/pretty-format/src/plugins/AsymmetricMatcher.js @@ -9,7 +9,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const asymmetricMatcher = Symbol.for('jest.asymmetricMatcher'); const SPACE = ' '; diff --git a/packages/pretty-format/src/plugins/ConvertAnsi.js b/packages/pretty-format/src/plugins/ConvertAnsi.js index ab7cc144e3f5..7a92fb7a0019 100644 --- a/packages/pretty-format/src/plugins/ConvertAnsi.js +++ b/packages/pretty-format/src/plugins/ConvertAnsi.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const ansiRegex = require('ansi-regex'); diff --git a/packages/pretty-format/src/plugins/HTMLElement.js b/packages/pretty-format/src/plugins/HTMLElement.js index 34abe0a1628f..c956d4aeec31 100644 --- a/packages/pretty-format/src/plugins/HTMLElement.js +++ b/packages/pretty-format/src/plugins/HTMLElement.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const escapeHTML = require('./lib/escapeHTML'); const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)/; diff --git a/packages/pretty-format/src/plugins/ImmutableList.js b/packages/pretty-format/src/plugins/ImmutableList.js index 9239bb6d3fe5..d2751116f096 100644 --- a/packages/pretty-format/src/plugins/ImmutableList.js +++ b/packages/pretty-format/src/plugins/ImmutableList.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ImmutableMap.js b/packages/pretty-format/src/plugins/ImmutableMap.js index 5b28b2045114..1ae6067cae07 100644 --- a/packages/pretty-format/src/plugins/ImmutableMap.js +++ b/packages/pretty-format/src/plugins/ImmutableMap.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js index 2ff1f1d84ecc..c732d0a1b530 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js index 823134aacac3..8c7a2ff78605 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ImmutableSet.js b/packages/pretty-format/src/plugins/ImmutableSet.js index cbdd208351fc..c5663b06c886 100644 --- a/packages/pretty-format/src/plugins/ImmutableSet.js +++ b/packages/pretty-format/src/plugins/ImmutableSet.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ImmutableStack.js b/packages/pretty-format/src/plugins/ImmutableStack.js index 7f4de0331042..6098659be11b 100644 --- a/packages/pretty-format/src/plugins/ImmutableStack.js +++ b/packages/pretty-format/src/plugins/ImmutableStack.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print, Plugin} from '../types.js'; +import type {Colors, Indent, Options, Print, Plugin} from 'types/PrettyFormat'; const printImmutable = require('./lib/printImmutable'); diff --git a/packages/pretty-format/src/plugins/ReactTestComponent.js b/packages/pretty-format/src/plugins/ReactTestComponent.js index db8977963da0..73c2f3ae52c5 100644 --- a/packages/pretty-format/src/plugins/ReactTestComponent.js +++ b/packages/pretty-format/src/plugins/ReactTestComponent.js @@ -18,7 +18,7 @@ import type { Plugin, ReactTestObject, ReactTestChild, -} from '../types.js'; +} from 'types/PrettyFormat'; const escapeHTML = require('./lib/escapeHTML'); diff --git a/packages/pretty-format/src/plugins/lib/printImmutable.js b/packages/pretty-format/src/plugins/lib/printImmutable.js index 01c6ad6c66ec..273fb6ef2a0a 100644 --- a/packages/pretty-format/src/plugins/lib/printImmutable.js +++ b/packages/pretty-format/src/plugins/lib/printImmutable.js @@ -10,7 +10,7 @@ 'use strict'; -import type {Colors, Indent, Options, Print} from '../../types.js'; +import type {Colors, Indent, Options, Print} from 'types/PrettyFormat'; const IMMUTABLE_NAMESPACE = 'Immutable.'; const SPACE = ' '; diff --git a/packages/pretty-format/src/types.js b/types/PrettyFormat.js similarity index 100% rename from packages/pretty-format/src/types.js rename to types/PrettyFormat.js From 21622c3aefde448f7ba29530465f02bb9462b349 Mon Sep 17 00:00:00 2001 From: cpojer Date: Fri, 28 Apr 2017 02:10:25 +0100 Subject: [PATCH 2/2] Add a test. --- .../snapshot-serializers-test.js.snap | 8 ++++---- .../__tests__/snapshot-serializers-test.js | 2 +- .../snapshot-serializers/package.json | 3 +++ .../snapshot-serializers/plugins/bar.js | 5 ++++- .../snapshot-serializers/transformer.js | 18 ++++++++++++++++++ .../snapshot-serializers/utils.js | 2 +- 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 integration_tests/snapshot-serializers/transformer.js diff --git a/integration_tests/__tests__/__snapshots__/snapshot-serializers-test.js.snap b/integration_tests/__tests__/__snapshots__/snapshot-serializers-test.js.snap index 2a87b6477f19..4acc30034214 100644 --- a/integration_tests/__tests__/__snapshots__/snapshot-serializers-test.js.snap +++ b/integration_tests/__tests__/__snapshots__/snapshot-serializers-test.js.snap @@ -7,8 +7,8 @@ Object { id=\\"foo\\" /> ", - "snapshot serializers works with first plugin 1": "foo: 1", - "snapshot serializers works with nested serializable objects 1": "foo: bar: 2", + "snapshot serializers works with first plugin 1": "foo - 1", + "snapshot serializers works with nested serializable objects 1": "foo - bar - 2", "snapshot serializers works with prepended plugins and default serializers 1": "
", "snapshot serializers works with prepended plugins from expect method called once 1": " @@ -39,6 +39,6 @@ Object { bProp={FOO: 8} /> ", - "snapshot serializers works with second plugin 1": "bar: 2", + "snapshot serializers works with second plugin 1": "bar - 2", } `; diff --git a/integration_tests/__tests__/snapshot-serializers-test.js b/integration_tests/__tests__/snapshot-serializers-test.js index 69db584d7f1c..eea69cd2e89d 100644 --- a/integration_tests/__tests__/snapshot-serializers-test.js +++ b/integration_tests/__tests__/snapshot-serializers-test.js @@ -17,7 +17,7 @@ const snapshotsDir = path.resolve(testDir, '__tests__/__snapshots__'); const snapshotPath = path.resolve(snapshotsDir, 'snapshot-test.js.snap'); const runAndAssert = () => { - const result = runJest.json('snapshot-serializers'); + const result = runJest.json('snapshot-serializers', ['--no-cache']); const json = result.json; expect(json.numTotalTests).toBe(7); expect(json.numPassedTests).toBe(7); diff --git a/integration_tests/snapshot-serializers/package.json b/integration_tests/snapshot-serializers/package.json index 9f215865987a..658160fc0f8e 100644 --- a/integration_tests/snapshot-serializers/package.json +++ b/integration_tests/snapshot-serializers/package.json @@ -1,6 +1,9 @@ { "jest": { "testEnvironment": "node", + "transform": { + "^.+\\.js$": "/transformer.js" + }, "snapshotSerializers": [ "./plugins/foo", "/plugins/bar" diff --git a/integration_tests/snapshot-serializers/plugins/bar.js b/integration_tests/snapshot-serializers/plugins/bar.js index 0720054dbe67..67775fff8347 100644 --- a/integration_tests/snapshot-serializers/plugins/bar.js +++ b/integration_tests/snapshot-serializers/plugins/bar.js @@ -8,5 +8,8 @@ */ 'use strict'; +/* eslint-disable no-unused-vars */ + const createPlugin = require('../utils').createPlugin; -module.exports = createPlugin('bar'); + +// We inject the call to "createPlugin('bar') through the transformer" diff --git a/integration_tests/snapshot-serializers/transformer.js b/integration_tests/snapshot-serializers/transformer.js new file mode 100644 index 000000000000..e02d8e999063 --- /dev/null +++ b/integration_tests/snapshot-serializers/transformer.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +module.exports = { + process(src, filename, config, options) { + if (/bar.js$/.test(filename)) { + return `${src};\nmodule.exports = createPlugin('bar');`; + } + return src; + }, +}; diff --git a/integration_tests/snapshot-serializers/utils.js b/integration_tests/snapshot-serializers/utils.js index 704fa21925f0..4b86c01f3e43 100644 --- a/integration_tests/snapshot-serializers/utils.js +++ b/integration_tests/snapshot-serializers/utils.js @@ -10,7 +10,7 @@ exports.createPlugin = prop => { return { - print: (val, serialize) => `${prop}: ${serialize(val[prop])}`, + print: (val, serialize) => `${prop} - ${serialize(val[prop])}`, test: val => val && val.hasOwnProperty(prop), }; };