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

fix(jest-jasmine2, jest-types): remove jasmine types from @jest/types #12125

Merged
merged 8 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 11 additions & 25 deletions packages/jest-jasmine2/src/errorOnPrivate.ts
Expand Up @@ -7,29 +7,17 @@

import type {Global} from '@jest/types';
import {ErrorWithStack} from 'jest-util';
import type {Jasmine} from './types';

type DisabledGlobalKeys = 'fail' | 'pending' | 'spyOn' | 'spyOnProperty';

// prettier-ignore
const disabledGlobals: Record<DisabledGlobalKeys, string> = {
const disabledGlobals: Record<string, string> = {
fail: 'Illegal usage of global `fail`, prefer throwing an error, or the `done.fail` callback.',
pending: 'Illegal usage of global `pending`, prefer explicitly skipping a test using `test.skip`',
spyOn: 'Illegal usage of global `spyOn`, prefer `jest.spyOn`.',
spyOnProperty: 'Illegal usage of global `spyOnProperty`, prefer `jest.spyOn`.',
};

type DisabledJasmineMethodsKeys =
| 'addMatchers'
| 'any'
| 'anything'
| 'arrayContaining'
| 'createSpy'
| 'objectContaining'
| 'stringMatching';

// prettier-ignore
const disabledJasmineMethods: Record<DisabledJasmineMethodsKeys, string> = {
const disabledJasmineMethods: Record<string, string> = {
addMatchers: 'Illegal usage of `jasmine.addMatchers`, prefer `expect.extends`.',
any: 'Illegal usage of `jasmine.any`, prefer `expect.any`.',
anything: 'Illegal usage of `jasmine.anything`, prefer `expect.anything`.',
Expand All @@ -40,21 +28,19 @@ const disabledJasmineMethods: Record<DisabledJasmineMethodsKeys, string> = {
};

export function installErrorOnPrivate(global: Global.Global): void {
const jasmine = global.jasmine as Jasmine;
const jasmine = global.jasmine;

(Object.keys(disabledGlobals) as Array<DisabledGlobalKeys>).forEach(
functionName => {
global[functionName] = () => {
throwAtFunction(disabledGlobals[functionName], global[functionName]);
};
},
);
Object.keys(disabledGlobals).forEach(functionName => {
global[functionName] = () => {
// @ts-expect-error
throwAtFunction(disabledGlobals[functionName], global[functionName]);
};
});

(
Object.keys(disabledJasmineMethods) as Array<DisabledJasmineMethodsKeys>
).forEach(methodName => {
Object.keys(disabledJasmineMethods).forEach(methodName => {
// @ts-expect-error
jasmine[methodName] = () => {
// @ts-expect-error
throwAtFunction(disabledJasmineMethods[methodName], jasmine[methodName]);
};
});
Expand Down
15 changes: 14 additions & 1 deletion packages/jest-jasmine2/src/jasmine/jasmineLight.ts
Expand Up @@ -40,10 +40,23 @@ import Timer from './Timer';
import createSpy from './createSpy';
import SpyRegistry from './spyRegistry';

const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL');

export const create = function (createOptions: Record<string, any>): Jasmine {
const j$ = {...createOptions} as Jasmine;

j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.testTimeout || 5000;
Object.defineProperty(j$, '_DEFAULT_TIMEOUT_INTERVAL', {
configurable: true,
enumerable: true,
get() {
return (
(global as any)[testTimeoutSymbol] || createOptions.testTimeout || 5000
);
},
set(value) {
(global as any)[testTimeoutSymbol] = value;
},
});

j$.getEnv = function () {
const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env());
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Expand Up @@ -230,7 +230,7 @@ export default function jasmineAsyncInstall(
globalConfig: Config.GlobalConfig,
global: Global.Global,
): void {
const jasmine = global.jasmine as Jasmine;
const jasmine = global.jasmine;
const mutex = throat(globalConfig.maxConcurrency);

const env = jasmine.getEnv();
Expand Down
7 changes: 2 additions & 5 deletions packages/jest-jasmine2/src/jestExpect.ts
Expand Up @@ -7,7 +7,6 @@

/* eslint-disable local/prefer-spread-eventually */

import type {Global} from '@jest/types';
import expect = require('expect');
import {
addSerializer,
Expand All @@ -16,9 +15,7 @@ import {
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingSnapshot,
} from 'jest-snapshot';
import type {Jasmine, JasmineMatchersObject, RawMatcherFn} from './types';

declare const global: Global.Global;
import type {JasmineMatchersObject, RawMatcherFn} from './types';

export default (config: {expand: boolean}): void => {
global.expect = expect;
Expand All @@ -31,7 +28,7 @@ export default (config: {expand: boolean}): void => {
});
expect.addSnapshotSerializer = addSerializer;

const jasmine = global.jasmine as Jasmine;
const jasmine = global.jasmine;
jasmine.anything = expect.anything;
jasmine.any = expect.any;
jasmine.objectContaining = expect.objectContaining;
Expand Down
9 changes: 3 additions & 6 deletions packages/jest-jasmine2/src/setup_jest_globals.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Config, Global} from '@jest/types';
import type {Config} from '@jest/types';
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import {
SnapshotState,
Expand All @@ -19,9 +19,6 @@ import type {
default as JasmineSpec,
SpecResult,
} from './jasmine/Spec';
import type {Jasmine} from './types';

declare const global: Global.Global;

export type SetupOptions = {
config: Config.ProjectConfig;
Expand Down Expand Up @@ -67,7 +64,7 @@ const addAssertionErrors = (result: SpecResult) => {
};

const patchJasmine = () => {
(global.jasmine as Jasmine).Spec = (realSpec => {
global.jasmine.Spec = (realSpec => {
class Spec extends realSpec {
constructor(attr: Attributes) {
const resultCallback = attr.resultCallback;
Expand All @@ -86,7 +83,7 @@ const patchJasmine = () => {
}

return Spec;
})((global.jasmine as Jasmine).Spec);
})(global.jasmine.Spec);
};

export default async ({
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-jasmine2/src/types.ts
Expand Up @@ -96,6 +96,15 @@ declare global {
namespace NodeJS {
interface Global {
expect: typeof expect;
jasmine: Jasmine;
}
}
}

declare module '@jest/types' {
namespace Global {
interface GlobalAdditions {
jasmine: Jasmine;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about pending etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied them all here for completeness. They were not necessary internally, but might be useful for something else.

}
}
}
8 changes: 2 additions & 6 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -1926,12 +1926,8 @@ export default class Runtime {
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);

const setTimeout = (timeout: number) => {
if (this._environment.global.jasmine) {
this._environment.global.jasmine._DEFAULT_TIMEOUT_INTERVAL = timeout;
} else {
// @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587
this._environment.global[testTimeoutSymbol] = timeout;
}
// @ts-expect-error: https://github.com/Microsoft/TypeScript/issues/24587
this._environment.global[testTimeoutSymbol] = timeout;
return jestObject;
};

Expand Down
11 changes: 0 additions & 11 deletions packages/jest-types/src/Global.ts
Expand Up @@ -51,12 +51,6 @@ export type EachTestFn<EachCallback extends TestCallback> = (
...args: Array<any>
) => ReturnType<EachCallback>;

// TODO: Get rid of this at some point
type Jasmine = {
_DEFAULT_TIMEOUT_INTERVAL?: number;
addMatchers: (matchers: Record<string, unknown>) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addMatchers implementation was removed in #9853

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think addMatchers still exist on jasmine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I had in mind Jest globals. It was removed from Jest globals, but Jasmin types still have addMatchers here: https://github.com/facebook/jest/blob/0d0844a249a179197e82bd1ea097a4cb6dad9f32/packages/jest-jasmine2/src/types.ts#L91

};

type Each<EachCallback extends TestCallback> =
| ((
table: EachTable,
Expand Down Expand Up @@ -124,11 +118,6 @@ export interface TestFrameworkGlobals {

export interface GlobalAdditions extends TestFrameworkGlobals {
__coverage__: CoverageMapData;
jasmine: Jasmine;
fail: () => void;
pending: () => void;
spyOn: () => void;
spyOnProperty: () => void;
}

export interface Global
Expand Down