Skip to content

Commit

Permalink
expect: Export expect as default export
Browse files Browse the repository at this point in the history
This allows types to be exported as well.
Fixes #11487
Strictly speaking a breaking change, as expect = require('expect') not longer works, but as this is usually used as a global, this shouldn't be much of an issue.
  • Loading branch information
NaridaL committed May 30, 2021
1 parent 82d1a1a commit 0523c5a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 32 deletions.
17 changes: 5 additions & 12 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import type {
AsyncExpectationResult,
Expect,
ExpectationResult,
MatcherState as JestMatcherState,
Matchers as MatcherInterface,
MatcherState,
MatchersObject,
PromiseMatcherFn,
RawMatcherFn,
Expand All @@ -62,7 +61,7 @@ const createToThrowErrorMatchingSnapshotMatcher = function (
matcher: RawMatcherFn,
) {
return function (
this: JestMatcherState,
this: MatcherState,
received: any,
testNameOrInlineSnapshot?: string,
) {
Expand Down Expand Up @@ -251,7 +250,7 @@ const makeThrowingMatcher = (
let throws = true;
const utils = {...matcherUtils, iterableEquality, subsetEquality};

const matcherContext: JestMatcherState = {
const matcherContext: MatcherState = {
// When throws is disabled, the matcher will not throw errors during test
// execution but instead add them to the global matcher state. If a
// matcher throws, test execution is normally stopped immediately. The
Expand Down Expand Up @@ -422,11 +421,5 @@ expect.getState = getState;
expect.setState = setState;
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;

const expectExport = expect as Expect;

declare namespace expectExport {
export type MatcherState = JestMatcherState;
export interface Matchers<R> extends MatcherInterface<R> {}
}

export = expectExport;
export default expect as Expect;
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
createEmptyTestResult,
} from '@jest/test-result';
import type {Circus, Config, Global} from '@jest/types';
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import expect, {Expect} from 'expect';
import {bind} from 'jest-each';
import {formatExecError, formatResultsErrors} from 'jest-message-util';
import {
Expand All @@ -34,7 +34,7 @@ import {
} from '../state';
import testCaseReportHandler from '../testCaseReportHandler';
import {getTestID} from '../utils';
import createExpect, {Expect} from './jestExpect';
import createExpect from './jestExpect';

type Process = NodeJS.Process;

Expand Down Expand Up @@ -159,7 +159,7 @@ export const initialize = async ({
updateSnapshot,
});
// @ts-expect-error: snapshotState is a jest extension of `expect`
setState({snapshotState, testPath});
expect.setState({snapshotState, testPath});

addEventHandler(handleSnapshotStateAfterRetry(snapshotState));
if (sendMessageToJest) {
Expand Down Expand Up @@ -276,7 +276,7 @@ const handleSnapshotStateAfterRetry =
const eventHandler = async (event: Circus.Event) => {
switch (event.name) {
case 'test_start': {
setState({currentTestName: getTestID(event.test)});
expect.setState({currentTestName: getTestID(event.test)});
break;
}
case 'test_done': {
Expand All @@ -288,7 +288,7 @@ const eventHandler = async (event: Circus.Event) => {
};

const _addExpectedAssertionErrors = (test: Circus.TestEntry) => {
const failures = extractExpectedAssertionsErrors();
const failures = expect.extractExpectedAssertionsErrors();
const errors = failures.map(failure => failure.error);
test.errors = test.errors.concat(errors);
};
Expand All @@ -297,8 +297,8 @@ const _addExpectedAssertionErrors = (test: Circus.TestEntry) => {
// test execution and add them to the test result, potentially failing
// a passing test.
const _addSuppressedErrors = (test: Circus.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 @@ -6,7 +6,7 @@
*/

import type {Config} from '@jest/types';
import expect = require('expect');
import expect, {Expect} from 'expect';
import {
addSerializer,
toMatchInlineSnapshot,
Expand All @@ -15,8 +15,6 @@ import {
toThrowErrorMatchingSnapshot,
} from 'jest-snapshot';

export type Expect = typeof expect;

export default (config: Pick<Config.GlobalConfig, 'expand'>): Expect => {
expect.setState({expand: config.expand});
expect.extend({
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-globals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type {Jest} from '@jest/environment';
import type {Global} from '@jest/types';
import importedExpect = require('expect');
import type importedExpect from 'expect';

export declare const jest: Jest;

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/jestExpect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/* eslint-disable local/prefer-spread-eventually */

import type {Global} from '@jest/types';
import expect = require('expect');
import expect, {MatcherState} from 'expect';
import {
addSerializer,
toMatchInlineSnapshot,
Expand Down Expand Up @@ -42,7 +42,7 @@ export default (config: {expand: boolean}): void => {
const jestMatchersObject = Object.create(null);
Object.keys(jasmineMatchersObject).forEach(name => {
jestMatchersObject[name] = function (
this: expect.MatcherState,
this: MatcherState,
...args: Array<unknown>
): RawMatcherFn {
// use "expect.extend" if you need to use equality testers (via this.equal)
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-jasmine2/src/setup_jest_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type {Config, Global} from '@jest/types';
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import expect from 'expect';
import {
SnapshotState,
SnapshotStateType,
Expand Down Expand Up @@ -34,8 +34,8 @@ export type SetupOptions = {
// test execution and add them to the test result, potentially failing
// a passing test.
const addSuppressedErrors = (result: SpecResult) => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
const {suppressedErrors} = expect.getState();
expect.setState({suppressedErrors: []});
if (suppressedErrors.length) {
result.status = 'failed';

Expand All @@ -53,7 +53,7 @@ const addSuppressedErrors = (result: SpecResult) => {
};

const addAssertionErrors = (result: SpecResult) => {
const assertionErrors = extractExpectedAssertionsErrors();
const assertionErrors = expect.extractExpectedAssertionsErrors();
if (assertionErrors.length) {
const jasmineErrors = assertionErrors.map(({actual, error, expected}) => ({
actual,
Expand All @@ -78,7 +78,7 @@ const patchJasmine = () => {
};
const onStart = attr.onStart;
attr.onStart = (context: JasmineSpec) => {
setState({currentTestName: context.getFullName()});
expect.setState({currentTestName: context.getFullName()});
onStart && onStart.call(attr, context);
};
super(attr);
Expand Down Expand Up @@ -115,7 +115,7 @@ export default async ({
updateSnapshot,
});
// @ts-expect-error: snapshotState is a jest extension of `expect`
setState({snapshotState, testPath});
expect.setState({snapshotState, testPath});
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
};
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type {AssertionError} from 'assert';
import type {Config} from '@jest/types';
import expect = require('expect');
import type expect from 'expect';
import type CallTracker from './jasmine/CallTracker';
import type Env from './jasmine/Env';
import type JsApiReporter from './jasmine/JsApiReporter';
Expand Down

0 comments on commit 0523c5a

Please sign in to comment.