Skip to content

Commit

Permalink
Test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsograziano committed Feb 14, 2024
1 parent c343abd commit 9fd0f86
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions verify/test.verify.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
import { AsyncLocalStorage } from 'async_hooks'
import {
test as originalTest,
before as originalBefore,
beforeEach as originalBeforeEach,
afterEach as originalAfterEach,
after as originalAfter,
mock as originalMock,
only as originalOnly,
skip as originalSkip,
todo as originalTodo
} from 'node:test'
import t from 'node:test'

const asyncLocalStorage = new AsyncLocalStorage()

export const test = originalMock.fn((...args) => {
export const test = t.mock.fn((...args) => {
asyncLocalStorage.run(new Map([['testName', args[0]]]), () => {
return originalTest(...args)
return t.test(...args)
})
})
export function getCurrentTestName() {
const store = asyncLocalStorage.getStore()
return store ? store.get('testName') : undefined
}

export const before = originalMock.fn((...args) => {
export const before = t.mock.fn((...args) => {
asyncLocalStorage.run(new Map([['hook', 'before']]), () => {
return originalBefore(...args)
return t.before(...args)
})
})

export const beforeEach = originalMock.fn((...args) => {
export const beforeEach = t.mock.fn((...args) => {
asyncLocalStorage.run(new Map([['hook', 'beforeEach']]), () => {
return originalBeforeEach(...args)
return t.beforeEach(...args)
})
})

export const after = originalMock.fn((...args) => {
export const after = t.mock.fn((...args) => {
asyncLocalStorage.run(new Map([['hook', 'after']]), () => {
return originalAfter(...args)
return t.after(...args)
})
})

export const afterEach = originalMock.fn((...args) => {
export const afterEach = t.mock.fn((...args) => {
asyncLocalStorage.run(new Map([['hook', 'afterEach']]), () => {
return originalAfterEach(...args)
return t.afterEach(...args)
})
})

Expand All @@ -52,12 +42,12 @@ export function getHook() {
return store ? store.get('hook') : undefined
}

export const only = originalMock.fn(originalOnly)
export const skip = originalMock.fn(originalSkip)
export const todo = originalMock.fn(originalTodo)
export const only = t.mock.fn(t.only)
export const skip = t.mock.fn(t.skip)
export const todo = t.mock.fn(t.todo)

export const mockCalls = []
export const mock = new Proxy(originalMock, {
export const mock = new Proxy(t.mock, {
get(target, prop) {
const originalMethod = target[prop]

Expand Down

0 comments on commit 9fd0f86

Please sign in to comment.