Skip to content

Commit

Permalink
Loader refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsograziano committed Feb 14, 2024
1 parent 9fd0f86 commit 3d8fd44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/a02-assertions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
8 changes: 0 additions & 8 deletions src/a02-assertions/verifiers/index.verify.js

This file was deleted.

32 changes: 32 additions & 0 deletions verify/loader.js
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 3d8fd44

Please sign in to comment.