Skip to content

Commit

Permalink
Fix snapshot serializer require, restructure pretty-format. (#3399)
Browse files Browse the repository at this point in the history
* Fix snapshot serializer require, restructure pretty-format.

* Add a test.
  • Loading branch information
cpojer committed Apr 28, 2017
1 parent 33dceab commit 53bea61
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 56 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.*/dangerfile.js

[options]
module.name_mapper='^pretty-format$' -> '<PROJECT_ROOT>/packages/pretty-format/src/index.js'
module.name_mapper='^types/\(.*\)$' -> '<PROJECT_ROOT>/types/\1.js'
module.name_mapper='\(jest-[^/]*\)' -> '<PROJECT_ROOT>/packages/\1/src/index.js'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ 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": "
<div
aProp={
Object {
\\"a\\": 6,
}
}
bProp={foo: 8}
bProp={foo - 8}
/>
",
"snapshot serializers works with prepended plugins from expect method called once 1": "
Expand All @@ -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",
}
`;
2 changes: 1 addition & 1 deletion integration_tests/__tests__/snapshot-serializers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/snapshot-serializers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "<rootDir>/transformer.js"
},
"snapshotSerializers": [
"./plugins/foo",
"<rootDir>/plugins/bar"
Expand Down
5 changes: 4 additions & 1 deletion integration_tests/snapshot-serializers/plugins/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions integration_tests/snapshot-serializers/transformer.js
Original file line number Diff line number Diff line change
@@ -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;
},
};
2 changes: 1 addition & 1 deletion integration_tests/snapshot-serializers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
};
22 changes: 12 additions & 10 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
};
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function jasmine2(
)({
config,
globalConfig,
localRequire: runtime.requireModule.bind(runtime),
testPath,
});

Expand Down
12 changes: 9 additions & 3 deletions packages/jest-jasmine2/src/setup-jest-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -24,6 +25,7 @@ const {
export type SetupOptions = {|
config: ProjectConfig,
globalConfig: GlobalConfig,
localRequire: (moduleName: string) => Plugin,
testPath: Path,
|};

Expand Down Expand Up @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
20 changes: 10 additions & 10 deletions packages/jest-snapshot/src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 16 additions & 1 deletion packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/AsymmetricMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ConvertAnsi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)/;
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableOrderedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableOrderedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ImmutableStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/ReactTestComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
Plugin,
ReactTestObject,
ReactTestChild,
} from '../types.js';
} from 'types/PrettyFormat';

const escapeHTML = require('./lib/escapeHTML');

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/plugins/lib/printImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand Down
File renamed without changes.

0 comments on commit 53bea61

Please sign in to comment.