generated from posva/vue-ts-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create and restore spies with other test frameworks (#144)
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
- Loading branch information
Showing
8 changed files
with
142 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { getJestGlobal } from './testers/jest' | ||
import { getSinonGlobal } from './testers/sinon' | ||
import { getVitestGlobal } from './testers/vitest' | ||
|
||
/** | ||
* Creates a spy on a function | ||
* | ||
* @param fn function to spy on | ||
* @returns [spy, mockClear] | ||
*/ | ||
export function createSpy<Fn extends (...args: any[]) => any>( | ||
fn: Fn, | ||
spyFactory?: RouterMockSpyOptions | ||
): [_InferSpyType<Fn>, () => void] { | ||
if (spyFactory) { | ||
const spy = spyFactory.create(fn) | ||
return [spy, () => spyFactory.reset(spy)] | ||
} | ||
|
||
const sinon = getSinonGlobal() | ||
if (sinon) { | ||
const spy = sinon.spy(fn) | ||
return [spy as unknown as _InferSpyType<Fn>, () => spy.resetHistory()] | ||
} | ||
|
||
const jest = getVitestGlobal() || getJestGlobal() | ||
if (jest) { | ||
const spy = jest.fn(fn) | ||
return [spy as unknown as _InferSpyType<Fn>, () => spy.mockClear()] | ||
} | ||
|
||
console.error( | ||
`Couldn't detect a global spy (tried jest and sinon). Make sure to provide a "spy.create" option when creating the router mock.` | ||
) | ||
throw new Error('No Spy Available') | ||
} | ||
|
||
/** | ||
* Options passed to the `spy` option of the `createRouterMock` function | ||
*/ | ||
export interface RouterMockSpyOptions { | ||
/** | ||
* Creates a spy (for example, `create: fn => vi.fn(fn)` with vitest) | ||
*/ | ||
create: (...args: any[]) => any | ||
|
||
/** | ||
* Resets a spy but keeps it active. | ||
*/ | ||
reset: (spy: _InferSpyType) => void | ||
|
||
/** | ||
* Restores the original function given to the spy. | ||
*/ | ||
restore: (spy: _InferSpyType) => void | ||
} | ||
|
||
/** | ||
* Define your own Spy to adapt to your testing framework (jest, peeky, sinon, vitest, etc) | ||
* @beta: still trying out, could change in the future | ||
* | ||
* @example | ||
* ```ts | ||
* import 'vue-router-mock' // Only needed on external d.ts files | ||
* | ||
* declare module 'vue-router-mock' { | ||
* export interface RouterMockSpy<Fn> { | ||
* spy: Sinon.Spy<Parameters<Fn>, ReturnType<Fn>> | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export interface RouterMockSpy< | ||
Fn extends (...args: any[]) => any = (...args: any[]) => any | ||
> { | ||
// cannot be added or it wouldn't be extensible | ||
// spy: any | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type _InferSpyType< | ||
Fn extends (...args: any[]) => any = (...args: any[]) => any | ||
// @ts-ignore: the version with Record<'spy', any> doesn't work... | ||
> = keyof RouterMockSpy<Fn> extends 'spy' ? RouterMockSpy<Fn>['spy'] : Fn | ||
// > = RouterMockSpy<Fn> extends Record<'spy', any> ? RouterMockSpy<Fn>['spy'] : Fn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getJestGlobal() { | ||
return typeof jest !== 'undefined' && jest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { SinonStatic } from 'sinon' | ||
|
||
declare const sinon: SinonStatic | undefined | ||
|
||
export function getSinonGlobal() { | ||
return typeof sinon !== 'undefined' && sinon | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// cannot import the actual typ | ||
declare const vi: typeof jest | ||
|
||
export function getVitestGlobal() { | ||
return typeof vi !== 'undefined' && vi | ||
} |