diff --git a/CHANGELOG.md b/CHANGELOG.md index f50ca289d9af..2e88fb52389d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[jest-jasmine2]` Convert `PCancelable` to TypeScript ([#10215](https://github.com/facebook/jest/pull/10215)) - `[jest-jasmine2]` Refine typings of `queueRunner` ([#10215](https://github.com/facebook/jest/pull/10215)) +- `[jest-jasmine2]` Remove usage of `Function` type ([#10216](https://github.com/facebook/jest/pull/10216)) - `[jest-resolve]` Improve types ([#10239](https://github.com/facebook/jest/pull/10239)) ### Performance diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index cc2acc3eee6a..92c7e1e4cdee 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -41,7 +41,13 @@ import queueRunner, { import treeProcessor, {TreeNode} from '../treeProcessor'; import isError from '../isError'; import assertionErrorMessage from '../assertionErrorMessage'; -import type {AssertionErrorWithStack, Jasmine, Reporter, Spy} from '../types'; +import type { + AssertionErrorWithStack, + Jasmine, + Reporter, + SpecDefinitionsFn, + Spy, +} from '../types'; import type {default as Spec, SpecResult} from './Spec'; import type Suite from './Suite'; @@ -64,7 +70,10 @@ export default function (j$: Jasmine) { runnablesToRun?: Array, suiteTree?: Suite, ) => Promise; - fdescribe: (description: string, specDefinitions: Function) => Suite; + fdescribe: ( + description: string, + specDefinitions: SpecDefinitionsFn, + ) => Suite; spyOn: ( obj: Record, methodName: string, @@ -78,15 +87,21 @@ export default function (j$: Jasmine) { clearReporters: () => void; addReporter: (reporterToAdd: Reporter) => void; it: (description: string, fn: QueueableFn['fn'], timeout?: number) => Spec; - xdescribe: (description: string, specDefinitions: Function) => Suite; + xdescribe: ( + description: string, + specDefinitions: SpecDefinitionsFn, + ) => Suite; xit: (description: string, fn: QueueableFn['fn'], timeout?: number) => Spec; beforeAll: (beforeAllFunction: QueueableFn['fn'], timeout?: number) => void; todo: () => Spec; provideFallbackReporter: (reporterToAdd: Reporter) => void; allowRespy: (allow: boolean) => void; - describe: (description: string, specDefinitions: Function) => Suite; + describe: ( + description: string, + specDefinitions: SpecDefinitionsFn, + ) => Suite; - constructor(_options?: object) { + constructor(_options?: Record) { let totalSpecsDefined = 0; let catchExceptions = true; @@ -411,13 +426,16 @@ export default function (j$: Jasmine) { return suite; }; - const addSpecsToSuite = (suite: Suite, specDefinitions: Function) => { + const addSpecsToSuite = ( + suite: Suite, + specDefinitions: SpecDefinitionsFn, + ) => { const parentSuite = currentDeclarationSuite; parentSuite.addChild(suite); currentDeclarationSuite = suite; let declarationError: undefined | Error = undefined; - let describeReturnValue: undefined | Error = undefined; + let describeReturnValue: unknown | Error; try { describeReturnValue = specDefinitions.call(suite); } catch (e) { diff --git a/packages/jest-jasmine2/src/jasmine/Spec.ts b/packages/jest-jasmine2/src/jasmine/Spec.ts index 8f0bcdfe5db4..30ea80466a37 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.ts +++ b/packages/jest-jasmine2/src/jasmine/Spec.ts @@ -171,7 +171,7 @@ export default class Spec { } } - execute(onComplete: Function, enabled: boolean) { + execute(onComplete?: () => void, enabled?: boolean) { const self = this; this.onStart(this); @@ -203,7 +203,7 @@ export default class Spec { this.currentRun.then(() => complete(true)); - function complete(enabledAgain: boolean) { + function complete(enabledAgain?: boolean) { self.result.status = self.status(enabledAgain); self.resultCallback(self.result); diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index dc1ae4e42682..7bfb21227203 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -30,7 +30,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* eslint-disable sort-keys */ -import type {Jasmine} from '../types'; +import type {Jasmine, SpecDefinitionsFn} from '../types'; import createSpy from './createSpy'; import Env from './Env'; import JsApiReporter from './JsApiReporter'; @@ -45,7 +45,7 @@ export const create = function (createOptions: Record): Jasmine { j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.testTimeout || 5000; - j$.getEnv = function (options?: object) { + j$.getEnv = function (options?: Record) { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options)); //jasmine. singletons in here (setTimeout blah blah). return env; @@ -66,15 +66,15 @@ export const create = function (createOptions: Record): Jasmine { // Interface is a reserved word in strict mode, so can't export it as ESM export const _interface = function (jasmine: Jasmine, env: any) { const jasmineInterface = { - describe(description: string, specDefinitions: Function) { + describe(description: string, specDefinitions: SpecDefinitionsFn) { return env.describe(description, specDefinitions); }, - xdescribe(description: string, specDefinitions: Function) { + xdescribe(description: string, specDefinitions: SpecDefinitionsFn) { return env.xdescribe(description, specDefinitions); }, - fdescribe(description: string, specDefinitions: Function) { + fdescribe(description: string, specDefinitions: SpecDefinitionsFn) { return env.fdescribe(description, specDefinitions); }, diff --git a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts index 0a6768348f7c..b33e399cc304 100644 --- a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts +++ b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts @@ -17,26 +17,30 @@ import throat from 'throat'; import isError from './isError'; import type {Jasmine} from './types'; import type Spec from './jasmine/Spec'; -import type {QueueableFn} from './queueRunner'; +import type {DoneFn, QueueableFn} from './queueRunner'; -interface DoneFn { - (): void; - fail: (error: Error) => void; -} - -function isPromise(obj: any) { +function isPromise(obj: any): obj is PromiseLike { return obj && typeof obj.then === 'function'; } +const doneFnNoop = () => {}; + +doneFnNoop.fail = () => {}; + function promisifyLifeCycleFunction( - originalFn: Function, + originalFn: (beforeAllFunction: QueueableFn['fn'], timeout?: number) => void, env: Jasmine['currentEnv_'], ) { return function ( - fn: Function | (() => Promise) | GeneratorFunction | undefined, + fn: + | ((done: DoneFn) => void | PromiseLike) + | (() => Promise) + | GeneratorFunction + | undefined, timeout?: number, - ) { + ): void { if (!fn) { + // @ts-expect-error: missing fn arg is handled by originalFn return originalFn.call(env); } @@ -59,7 +63,7 @@ function promisifyLifeCycleFunction( // didn't return a promise. const asyncJestLifecycle = function (done: DoneFn) { const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn; - const returnValue = wrappedFn.call({}); + const returnValue = wrappedFn.call({}, doneFnNoop); if (isPromise(returnValue)) { returnValue.then(done.bind(null, null), (error: Error) => { @@ -82,12 +86,21 @@ function promisifyLifeCycleFunction( // Similar to promisifyLifeCycleFunction but throws an error // when the return value is neither a Promise nor `undefined` function promisifyIt( - originalFn: Function, + originalFn: ( + description: string, + fn: QueueableFn['fn'], + timeout?: number, + ) => Spec, env: Jasmine['currentEnv_'], jasmine: Jasmine, ) { - return function (specName: string, fn: Function, timeout?: number) { + return function ( + specName: string, + fn?: (done: DoneFn) => void | PromiseLike, + timeout?: number, + ): Spec { if (!fn) { + // @ts-expect-error: missing fn arg is handled by originalFn const spec = originalFn.call(env, specName); spec.pend('not implemented'); return spec; @@ -109,7 +122,7 @@ function promisifyIt( const asyncJestTest = function (done: DoneFn) { const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn; - const returnValue = wrappedFn.call({}); + const returnValue = wrappedFn.call({}, doneFnNoop); if (isPromise(returnValue)) { returnValue.then(done.bind(null, null), (error: Error) => { diff --git a/packages/jest-jasmine2/src/jestExpect.ts b/packages/jest-jasmine2/src/jestExpect.ts index e0495cb9f0b7..15b988134042 100644 --- a/packages/jest-jasmine2/src/jestExpect.ts +++ b/packages/jest-jasmine2/src/jestExpect.ts @@ -14,18 +14,10 @@ import { toThrowErrorMatchingInlineSnapshot, toThrowErrorMatchingSnapshot, } from 'jest-snapshot'; -import type {Jasmine, RawMatcherFn} from './types'; +import type {Jasmine, JasmineMatchersObject, RawMatcherFn} from './types'; declare const global: Global.Global; -type JasmineMatcher = { - (matchersUtil: any, context: any): JasmineMatcher; - compare: () => RawMatcherFn; - negativeCompare: () => RawMatcherFn; -}; - -type JasmineMatchersObject = {[id: string]: JasmineMatcher}; - export default (config: {expand: boolean}): void => { global.expect = expect; expect.setState({expand: config.expand}); diff --git a/packages/jest-jasmine2/src/queueRunner.ts b/packages/jest-jasmine2/src/queueRunner.ts index 789b618a3824..b411223d3cce 100644 --- a/packages/jest-jasmine2/src/queueRunner.ts +++ b/packages/jest-jasmine2/src/queueRunner.ts @@ -20,8 +20,13 @@ export type Options = { userContext: any; }; +export interface DoneFn { + (error?: any): void; + fail: (error: Error) => void; +} + export type QueueableFn = { - fn: (done: (error?: any) => void) => void; + fn: (done: DoneFn) => void; timeout?: () => number; initError?: Error; }; diff --git a/packages/jest-jasmine2/src/types.ts b/packages/jest-jasmine2/src/types.ts index b97db928ef04..06c96674a5ae 100644 --- a/packages/jest-jasmine2/src/types.ts +++ b/packages/jest-jasmine2/src/types.ts @@ -20,6 +20,8 @@ import type {default as Suite, SuiteResult} from './jasmine/Suite'; import type SpyStrategy from './jasmine/SpyStrategy'; import type CallTracker from './jasmine/CallTracker'; +export type SpecDefinitionsFn = () => void; + export interface AssertionErrorWithStack extends AssertionError { stack: string; } @@ -64,11 +66,21 @@ export interface Spy extends Record { restoreObjectToOriginalState?: () => void; } +type JasmineMatcher = { + (matchersUtil: unknown, context: unknown): JasmineMatcher; + compare: () => RawMatcherFn; + negativeCompare: () => RawMatcherFn; +}; + +export type JasmineMatchersObject = {[id: string]: JasmineMatcher}; + export type Jasmine = { _DEFAULT_TIMEOUT_INTERVAL: number; DEFAULT_TIMEOUT_INTERVAL: number; currentEnv_: ReturnType['prototype']; - getEnv: (options?: object) => ReturnType['prototype']; + getEnv: ( + options?: Record, + ) => ReturnType['prototype']; createSpy: typeof createSpy; Env: ReturnType; JsApiReporter: typeof JsApiReporter; @@ -79,7 +91,7 @@ export type Jasmine = { Timer: typeof Timer; version: string; testPath: Config.Path; - addMatchers: Function; + addMatchers: (matchers: JasmineMatchersObject) => void; } & typeof expect & NodeJS.Global;