Skip to content

Commit

Permalink
rollback som expect and pretty-format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 26, 2018
1 parent ba200c6 commit b4f9e22
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 55 deletions.
4 changes: 2 additions & 2 deletions e2e/browser-support/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

/* eslint-disable */
var {default: expect} = require('../../packages/expect/build-es5/index.js');
var expect = require('../../packages/expect/build-es5/index.js');
var mock = require('../../packages/jest-mock/build-es5/index.js');
var {default: prettyFormat} = require('../../packages/pretty-format/build-es5/index.js');
var prettyFormat = require('../../packages/pretty-format/build-es5/index.js');

describe('es5 builds in browser', function() {
it('runs assertions', function() {
Expand Down
25 changes: 12 additions & 13 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import type {
Expect,
ExpectationObject,
AsyncExpectationResult,
SyncExpectationResult,
Expand Down Expand Up @@ -46,12 +47,7 @@ import {
getMatchers,
setMatchers,
} from './jestMatchersObject';

export {
default as extractExpectedAssertionsErrors,
} from './extractExpectedAssertionsErrors';

export {getState, setState};
import extractExpectedAssertionsErrors from './extractExpectedAssertionsErrors';

class JestAssertionError extends Error {
matcherResult: any;
Expand Down Expand Up @@ -320,11 +316,9 @@ const makeThrowingMatcher = (
}
};

export const extend = (matchers: MatchersObject): void =>
expect.extend = (matchers: MatchersObject): void =>
setMatchers(matchers, false, expect);

expect.extend = extend;

expect.anything = anything;
expect.any = any;

Expand Down Expand Up @@ -358,7 +352,7 @@ const _validateResult = result => {
}
};

export function assertions(expected: number) {
function assertions(expected: number) {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, assertions);
Expand All @@ -367,7 +361,7 @@ export function assertions(expected: number) {
getState().expectedAssertionsNumber = expected;
getState().expectedAssertionsNumberError = error;
}
export function hasAssertions(...args: Array<any>) {
function hasAssertions(...args: Array<any>) {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, hasAssertions);
Expand All @@ -383,6 +377,11 @@ setMatchers(matchers, true, expect);
setMatchers(spyMatchers, true, expect);
setMatchers(toThrowMatchers, true, expect);

export const addSnapshotSerializer = () => void 0;
expect.addSnapshotSerializer = () => void 0;
expect.assertions = assertions;
expect.hasAssertions = hasAssertions;
expect.getState = getState;
expect.setState = setState;
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;

export default expect;
export default (expect: Expect);
8 changes: 6 additions & 2 deletions packages/expect/src/jestMatchersObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
*/

import {AsymmetricMatcher} from './asymmetricMatchers';
import type {MatchersObject, SyncExpectationResult} from 'types/Matchers';
import type {
Expect,
MatchersObject,
SyncExpectationResult,
} from 'types/Matchers';

// Global matchers object holds the list of available matchers and
// the state, that can hold matcher specific values that change over time.
Expand Down Expand Up @@ -43,7 +47,7 @@ export const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
export const setMatchers = (
matchers: MatchersObject,
isInternal: boolean,
expect: any,
expect: Expect,
) => {
Object.keys(matchers).forEach(key => {
const matcher = matchers[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {AssertionResult, TestResult, Status} from 'types/TestResult';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {Event, RunResult, TestEntry} from 'types/Circus';

import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import expect from 'expect';
import {formatExecError, formatResultsErrors} from 'jest-message-util';
import {
SnapshotState,
Expand Down Expand Up @@ -106,7 +106,7 @@ export const initialize = ({
getPrettier,
updateSnapshot,
});
setState({snapshotState, testPath});
expect.setState({snapshotState, testPath});

// Return it back to the outer scope (test runner outside the VM).
return {globals, snapshotState};
Expand Down Expand Up @@ -221,7 +221,7 @@ export const runAndTransformResultsToJestFormat = async ({
const eventHandler = (event: Event) => {
switch (event.name) {
case 'test_start': {
setState({currentTestName: getTestID(event.test)});
expect.setState({currentTestName: getTestID(event.test)});
break;
}
case 'test_done': {
Expand All @@ -233,17 +233,17 @@ const eventHandler = (event: Event) => {
};

const _addExpectedAssertionErrors = (test: TestEntry) => {
const failures = extractExpectedAssertionsErrors();
const failures = expect.extractExpectedAssertionsErrors();
const errors = failures.map(failure => failure.error);
test.errors = test.errors.concat(errors);
};

// Get suppressed errors from ``jest-matchers`` that weren't throw during
// Get suppressed errors from `expect` that weren't throw during
// test execution and add them to the test result, potentially failing
// a passing test.
const _addSuppressedErrors = (test: TestEntry) => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
const {suppressedErrors} = expect.getState();
expect.setState({suppressedErrors: []});
if (suppressedErrors.length) {
test.errors = test.errors.concat(suppressedErrors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type {RawMatcherFn} from 'types/Matchers';

import expect, {setState, extend, assertions, hasAssertions} from 'expect';
import expect from 'expect';

import {
addSerializer,
Expand All @@ -27,16 +27,13 @@ type JasmineMatcher = {

export default (config: {expand: boolean}) => {
global.expect = expect;
setState({expand: config.expand});
extend({
expect.setState({expand: config.expand});
expect.extend({
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
});
global.expect.extend = extend;
global.expect.assertions = assertions;
global.expect.hasAssertions = hasAssertions;

(expect: Object).addSnapshotSerializer = addSerializer;
};
4 changes: 2 additions & 2 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type {DiffOptions} from './diffStrings';

import prettyFormat, {plugins as prettyPlugins} from 'pretty-format';
import prettyFormat from 'pretty-format';
import chalk from 'chalk';
import getType from 'jest-get-type';
import diffStrings from './diffStrings';
Expand All @@ -22,7 +22,7 @@ const {
Immutable,
ReactElement,
ReactTestComponent,
} = prettyPlugins;
} = prettyFormat.plugins;

const PLUGINS = [
ReactTestComponent,
Expand Down
14 changes: 3 additions & 11 deletions packages/jest-jasmine2/src/jestExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@

import type {RawMatcherFn} from 'types/Matchers';

import expect, {
setState,
extend,
assertions,
hasAssertions,
} from 'expect';
import expect from 'expect';
import {
addSerializer,
toMatchSnapshot,
Expand All @@ -32,17 +27,14 @@ type JasmineMatchersObject = {[id: string]: JasmineMatcher};

export default (config: {expand: boolean}) => {
global.expect = expect;
setState({expand: config.expand});
extend({
expect.setState({expand: config.expand});
expect.extend({
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
});
(expect: Object).addSnapshotSerializer = addSerializer;
global.expect.extend = extend;
global.expect.assertions = assertions;
global.expect.hasAssertions = hasAssertions;

const jasmine = global.jasmine;
jasmine.anything = expect.anything;
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-jasmine2/src/setup_jest_globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {Plugin} from 'types/PrettyFormat';

import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import expect from 'expect';
import {
buildSnapshotResolver,
SnapshotState,
Expand All @@ -29,8 +29,8 @@ export type SetupOptions = {|
// test execution and add them to the test result, potentially failing
// a passing test.
const addSuppressedErrors = result => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
const {suppressedErrors} = expect.getState();
expect.setState({suppressedErrors: []});
if (suppressedErrors.length) {
result.status = 'failed';

Expand All @@ -47,7 +47,7 @@ const addSuppressedErrors = result => {
};

const addAssertionErrors = result => {
const assertionErrors = extractExpectedAssertionsErrors();
const assertionErrors = expect.extractExpectedAssertionsErrors();
if (assertionErrors.length) {
const jasmineErrors = assertionErrors.map(({actual, error, expected}) => ({
actual,
Expand All @@ -71,7 +71,7 @@ const patchJasmine = () => {
};
const onStart = attr.onStart;
attr.onStart = context => {
setState({currentTestName: context.getFullName()});
expect.setState({currentTestName: context.getFullName()});
onStart && onStart.call(attr, context);
};
realSpec.call(this, attr);
Expand Down Expand Up @@ -117,7 +117,7 @@ export default ({
: null,
updateSnapshot,
});
setState({snapshotState, testPath});
expect.setState({snapshotState, testPath});
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
};
4 changes: 2 additions & 2 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

import chalk from 'chalk';
import getType from 'jest-get-type';
import prettyFormat, {plugins as prettyPlugins} from 'pretty-format';
import prettyFormat from 'pretty-format';
const {
AsymmetricMatcher,
DOMCollection,
DOMElement,
Immutable,
ReactElement,
ReactTestComponent,
} = prettyPlugins;
} = prettyFormat.plugins;

const PLUGINS = [
ReactTestComponent,
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-snapshot/src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type {Plugin} from 'types/PrettyFormat';

import {plugins as prettyPlugins} from 'pretty-format';
import prettyFormat from 'pretty-format';
import jestMockSerializer from './mock_serializer';

const {
Expand All @@ -19,7 +19,7 @@ const {
ReactElement,
ReactTestComponent,
AsymmetricMatcher,
} = prettyPlugins;
} = prettyFormat.plugins;

let PLUGINS: Array<Plugin> = [
ReactTestComponent,
Expand Down
5 changes: 3 additions & 2 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Config,
Options,
OptionsReceived,
NewPlugin,
Plugin,
Plugins,
Refs,
Expand Down Expand Up @@ -496,7 +497,7 @@ export default function prettyFormat(val: any, options?: OptionsReceived): strin
return printComplexValue(val, getConfig(options), '', 0, []);
}

export const plugins = {
const plugins: {[s: string]: NewPlugin} = {
AsymmetricMatcher,
ConvertAnsi,
DOMCollection,
Expand All @@ -506,5 +507,5 @@ export const plugins = {
ReactTestComponent,
};

// TODO: Remove at some point (currently breaks `dom-testing-library`)
// TODO: Consider exporting as ESM
prettyFormat.plugins = plugins;

0 comments on commit b4f9e22

Please sign in to comment.