diff --git a/e2e/browser-support/browser-test.js b/e2e/browser-support/browser-test.js index 6dd618a85119..f369f54d93dd 100644 --- a/e2e/browser-support/browser-test.js +++ b/e2e/browser-support/browser-test.js @@ -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() { diff --git a/packages/expect/src/__tests__/assertionCounts.test.js b/packages/expect/src/__tests__/assertionCounts.test.js index 8e5e42cf9031..b3c4029d9a4b 100644 --- a/packages/expect/src/__tests__/assertionCounts.test.js +++ b/packages/expect/src/__tests__/assertionCounts.test.js @@ -8,29 +8,29 @@ 'use strict'; -import jestExpect, {assertions, hasAssertions} from '../'; +import jestExpect from '../'; describe('.assertions()', () => { it('does not throw', () => { - assertions(2); + jestExpect.assertions(2); jestExpect('a').not.toBe('b'); jestExpect('a').toBe('a'); }); it('redeclares different assertion count', () => { - assertions(3); + jestExpect.assertions(3); jestExpect('a').not.toBe('b'); jestExpect('a').toBe('a'); - assertions(2); + jestExpect.assertions(2); }); it('expects no assertions', () => { - assertions(0); + jestExpect.assertions(0); }); }); describe('.hasAssertions()', () => { it('does not throw if there is an assertion', () => { - hasAssertions(); + jestExpect.hasAssertions(); jestExpect('a').toBe('a'); }); diff --git a/packages/expect/src/index.js b/packages/expect/src/index.js index 41d007959273..ac2644672c48 100644 --- a/packages/expect/src/index.js +++ b/packages/expect/src/index.js @@ -8,6 +8,7 @@ */ import type { + Expect, ExpectationObject, AsyncExpectationResult, SyncExpectationResult, @@ -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; @@ -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; @@ -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); @@ -367,7 +361,7 @@ export function assertions(expected: number) { getState().expectedAssertionsNumber = expected; getState().expectedAssertionsNumberError = error; } -export function hasAssertions(...args: Array) { +function hasAssertions(...args: Array) { const error = new Error(); if (Error.captureStackTrace) { Error.captureStackTrace(error, hasAssertions); @@ -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); diff --git a/packages/expect/src/jestMatchersObject.js b/packages/expect/src/jestMatchersObject.js index 24f2a2a4cecf..b3b7d8f87d7d 100644 --- a/packages/expect/src/jestMatchersObject.js +++ b/packages/expect/src/jestMatchersObject.js @@ -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. @@ -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]; diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.js b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.js index da65ad8abc8e..7eb30f05af1b 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.js +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.js @@ -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, @@ -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}; @@ -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': { @@ -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); } diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.js b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.js index 6e3b4bc0cb9b..dab570510844 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.js +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.js @@ -9,7 +9,7 @@ import type {RawMatcherFn} from 'types/Matchers'; -import expect, {setState, extend, assertions, hasAssertions} from 'expect'; +import expect from 'expect'; import { addSerializer, @@ -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; }; diff --git a/packages/jest-diff/src/index.js b/packages/jest-diff/src/index.js index 16db2abfc68a..b194c00fd18d 100644 --- a/packages/jest-diff/src/index.js +++ b/packages/jest-diff/src/index.js @@ -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'; @@ -22,7 +22,7 @@ const { Immutable, ReactElement, ReactTestComponent, -} = prettyPlugins; +} = prettyFormat.plugins; const PLUGINS = [ ReactTestComponent, diff --git a/packages/jest-jasmine2/src/jestExpect.js b/packages/jest-jasmine2/src/jestExpect.js index 83c9ade75a53..46ef61026e4c 100644 --- a/packages/jest-jasmine2/src/jestExpect.js +++ b/packages/jest-jasmine2/src/jestExpect.js @@ -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, @@ -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; diff --git a/packages/jest-jasmine2/src/setup_jest_globals.js b/packages/jest-jasmine2/src/setup_jest_globals.js index 98f1e0728030..501fda334611 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.js +++ b/packages/jest-jasmine2/src/setup_jest_globals.js @@ -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, @@ -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'; @@ -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, @@ -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); @@ -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; }; diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index de39f05a01c0..03f0e6369826 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -9,7 +9,7 @@ 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, @@ -17,7 +17,7 @@ const { Immutable, ReactElement, ReactTestComponent, -} = prettyPlugins; +} = prettyFormat.plugins; const PLUGINS = [ ReactTestComponent, diff --git a/packages/jest-snapshot/src/plugins.js b/packages/jest-snapshot/src/plugins.js index 9452fa7ea229..8a3131ccc980 100644 --- a/packages/jest-snapshot/src/plugins.js +++ b/packages/jest-snapshot/src/plugins.js @@ -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 { @@ -19,7 +19,7 @@ const { ReactElement, ReactTestComponent, AsymmetricMatcher, -} = prettyPlugins; +} = prettyFormat.plugins; let PLUGINS: Array = [ ReactTestComponent, diff --git a/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.js b/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.js index 5b9e9db158d0..382ae6bb4270 100644 --- a/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.js +++ b/packages/pretty-format/src/__tests__/AsymmetricMatcher.test.js @@ -9,9 +9,9 @@ import type {OptionsReceived} from 'types/PrettyFormat'; -import prettyFormat, {plugins} from '../'; +import prettyFormat from '../'; -const {AsymmetricMatcher} = plugins; +const {AsymmetricMatcher} = prettyFormat.plugins; let options: OptionsReceived; function fnNameFor(func) { diff --git a/packages/pretty-format/src/__tests__/ConvertAnsi.test.js b/packages/pretty-format/src/__tests__/ConvertAnsi.test.js index 65d5b4d79cc6..84000fdbb515 100644 --- a/packages/pretty-format/src/__tests__/ConvertAnsi.test.js +++ b/packages/pretty-format/src/__tests__/ConvertAnsi.test.js @@ -8,8 +8,9 @@ */ import ansiStyle from 'ansi-styles'; -import prettyFormat, {plugins} from '../'; -const {ConvertAnsi} = plugins; +import prettyFormat from '../'; + +const {ConvertAnsi} = prettyFormat.plugins; const prettyFormatResult = (val: string) => prettyFormat(val, { diff --git a/packages/pretty-format/src/__tests__/DOMCollection.test.js b/packages/pretty-format/src/__tests__/DOMCollection.test.js index 2b3ee9200b50..fb027efc5b7d 100644 --- a/packages/pretty-format/src/__tests__/DOMCollection.test.js +++ b/packages/pretty-format/src/__tests__/DOMCollection.test.js @@ -11,9 +11,10 @@ 'use strict'; -import {plugins} from '../'; +import prettyFormat from '../'; import getPrettyPrint from './getPrettyPrint'; -const {DOMCollection, DOMElement} = plugins; + +const {DOMCollection, DOMElement} = prettyFormat.plugins; const toPrettyPrintTo = getPrettyPrint([DOMCollection, DOMElement]); const expect: any = global.expect; diff --git a/packages/pretty-format/src/__tests__/DOMElement.test.js b/packages/pretty-format/src/__tests__/DOMElement.test.js index 448557e50117..231549b86e75 100644 --- a/packages/pretty-format/src/__tests__/DOMElement.test.js +++ b/packages/pretty-format/src/__tests__/DOMElement.test.js @@ -11,9 +11,10 @@ 'use strict'; -import prettyFormat, {plugins} from '../'; +import prettyFormat from '../'; import getPrettyPrint from './getPrettyPrint'; -const {DOMElement} = plugins; + +const {DOMElement} = prettyFormat.plugins; const toPrettyPrintTo = getPrettyPrint([DOMElement]); const expect: any = global.expect; diff --git a/packages/pretty-format/src/__tests__/Immutable.test.js b/packages/pretty-format/src/__tests__/Immutable.test.js index a1809423d435..3c9a7151c063 100644 --- a/packages/pretty-format/src/__tests__/Immutable.test.js +++ b/packages/pretty-format/src/__tests__/Immutable.test.js @@ -11,10 +11,10 @@ import React from 'react'; import Immutable from 'immutable'; -import {plugins as prettyPlugins} from '..' +import prettyFormat from '..' import getPrettyPrint from './getPrettyPrint'; -const {Immutable: ImmutablePlugin, ReactElement} = prettyPlugins; +const {Immutable: ImmutablePlugin, ReactElement} = prettyFormat.plugins; const toPrettyPrintTo = getPrettyPrint([ReactElement, ImmutablePlugin]); diff --git a/packages/pretty-format/src/__tests__/react.test.js b/packages/pretty-format/src/__tests__/react.test.js index b952b8083bfc..e99d53b6cbeb 100644 --- a/packages/pretty-format/src/__tests__/react.test.js +++ b/packages/pretty-format/src/__tests__/react.test.js @@ -11,12 +11,12 @@ import type {OptionsReceived} from 'types/PrettyFormat'; import React from 'react'; import renderer from 'react-test-renderer'; -import prettyFormat, {plugins} from '../'; +import prettyFormat from '../'; const elementSymbol = Symbol.for('react.element'); const fragmentSymbol = Symbol.for('react.fragment'); const testSymbol = Symbol.for('react.test.json'); -const {ReactElement, ReactTestComponent} = plugins; +const {ReactElement, ReactTestComponent} = prettyFormat.plugins; const formatElement = (element: any, options?: OptionsReceived) => prettyFormat( diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index ded981b495ff..46abdc4c5180 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -12,6 +12,7 @@ import type { Config, Options, OptionsReceived, + NewPlugin, Plugin, Plugins, Refs, @@ -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, @@ -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;