Skip to content

Commit

Permalink
fix(esm): add unit test for require module
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamann committed Jan 24, 2024
1 parent 8aa5e3b commit 220d161
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"unit:ts-esm": "cross-env TS_NODE_PROJECT=./test/configs/ts-esm.tsconfig.json FASTIFY_AUTOLOAD_TYPESCRIPT=1 node -r ts-node/register --loader ts-node/esm suite-runner.js \"templates/app-ts-esm/test/**/*.test.ts\"",
"unit:suites": "node should-skip-test-suites.js || npm run all-suites",
"all-suites": "npm run unit:cjs && npm run unit:esm && npm run unit:ts-cjs && npm run unit:ts-esm",
"unit:cli-js-esm": "node suite-runner.js \"test/esm/**/*.test.js\"",
"unit:cli-js": "tap \"test/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"unit:cli-ts": "cross-env TS_NODE_PROJECT=./test/configs/ts-cjs.tsconfig.json tap \"test/**/*.test.ts\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts",
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts && npm run unit:cli-js-esm",
"test:cli-and-typescript": "npm run unit:cli && npm run test:typescript",
"test:typescript": "tsd templates/plugin -t ./../../index.d.ts && tsc --project templates/app-ts/tsconfig.json --noEmit && tsc --project templates/app-ts-esm/tsconfig.json --noEmit"
},
Expand Down
8 changes: 8 additions & 0 deletions test/esm/data/custom-logger.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = {
name: 'Custom Logger',
customLevels: {
test: 99
}
}
3 changes: 3 additions & 0 deletions test/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
16 changes: 16 additions & 0 deletions test/esm/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { requireModule } from '../../util.js'
import { resolve, join } from 'node:path'
import t from 'tap'
import * as url from 'url'

const test = t.test

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

test('requiring a commonjs module works in an esm project', (t) => {
t.plan(1)
const module = requireModule(
resolve(join(__dirname, './data/custom-logger.cjs'))
)
t.strictSame(module, { name: 'Custom Logger', customLevels: { test: 99 } })
})

0 comments on commit 220d161

Please sign in to comment.