Skip to content

Commit c2d4aa5

Browse files
authored
chore(jest-jasmine2): remove usage of Function type (#10216)
1 parent 098ce75 commit c2d4aa5

File tree

8 files changed

+81
-40
lines changed

8 files changed

+81
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
- `[jest-jasmine2]` Convert `PCancelable` to TypeScript ([#10215](https://github.com/facebook/jest/pull/10215))
1717
- `[jest-jasmine2]` Refine typings of `queueRunner` ([#10215](https://github.com/facebook/jest/pull/10215))
18+
- `[jest-jasmine2]` Remove usage of `Function` type ([#10216](https://github.com/facebook/jest/pull/10216))
1819
- `[jest-resolve]` Improve types ([#10239](https://github.com/facebook/jest/pull/10239))
1920

2021
### Performance

packages/jest-jasmine2/src/jasmine/Env.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ import queueRunner, {
4141
import treeProcessor, {TreeNode} from '../treeProcessor';
4242
import isError from '../isError';
4343
import assertionErrorMessage from '../assertionErrorMessage';
44-
import type {AssertionErrorWithStack, Jasmine, Reporter, Spy} from '../types';
44+
import type {
45+
AssertionErrorWithStack,
46+
Jasmine,
47+
Reporter,
48+
SpecDefinitionsFn,
49+
Spy,
50+
} from '../types';
4551
import type {default as Spec, SpecResult} from './Spec';
4652
import type Suite from './Suite';
4753

@@ -64,7 +70,10 @@ export default function (j$: Jasmine) {
6470
runnablesToRun?: Array<string>,
6571
suiteTree?: Suite,
6672
) => Promise<void>;
67-
fdescribe: (description: string, specDefinitions: Function) => Suite;
73+
fdescribe: (
74+
description: string,
75+
specDefinitions: SpecDefinitionsFn,
76+
) => Suite;
6877
spyOn: (
6978
obj: Record<string, Spy>,
7079
methodName: string,
@@ -78,15 +87,21 @@ export default function (j$: Jasmine) {
7887
clearReporters: () => void;
7988
addReporter: (reporterToAdd: Reporter) => void;
8089
it: (description: string, fn: QueueableFn['fn'], timeout?: number) => Spec;
81-
xdescribe: (description: string, specDefinitions: Function) => Suite;
90+
xdescribe: (
91+
description: string,
92+
specDefinitions: SpecDefinitionsFn,
93+
) => Suite;
8294
xit: (description: string, fn: QueueableFn['fn'], timeout?: number) => Spec;
8395
beforeAll: (beforeAllFunction: QueueableFn['fn'], timeout?: number) => void;
8496
todo: () => Spec;
8597
provideFallbackReporter: (reporterToAdd: Reporter) => void;
8698
allowRespy: (allow: boolean) => void;
87-
describe: (description: string, specDefinitions: Function) => Suite;
99+
describe: (
100+
description: string,
101+
specDefinitions: SpecDefinitionsFn,
102+
) => Suite;
88103

89-
constructor(_options?: object) {
104+
constructor(_options?: Record<string, unknown>) {
90105
let totalSpecsDefined = 0;
91106

92107
let catchExceptions = true;
@@ -411,13 +426,16 @@ export default function (j$: Jasmine) {
411426
return suite;
412427
};
413428

414-
const addSpecsToSuite = (suite: Suite, specDefinitions: Function) => {
429+
const addSpecsToSuite = (
430+
suite: Suite,
431+
specDefinitions: SpecDefinitionsFn,
432+
) => {
415433
const parentSuite = currentDeclarationSuite;
416434
parentSuite.addChild(suite);
417435
currentDeclarationSuite = suite;
418436

419437
let declarationError: undefined | Error = undefined;
420-
let describeReturnValue: undefined | Error = undefined;
438+
let describeReturnValue: unknown | Error;
421439
try {
422440
describeReturnValue = specDefinitions.call(suite);
423441
} catch (e) {

packages/jest-jasmine2/src/jasmine/Spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export default class Spec {
171171
}
172172
}
173173

174-
execute(onComplete: Function, enabled: boolean) {
174+
execute(onComplete?: () => void, enabled?: boolean) {
175175
const self = this;
176176

177177
this.onStart(this);
@@ -203,7 +203,7 @@ export default class Spec {
203203

204204
this.currentRun.then(() => complete(true));
205205

206-
function complete(enabledAgain: boolean) {
206+
function complete(enabledAgain?: boolean) {
207207
self.result.status = self.status(enabledAgain);
208208
self.resultCallback(self.result);
209209

packages/jest-jasmine2/src/jasmine/jasmineLight.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3030
*/
3131
/* eslint-disable sort-keys */
3232

33-
import type {Jasmine} from '../types';
33+
import type {Jasmine, SpecDefinitionsFn} from '../types';
3434
import createSpy from './createSpy';
3535
import Env from './Env';
3636
import JsApiReporter from './JsApiReporter';
@@ -45,7 +45,7 @@ export const create = function (createOptions: Record<string, any>): Jasmine {
4545

4646
j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.testTimeout || 5000;
4747

48-
j$.getEnv = function (options?: object) {
48+
j$.getEnv = function (options?: Record<string, unknown>) {
4949
const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options));
5050
//jasmine. singletons in here (setTimeout blah blah).
5151
return env;
@@ -66,15 +66,15 @@ export const create = function (createOptions: Record<string, any>): Jasmine {
6666
// Interface is a reserved word in strict mode, so can't export it as ESM
6767
export const _interface = function (jasmine: Jasmine, env: any) {
6868
const jasmineInterface = {
69-
describe(description: string, specDefinitions: Function) {
69+
describe(description: string, specDefinitions: SpecDefinitionsFn) {
7070
return env.describe(description, specDefinitions);
7171
},
7272

73-
xdescribe(description: string, specDefinitions: Function) {
73+
xdescribe(description: string, specDefinitions: SpecDefinitionsFn) {
7474
return env.xdescribe(description, specDefinitions);
7575
},
7676

77-
fdescribe(description: string, specDefinitions: Function) {
77+
fdescribe(description: string, specDefinitions: SpecDefinitionsFn) {
7878
return env.fdescribe(description, specDefinitions);
7979
},
8080

packages/jest-jasmine2/src/jasmineAsyncInstall.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@ import throat from 'throat';
1717
import isError from './isError';
1818
import type {Jasmine} from './types';
1919
import type Spec from './jasmine/Spec';
20-
import type {QueueableFn} from './queueRunner';
20+
import type {DoneFn, QueueableFn} from './queueRunner';
2121

22-
interface DoneFn {
23-
(): void;
24-
fail: (error: Error) => void;
25-
}
26-
27-
function isPromise(obj: any) {
22+
function isPromise(obj: any): obj is PromiseLike<unknown> {
2823
return obj && typeof obj.then === 'function';
2924
}
3025

26+
const doneFnNoop = () => {};
27+
28+
doneFnNoop.fail = () => {};
29+
3130
function promisifyLifeCycleFunction(
32-
originalFn: Function,
31+
originalFn: (beforeAllFunction: QueueableFn['fn'], timeout?: number) => void,
3332
env: Jasmine['currentEnv_'],
3433
) {
3534
return function <T>(
36-
fn: Function | (() => Promise<T>) | GeneratorFunction | undefined,
35+
fn:
36+
| ((done: DoneFn) => void | PromiseLike<T>)
37+
| (() => Promise<T>)
38+
| GeneratorFunction
39+
| undefined,
3740
timeout?: number,
38-
) {
41+
): void {
3942
if (!fn) {
43+
// @ts-expect-error: missing fn arg is handled by originalFn
4044
return originalFn.call(env);
4145
}
4246

@@ -59,7 +63,7 @@ function promisifyLifeCycleFunction(
5963
// didn't return a promise.
6064
const asyncJestLifecycle = function (done: DoneFn) {
6165
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
62-
const returnValue = wrappedFn.call({});
66+
const returnValue = wrappedFn.call({}, doneFnNoop);
6367

6468
if (isPromise(returnValue)) {
6569
returnValue.then(done.bind(null, null), (error: Error) => {
@@ -82,12 +86,21 @@ function promisifyLifeCycleFunction(
8286
// Similar to promisifyLifeCycleFunction but throws an error
8387
// when the return value is neither a Promise nor `undefined`
8488
function promisifyIt(
85-
originalFn: Function,
89+
originalFn: (
90+
description: string,
91+
fn: QueueableFn['fn'],
92+
timeout?: number,
93+
) => Spec,
8694
env: Jasmine['currentEnv_'],
8795
jasmine: Jasmine,
8896
) {
89-
return function (specName: string, fn: Function, timeout?: number) {
97+
return function (
98+
specName: string,
99+
fn?: (done: DoneFn) => void | PromiseLike<void>,
100+
timeout?: number,
101+
): Spec {
90102
if (!fn) {
103+
// @ts-expect-error: missing fn arg is handled by originalFn
91104
const spec = originalFn.call(env, specName);
92105
spec.pend('not implemented');
93106
return spec;
@@ -109,7 +122,7 @@ function promisifyIt(
109122

110123
const asyncJestTest = function (done: DoneFn) {
111124
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
112-
const returnValue = wrappedFn.call({});
125+
const returnValue = wrappedFn.call({}, doneFnNoop);
113126

114127
if (isPromise(returnValue)) {
115128
returnValue.then(done.bind(null, null), (error: Error) => {

packages/jest-jasmine2/src/jestExpect.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ import {
1414
toThrowErrorMatchingInlineSnapshot,
1515
toThrowErrorMatchingSnapshot,
1616
} from 'jest-snapshot';
17-
import type {Jasmine, RawMatcherFn} from './types';
17+
import type {Jasmine, JasmineMatchersObject, RawMatcherFn} from './types';
1818

1919
declare const global: Global.Global;
2020

21-
type JasmineMatcher = {
22-
(matchersUtil: any, context: any): JasmineMatcher;
23-
compare: () => RawMatcherFn;
24-
negativeCompare: () => RawMatcherFn;
25-
};
26-
27-
type JasmineMatchersObject = {[id: string]: JasmineMatcher};
28-
2921
export default (config: {expand: boolean}): void => {
3022
global.expect = expect;
3123
expect.setState({expand: config.expand});

packages/jest-jasmine2/src/queueRunner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ export type Options = {
2020
userContext: any;
2121
};
2222

23+
export interface DoneFn {
24+
(error?: any): void;
25+
fail: (error: Error) => void;
26+
}
27+
2328
export type QueueableFn = {
24-
fn: (done: (error?: any) => void) => void;
29+
fn: (done: DoneFn) => void;
2530
timeout?: () => number;
2631
initError?: Error;
2732
};

packages/jest-jasmine2/src/types.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import type {default as Suite, SuiteResult} from './jasmine/Suite';
2020
import type SpyStrategy from './jasmine/SpyStrategy';
2121
import type CallTracker from './jasmine/CallTracker';
2222

23+
export type SpecDefinitionsFn = () => void;
24+
2325
export interface AssertionErrorWithStack extends AssertionError {
2426
stack: string;
2527
}
@@ -64,11 +66,21 @@ export interface Spy extends Record<string, any> {
6466
restoreObjectToOriginalState?: () => void;
6567
}
6668

69+
type JasmineMatcher = {
70+
(matchersUtil: unknown, context: unknown): JasmineMatcher;
71+
compare: () => RawMatcherFn;
72+
negativeCompare: () => RawMatcherFn;
73+
};
74+
75+
export type JasmineMatchersObject = {[id: string]: JasmineMatcher};
76+
6777
export type Jasmine = {
6878
_DEFAULT_TIMEOUT_INTERVAL: number;
6979
DEFAULT_TIMEOUT_INTERVAL: number;
7080
currentEnv_: ReturnType<typeof Env>['prototype'];
71-
getEnv: (options?: object) => ReturnType<typeof Env>['prototype'];
81+
getEnv: (
82+
options?: Record<string, unknown>,
83+
) => ReturnType<typeof Env>['prototype'];
7284
createSpy: typeof createSpy;
7385
Env: ReturnType<typeof Env>;
7486
JsApiReporter: typeof JsApiReporter;
@@ -79,7 +91,7 @@ export type Jasmine = {
7991
Timer: typeof Timer;
8092
version: string;
8193
testPath: Config.Path;
82-
addMatchers: Function;
94+
addMatchers: (matchers: JasmineMatchersObject) => void;
8395
} & typeof expect &
8496
NodeJS.Global;
8597

0 commit comments

Comments
 (0)