-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
chore(jest-jasmine2): remove usage of Function type
#10216
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<unknown> { | ||
| 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 <T>( | ||
| fn: Function | (() => Promise<T>) | GeneratorFunction | undefined, | ||
| fn: | ||
| | ((done: DoneFn) => void | PromiseLike<T>) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be an error to both take a done function and return a promise
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause some major type problems, as everywhere else is expecting only For the record, by unioning the functions TS can't support both of them when doing This can be resolved by using an interface: This then takes us back to where we were before, but this time it's the opposite: TS is complaining
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😞 |
||
| | (() => Promise<T>) | ||
| | 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) { | ||
G-Rath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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<void>, | ||
| 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) => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.