diff --git a/src/a02-assertions/package.json b/src/a02-assertions/package.json index b4ba476..533b0f8 100644 --- a/src/a02-assertions/package.json +++ b/src/a02-assertions/package.json @@ -9,7 +9,7 @@ }, "scripts": { "test": "node --test", - "verify": "node --test --loader=./verifiers/loader.js ./verifiers/handler.js" + "verify": "node --test --loader=../../verify/loader.js ./verifiers/handler.js" }, "keywords": [], "author": "", diff --git a/src/a02-assertions/verifiers/index.verify.js b/src/a02-assertions/verifiers/index.verify.js deleted file mode 100644 index 4f1cb31..0000000 --- a/src/a02-assertions/verifiers/index.verify.js +++ /dev/null @@ -1,8 +0,0 @@ -import { - sum as originalSum, - sumAsync as originalSumAsync -} from '../src/index.js' -import { mock } from 'node:test' - -export const sum = mock.fn(originalSum) -export const sumAsync = mock.fn(originalSumAsync) diff --git a/verify/loader.js b/verify/loader.js new file mode 100644 index 0000000..1e1deef --- /dev/null +++ b/verify/loader.js @@ -0,0 +1,32 @@ +import path from 'path' + +async function resolve(url, context, defaultResolve) { + // To avoid circular dependencies + // We suppose that all the verifiers include "verify" in the name + if ( + typeof context.parentURL !== 'undefined' && + context.parentURL.includes('verify') + ) { + return defaultResolve(url, context, defaultResolve) + } + + const modulesToPatch = { + 'node:test': './test.verify.js', + 'node:assert': './assert.verify.js', + // We're always mocking the src in order to spy the calls + '../src/index.js': path.join(process.cwd(), 'verifiers', 'index.verify.js') + } + + if (Object.keys(modulesToPatch).includes(url)) { + return defaultResolve( + new URL(modulesToPatch[url], import.meta.url).href, + context, + defaultResolve + ) + } + + // For all other modules, use the default loader + return defaultResolve(url, context, defaultResolve) +} + +export { resolve }