Skip to content

Commit

Permalink
fix: return this from jest even in non-global mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 9, 2024
1 parent 659fb93 commit cdb4503
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/jest.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import assert from 'node:assert/strict'

const registry = new Set()

function applyAll(method) {
assert(['mockClear', 'mockReset', 'mockRestore'].includes(method))
for (const obj of registry) obj[method]()
}
const applyAllWrap = (method) =>
function () {
assert(['mockClear', 'mockReset', 'mockRestore'].includes(method))
for (const obj of registry) obj[method]()
return this
}

export const allMocks = {
mockClear: () => applyAll('mockClear'),
mockReset: () => applyAll('mockReset'),
mockRestore: () => applyAll('mockRestore'),
clearAllMocks: applyAllWrap('mockClear'),
resetAllMocks: applyAllWrap('mockReset'),
restoreAllMocks: applyAllWrap('mockRestore'),
}

// We need parent and property for jest.spyOn and mockfn.mockRestore()
Expand Down
4 changes: 1 addition & 3 deletions src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ it.each = makeEach(it)

const jest = {
fn: (impl) => jestfn(impl), // hide extra arguments
clearAllMocks: () => allMocks.mockClear(),
resetAllMocks: () => allMocks.mockReset(),
restoreAllMocks: () => allMocks.mockRestore(),
...allMocks,
spyOn: (obj, name) => {
assert(obj && name && name in obj && !(name in {}) && !(name in Object.prototype))
const fn = jestfn(obj[name], obj, name)
Expand Down
2 changes: 2 additions & 0 deletions src/jest.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@ export function jestmock(name, mocker) {
}

mock.module(name, { defaultExport: value.default, namedExports: value })

return this
}
28 changes: 15 additions & 13 deletions src/jest.timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const warnOldTimers = () => {
if (!ok) console.warn('Warning: timer mocks are known to be glitchy before Node.js >=20.11.0')
}

export const useRealTimers = () => {
export function useRealTimers() {
mock.timers.reset()
return jest
return this
}

export const useFakeTimers = ({ doNotFake = [], ...rest } = {}) => {
export function useFakeTimers({ doNotFake = [], ...rest } = {}) {
assertHaveTimers()
warnOldTimers()
assert.deepEqual(rest, {}, 'Unsupported options')
Expand All @@ -33,33 +33,35 @@ export const useFakeTimers = ({ doNotFake = [], ...rest } = {}) => {
if (e.code !== 'ERR_INVALID_STATE') throw e
}

return jest
return this
}

export const runAllTimers = () => {
export function runAllTimers() {
assertHaveTimers()
warnOldTimers()
mock.timers.tick(100_000_000_000) // > 3 years
return jest
return this
}

export const runOnlyPendingTimers = () => {
export function runOnlyPendingTimers() {
const noInfiniteLoopBug = major >= 22 || (major === 20 && minor >= 11)
assert(noInfiniteLoopBug, 'runOnlyPendingTimers requires Node.js >=20.11.0')
mock.timers.runAll()
return jest
return this
}

export const advanceTimersByTime = (time) => {
export function advanceTimersByTime(time) {
assertHaveTimers()
warnOldTimers()
mock.timers.tick(time)
return jest
return this
}

export const advanceTimersByTimeAsync = async (time) => jest.advanceTimersByTime(time)
export async function advanceTimersByTimeAsync(time) {
return this.advanceTimersByTime(time)
}

export const setSystemTime = (time) => {
export function setSystemTime(time) {
mock.timers.setTime(+time)
return jest
return this
}

0 comments on commit cdb4503

Please sign in to comment.