diff --git a/.github/workflows/test-pnpm.yml b/.github/workflows/test-pnpm.yml new file mode 100644 index 0000000..58fdc3e --- /dev/null +++ b/.github/workflows/test-pnpm.yml @@ -0,0 +1,28 @@ +name: test-pnpm + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test-pnpm: + runs-on: ${{ matrix.os }} + timeout-minutes: 12 + strategy: + fail-fast: false + matrix: + node-version: [22.x] + os: [ubuntu-latest] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + - uses: pnpm/action-setup@v4 + with: + version: 9 + - run: npm install + - run: npm run build --if-present + - run: npm run test-ci-pnpm diff --git a/CHANGELOG.md b/CHANGELOG.md index d85dbc4..4787952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # changelog - * 2.6.8 _tbd_ + * 2.6.8 _Oct.17.2024_ + * [added pnpm unit-test,](https://github.com/iambumblehead/esmock/pull/315) thanks @darcyrush + * [resolve issue for pnpm](https://github.com/iambumblehead/esmock/pull/315) by escaping '+' char in regexp * [add log utility function](https://github.com/iambumblehead/esmock/pull/314) for debugging loader * [dropped ava and jest](https://github.com/iambumblehead/esmock/pull/314) from test sequence, node v22 --loader issues * 2.6.7 _Jul.16.2024_ diff --git a/package.json b/package.json index 043ba3f..6df7c62 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.6.7", + "version": "2.6.8", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests", @@ -75,6 +75,7 @@ "test:all": "cd tests && npm run test:all", "test:all-ci": "cd tests && npm run test:all-ci", "test": "npm run test:all", + "test-ci-pnpm": "cd tests && cd tests-pnpm && pnpm i && npm run test", "test-ci": "npm run test:install && npm run test:all-ci", "test-cover": "npm run test:install && c8 npm run test:all", "lint": "eslint .", diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 0b16977..84970ca 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -25,9 +25,14 @@ const isnotfoundRe = /isfound=false/ const iscommonjsmoduleRe = /^(commonjs|module)$/ const isstrict3 = /strict=3/ const hashbangRe = /^(#![^\n]*\n)/ + +// escape '+' char, pnpm generates long pathnames serialized with these +const moduleIdEsc = str => + str.indexOf('+') >= 0 ? str.replace(/(?!\\)\+/g, '\\+') : str + // returned regexp will match embedded moduleid w/ treeid const moduleIdReCreate = (moduleid, treeid) => new RegExp( - `.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`) + `.*(${moduleIdEsc(moduleid)}(\\?${treeid}(?:(?!#-#).)*)).*`) // node v12.0-v18.x, global const mockKeys = global.mockKeys = (global.mockKeys || {}) diff --git a/tests/tests-pnpm/package.json b/tests/tests-pnpm/package.json new file mode 100644 index 0000000..68d1960 --- /dev/null +++ b/tests/tests-pnpm/package.json @@ -0,0 +1,30 @@ +{ + "name": "scoped-package-mock", + "type": "module", + "repository": { + "type": "git", + "url": "git+https://github.com/iambumblehead/esmock.git" + }, + "description": "careful using this and rm -rf. pnpm does not support local file install :|", + "scripts": { + "test-tsimp": "TSIMP_PROJECT=./test/tsconfig.json node --import tsimp/import --test-reporter spec --test 'test/example.test.ts'", + "test-tsx": "TSX_TSCONFIG_PATH=./test/tsconfig.json node --import tsx --test-reporter spec --test 'test/example.test.ts'", + "test-tsnode": "TS_NODE_PROJECT=./test/tsconfig.json node --import ./ts-node.register.mjs --test-reporter spec --test 'test/example.test.ts'", + "node_modules-esmock:rm": "rm -r node_modules/esmock", + "node_modules-esmock:link": "ln -s ../../ node_modules/esmock", + "node_modules-esmock": "npm run node_modules-esmock:rm && npm run node_modules-esmock:link", + "test": "npm run node_modules-esmock && npm run test-tsnode" + }, + "dependencies": { + "@nestjs/core": "^10.3.8", + "@nestjs/platform-express": "^10.3.8" + }, + "devDependencies": { + "@types/node": "^20.12.7", + "ts-node": "^10.9.2", + "tsimp": "^2.0.11", + "esmock": "latest", + "tsx": "^4.9.3", + "typescript": "^5.4.5" + } +} diff --git a/tests/tests-pnpm/src/example.ts b/tests/tests-pnpm/src/example.ts new file mode 100644 index 0000000..7419398 --- /dev/null +++ b/tests/tests-pnpm/src/example.ts @@ -0,0 +1,7 @@ +import { NestFactory } from "@nestjs/core" + +export const example = async () => { + const test = await NestFactory.create({} as any) + console.log(test) + return test +} diff --git a/tests/tests-pnpm/src/tsconfig.json b/tests/tests-pnpm/src/tsconfig.json new file mode 100644 index 0000000..0186915 --- /dev/null +++ b/tests/tests-pnpm/src/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "Node16", + "moduleResolution": "Node16", + "declaration": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "forceConsistentCasingInFileNames": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "strict": true, + } +} diff --git a/tests/tests-pnpm/test/example.test.ts b/tests/tests-pnpm/test/example.test.ts new file mode 100644 index 0000000..ca51e60 --- /dev/null +++ b/tests/tests-pnpm/test/example.test.ts @@ -0,0 +1,21 @@ +import { describe, it } from "node:test" +import { equal } from "node:assert/strict" +import esmock from "esmock" + +describe("Example", async () => { + it("Mocks scoped module", async () => { + const { example } = await esmock( + "../src/example.js", + import.meta.url, + { + "@nestjs/core": { + NestFactory: { + create: async () => 'mocked' + } + } + } + ) + + equal(await example(), 'mocked') + }) +}) diff --git a/tests/tests-pnpm/test/tsconfig.json b/tests/tests-pnpm/test/tsconfig.json new file mode 100644 index 0000000..0d4681f --- /dev/null +++ b/tests/tests-pnpm/test/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "Node16", + "moduleResolution": "Node16", + "experimentalDecorators": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + } +} diff --git a/tests/tests-pnpm/ts-node.register.mjs b/tests/tests-pnpm/ts-node.register.mjs new file mode 100644 index 0000000..2dd25be --- /dev/null +++ b/tests/tests-pnpm/ts-node.register.mjs @@ -0,0 +1,6 @@ +// https://github.com/TypeStrong/ts-node/issues/2100 + +import { pathToFileURL } from "node:url" +import { register } from "node:module" + +register("ts-node/esm", pathToFileURL("./"))