Skip to content

Commit

Permalink
Fix snapshot serializer require, restructure pretty-format.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Apr 28, 2017
1 parent 4f020f6 commit 89412c5
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 47 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
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 89412c5

Please sign in to comment.