napi-rs/examples/napi/__tests__/test.framework.js
copilot-swe-agent[bot] 9f16cf5641 Migrate testing library from AVA to node:test
- Migrated all test files in examples/napi-compat-mode to node:test
- Migrated CLI test files to node:test
- Migrated examples/napi test framework to support node:test
- Removed AVA dependencies from package.json files
- Updated test scripts to use node:test
- Updated config to reference node:test instead of AVA

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
2025-10-10 14:19:48 +00:00

189 lines
4.5 KiB
JavaScript

const { bun } = process.versions
/**@type {import('ava').TestFn} */
let testRunner
if (bun) {
const { test, expect, afterAll, afterEach, beforeAll, beforeEach } =
await import('./bun-test.js')
const testContext = {
is: (actual, expected) => {
expect(actual).toEqual(expected)
},
not: (actual, expected) => {
expect(actual).not.toEqual(expected)
},
deepEqual: (actual, expected) => {
expect(actual).toEqual(expected)
},
throws: (fn, expected) => {
if (expected) {
expect(fn).toThrow(expected)
} else {
expect(fn).toThrow()
}
},
notThrows: (fn, expected) => {
if (expected) {
expect(fn).not.toThrow(expected)
} else {
expect(fn).not.toThrow()
}
},
throwsAsync: async (fn, expected) => {
if (expected) {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).toThrow(expected)
} else {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).toThrow()
}
},
notThrowsAsync: async (fn, expected) => {
if (expected) {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).not.toThrow(expected)
} else {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).not.toThrow()
}
},
true: (actual, message) => {
expect(actual).toBe(true, message)
},
false: (actual, message) => {
expect(actual).toBe(false, message)
},
pass: () => {
expect(true).toBe(true)
},
fail: () => {
expect(true).toBe(false)
},
regex: (actual, expected) => {
expect(actual).toMatch(expected)
},
snapshot: (..._args) => {
// TODO: Ignore snapshots test at this moment
},
}
testRunner = (title, spec) => {
test(title, async () => {
await Promise.resolve(spec(testContext))
})
}
testRunner.skip = (label, fn) => {
test.skip(label, () => {
fn(testContext)
})
}
testRunner.after = (fn) => {
afterAll(fn)
}
testRunner.before = (fn) => {
beforeAll(fn)
}
testRunner.afterEach = (fn) => {
afterEach(fn)
}
testRunner.beforeEach = (fn) => {
beforeEach(fn)
}
} else {
const { test, expect, afterAll, afterEach, beforeAll, beforeEach } =
await import('./node-test.js')
const testContext = {
is: (actual, expected) => {
expect(actual).toEqual(expected)
},
not: (actual, expected) => {
expect(actual).not.toEqual(expected)
},
deepEqual: (actual, expected) => {
expect(actual).toEqual(expected)
},
throws: (fn, expected) => {
if (expected) {
expect(fn).toThrow(expected)
} else {
expect(fn).toThrow()
}
},
notThrows: (fn, expected) => {
if (expected) {
expect(fn).not.toThrow(expected)
} else {
expect(fn).not.toThrow()
}
},
throwsAsync: async (fn, expected) => {
if (expected) {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).toThrow(expected)
} else {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).toThrow()
}
},
notThrowsAsync: async (fn, expected) => {
if (expected) {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).not.toThrow(expected)
} else {
expect(
async () => await (typeof fn === 'function' ? fn() : fn),
).not.toThrow()
}
},
true: (actual, message) => {
expect(actual).toBe(true, message)
},
false: (actual, message) => {
expect(actual).toBe(false, message)
},
pass: () => {
expect(true).toBe(true)
},
fail: () => {
expect(true).toBe(false)
},
regex: (actual, expected) => {
expect(actual).toMatch(expected)
},
snapshot: (..._args) => {
// TODO: Ignore snapshots test at this moment
},
}
testRunner = (title, spec) => {
test(title, async () => {
await Promise.resolve(spec(testContext))
})
}
testRunner.skip = (label, fn) => {
test.skip(label, () => {
fn(testContext)
})
}
testRunner.after = (fn) => {
afterAll(fn)
}
testRunner.before = (fn) => {
beforeAll(fn)
}
testRunner.afterEach = (fn) => {
afterEach(fn)
}
testRunner.beforeEach = (fn) => {
beforeEach(fn)
}
}
export { testRunner as test }