Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect: Export expect as default export #11490

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -9,7 +9,7 @@ const chalk = require('chalk');
const Immutable = require('immutable');
const {alignedAnsiStyleSerializer} = require('@jest/test-utils');
const {stringify} = require('jest-matcher-utils');
const jestExpect = require('../');
const {default: jestExpect} = require('../');
const chalkEnabled = chalk.enabled;

expect.addSnapshotSerializer(alignedAnsiStyleSerializer);
Expand Down
17 changes: 5 additions & 12 deletions packages/expect/src/index.ts
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';
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
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
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
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
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
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