Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test script variants using --experimental-import-meta-resolve #179

Merged
merged 7 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
build:
runs-on: ${{ matrix.os }}
timeout-minutes: 6
timeout-minutes: 12
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
Expand Down
18 changes: 17 additions & 1 deletion src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import {
const isObj = o => typeof o === 'object' && o
const isDefaultIn = o => isObj(o) && 'default' in o
const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/
const isMetaResolve = typeof import.meta.resolve === 'function'
const nextId = ((id = 0) => () => ++id)()
const rootDirSlashRe = /^\//
const protocolFileRe = /^file:/
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved

const asFileURL = p => protocolFileRe.test(p) ? p
: `file:///${p.replace(rootDirSlashRe, '')}`
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved

const esmockModuleMergeDefault = (defLive, def) =>
(isObj(defLive) && isObj(def)) ? Object.assign({}, defLive, def) : def
Expand Down Expand Up @@ -92,14 +98,24 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => {
return mockModuleKey
}

const esmockMetaResolve = async (id, parent) => {
try {
return decodeURI(await import.meta.resolve(id, asFileURL(parent)))
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved
return null
}
}

const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => {
ids = ids || Object.keys(defs)
id = ids[0]
mocks = mocks || []

if (!id) return mocks

const fileURL = resolvewith(id, parent)
const fileURL = isMetaResolve
? await esmockMetaResolve(id, parent)
: resolvewith(id, parent)
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved
if (!fileURL && opt.isModuleNotFoundError !== false)
throw esmockErr.errModuleIdNotFound(id, parent)

Expand Down
8 changes: 6 additions & 2 deletions tests/tests-jest-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
"runner": "jest-light-runner"
},
"scripts": {
"test:default": "NODE_OPTIONS=\"--loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts",
"test:win32": "set \"NODE_OPTIONS=--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts",
"test:default-metaresolve": "NODE_OPTIONS=\"--experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts",
"test:default-nometaresolve": "NODE_OPTIONS=\"--loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts",
"test:default": "npm run test:default-metaresolve && npm run test:default-nometaresolve",
"test:win32-metaresolve": "set \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts",
"test:win32-nometaresolve": "set \"NODE_OPTIONS=--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts",
"test:win32": "npm run test:win32-metaresolve && npm run test:win32-nometaresolve",
"test": "run-script-os"
}
}
8 changes: 6 additions & 2 deletions tests/tests-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
"runner": "jest-light-runner"
},
"scripts": {
"test:default": "NODE_OPTIONS=--loader=esmock jest",
"test:win32": "set NODE_OPTIONS=--loader=esmock && jest",
"test:default-metaresolve": "NODE_OPTIONS=\"--experimental-import-meta-resolve --loader=esmock\" jest",
"test:default-nometaresolve": "NODE_OPTIONS=--loader=esmock jest",
"test:default": "npm run test:default-metaresolve && npm run test:default-nometaresolve",
"test:win32-metaresolve": "set \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=esmock\" && jest",
"test:win32-nometaresolve": "set NODE_OPTIONS=--loader=esmock && jest",
"test:win32": "npm run test:win32-metaresolve && npm run test:win32-metaresolve",
"test": "run-script-os"
}
}
3 changes: 2 additions & 1 deletion tests/tests-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault"
},
"scripts": {
"test": "node --loader=esmock --test"
"test:metaresolve": "node --experimental-import-meta-resolve --loader=esmock --test",
"test": "node --loader=esmock --test && npm run test:metaresolve"
}
}
4 changes: 3 additions & 1 deletion tests/tests-nodets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault"
},
"scripts": {
"test": "node --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts"
"test-metaresolve": "node --experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts",
"test-nometaresolve": "node --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts",
"test": "npm run test-metaresolve && npm run test-nometaresolve"
}
}
4 changes: 3 additions & 1 deletion tests/tests-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"typescript": "^4.8.3"
},
"scripts": {
"test": "rimraf dist && tsc && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava",
"test-metaresolve": "rimraf dist && tsc && cross-env \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=esmock\" NODE_NO_WARNINGS=1 ava",
"test-nometaresolve": "rimraf dist && tsc && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava",
"test": "npm run test-metaresolve && npm run test-nometaresolve",
"test-no-maps": "rimraf dist && tsc --sourceMap false && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava"
},
"ava": {
Expand Down
4 changes: 3 additions & 1 deletion tests/tests-tsm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault"
},
"scripts": {
"test": "node --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts"
"test:metaresolve": "node --experimental-import-meta-resolve --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts",
"test:nometaresolve": "node --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts",
"test": "npm run test:metaresolve && npm run test:nometaresolve"
}
}