diff --git a/CHANGELOG.md b/CHANGELOG.md index c9764029..f407cd83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.5.1 _Sep.13.2023_ + * [resolve existing ".ts" files,](https://github.com/iambumblehead/esmock/pull/243) rather than ".js" files, when typescript detected, thanks @tpluscode * 2.5.0 _Sep.09.2023_ * [remove duplicate nextLoad call](https://github.com/iambumblehead/esmock/pull/239) * [add support for initialize hook](https://github.com/iambumblehead/esmock/pull/240) diff --git a/package.json b/package.json index 49428876..9a9dde77 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.5.0", + "version": "2.5.1", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests", @@ -60,7 +60,7 @@ "node": ">=14.16.0" }, "dependencies": { - "resolvewithplus": "^2.0.2" + "resolvewithplus": "^2.0.4" }, "devDependencies": { "c8": "^8.0.1", diff --git a/tests/local/main.ts b/tests/local/main-ts.ts similarity index 100% rename from tests/local/main.ts rename to tests/local/main-ts.ts diff --git a/tests/package.json b/tests/package.json index 44c7d030..63c715f9 100644 --- a/tests/package.json +++ b/tests/package.json @@ -43,6 +43,7 @@ "install:test-no-loader": "cd tests-no-loader && npm install", "install:test-nodets": "cd tests-nodets && npm install", "install:test-mocha": "cd tests-mocha && npm install", + "install:test-workspaces": "cd tests-workspaces && npm install", "install:all": "node --version && npm install && npm-run-all install:test*", "test:test-uvu": "cd tests-uvu && npm test", "test:test-mocha": "cd tests-mocha && npm test", @@ -55,6 +56,7 @@ "test:node18-test-nodets": "cd tests-nodets && npm test", "test:node18-test-source-map": "cd tests-source-map && npm test", "test:node18-test-no-loader": "cd tests-no-loader && npm test", + "test:node18-test-workspaces": "cd tests-workspaces && npm test", "test:node18:all": "npm run isnodelt18 || npm-run-all test:node18-test*", "test:all": "npm-run-all test:test* && npm run test:node18:all", "test:all-cover": "c8 --src=../src/* npm run test:all", diff --git a/tests/tests-FAIL-swc/esmock.node-swc.test.ts b/tests/tests-FAIL-swc/esmock.node-swc.test.ts index 0248852f..9b739f08 100644 --- a/tests/tests-FAIL-swc/esmock.node-swc.test.ts +++ b/tests/tests-FAIL-swc/esmock.node-swc.test.ts @@ -7,7 +7,7 @@ import esmock from 'esmock' // when the typescript sytax are removed, tests pass test('should mock ts when using node-ts', async () => { - const main = await esmock('../local/main.ts', { + const main = await esmock('../local/main-ts.ts', { path: { basename: () => 'hellow' } @@ -17,7 +17,7 @@ test('should mock ts when using node-ts', async () => { }) test('should mock pg', async () => { - const main = await esmock('../local/main.ts', { + const main = await esmock('../local/main-ts.ts', { 'pg': { Pool: (config:any) => { return config || 'mocked pool' diff --git a/tests/tests-FAIL-tsx/esmock.node.tsx.test.ts b/tests/tests-FAIL-tsx/esmock.node.tsx.test.ts index 3211030f..ac032abd 100644 --- a/tests/tests-FAIL-tsx/esmock.node.tsx.test.ts +++ b/tests/tests-FAIL-tsx/esmock.node.tsx.test.ts @@ -15,7 +15,7 @@ test('should mock js when using tsx', async () => { // tsx fails :/ https://github.com/esbuild-kit/tsx/issues/264 // // test('should mock ts when using tsx - unknown file extension', async () => { -// const main = await esmock('../local/main.ts', { +// const main = await esmock('../local/main-ts.ts', { // path: { // basename: () => 'hellow' // } diff --git a/tests/tests-nodets/esmock.node-ts.test.ts b/tests/tests-nodets/esmock.node-ts.test.ts index cca5049b..45571d3c 100644 --- a/tests/tests-nodets/esmock.node-ts.test.ts +++ b/tests/tests-nodets/esmock.node-ts.test.ts @@ -3,7 +3,7 @@ import assert from 'assert' import esmock from 'esmock' test('should mock ts when using node-ts', { only: true }, async () => { - const main = await esmock('../local/main.ts', { + const main = await esmock('../local/main-ts.ts', { path: { basename: () => 'hellow' } @@ -26,4 +26,3 @@ test('should mock import global at import tree w/ mixed esm cjs', async () => { assert.equal(consolelog.mock.calls[0].arguments[0], 'foo') assert.equal(consolelog.mock.calls[1].arguments[0], 'foo') }) - diff --git a/tests/tests-tsm/esmock.node-tsm.test.ts b/tests/tests-tsm/esmock.node-tsm.test.ts index 9c8085d9..5d591ba2 100644 --- a/tests/tests-tsm/esmock.node-tsm.test.ts +++ b/tests/tests-tsm/esmock.node-tsm.test.ts @@ -3,7 +3,7 @@ import assert from 'assert' import esmock from 'esmock' test('should mock ts when using node-ts', async () => { - const main = await esmock('../local/main.ts', { + const main = await esmock('../local/main-ts.ts', { path: { basename: () => 'hellow' } @@ -14,7 +14,7 @@ test('should mock ts when using node-ts', async () => { }) test('should mock pg', async () => { - const main = await esmock('../local/main.ts', { + const main = await esmock('../local/main-ts.ts', { 'pg': { Pool: (config:any) => { return config || 'mocked pool' diff --git a/tests/tests-workspaces/package.json b/tests/tests-workspaces/package.json new file mode 100644 index 00000000..3b91407d --- /dev/null +++ b/tests/tests-workspaces/package.json @@ -0,0 +1,14 @@ +{ + "name": "esmock unit tests, workspaces", + "workspaces": [ + "workspaces-js/*", + "workspaces-ts/*" + ], + "scripts": { + "isnodelt20_6": "node -e \"(([mj, mn]) => (+mj < 20 || (+mj === 20 && +mn < 6)))(process.versions.node.split('.')) || process.exit(1)\"", + "test-workspaces-js": "npm --prefix workspaces-js/js-b test", + "test-workspaces-ts": "npm --prefix workspaces-ts/ts-b test", + "test-workspaces": "npm run test-workspaces-js && npm run test-workspaces-ts", + "test": "npm run isnodelt20_6 || npm run test-workspaces" + } +} diff --git a/tests/tests-workspaces/workspaces-js/js-a/index.js b/tests/tests-workspaces/workspaces-js/js-a/index.js new file mode 100644 index 00000000..c970c823 --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-a/index.js @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-workspaces/workspaces-js/js-a/package.json b/tests/tests-workspaces/workspaces-js/js-a/package.json new file mode 100644 index 00000000..17e9f94a --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-a/package.json @@ -0,0 +1,6 @@ +{ + "name": "js-a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-workspaces/workspaces-js/js-b/esmock.js b/tests/tests-workspaces/workspaces-js/js-b/esmock.js new file mode 100644 index 00000000..87ab45dc --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-b/esmock.js @@ -0,0 +1,4 @@ +export * from '../../../../src/esmock.js' +export {default} from '../../../../src/esmock.js' + +console.log('exportngf') diff --git a/tests/tests-workspaces/workspaces-js/js-b/index.js b/tests/tests-workspaces/workspaces-js/js-b/index.js new file mode 100644 index 00000000..cd6f8eba --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-b/index.js @@ -0,0 +1,5 @@ +import {foo} from 'js-a' + +export default function test () { + return foo() +} diff --git a/tests/tests-workspaces/workspaces-js/js-b/package.json b/tests/tests-workspaces/workspaces-js/js-b/package.json new file mode 100644 index 00000000..05296667 --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-b/package.json @@ -0,0 +1,11 @@ +{ + "name": "js-b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --test" + }, + "dependencies": { + "js-a": "^0.0.0" + } +} diff --git a/tests/tests-workspaces/workspaces-js/js-b/test.js b/tests/tests-workspaces/workspaces-js/js-b/test.js new file mode 100644 index 00000000..94d4b7da --- /dev/null +++ b/tests/tests-workspaces/workspaces-js/js-b/test.js @@ -0,0 +1,11 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import esmock from '../../../../src/esmock.js' + +test('works ootb', async () => { + const sut = await esmock('./index.js', { + 'js-a': { foo: () => 'bar' } + }) + + assert.equal(sut(), 'bar') +}) diff --git a/tests/tests-workspaces/workspaces-ts/ts-a/index.js b/tests/tests-workspaces/workspaces-ts/ts-a/index.js new file mode 100644 index 00000000..c970c823 --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-a/index.js @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-workspaces/workspaces-ts/ts-a/package.json b/tests/tests-workspaces/workspaces-ts/ts-a/package.json new file mode 100644 index 00000000..b197b2fc --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-a/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts-a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-workspaces/workspaces-ts/ts-b/index.ts b/tests/tests-workspaces/workspaces-ts/ts-b/index.ts new file mode 100644 index 00000000..426e23a3 --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-b/index.ts @@ -0,0 +1,8 @@ +import {foo} from 'ts-a' +import localfile from './local-file.js' + +export default function test () { + return foo() +} + +export const localfilewrap = () => localfile() diff --git a/tests/tests-workspaces/workspaces-ts/ts-b/local-file.ts b/tests/tests-workspaces/workspaces-ts/ts-b/local-file.ts new file mode 100644 index 00000000..6fd75c5f --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-b/local-file.ts @@ -0,0 +1 @@ +export default () => 'local-value' diff --git a/tests/tests-workspaces/workspaces-ts/ts-b/package.json b/tests/tests-workspaces/workspaces-ts/ts-b/package.json new file mode 100644 index 00000000..0ba00655 --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-b/package.json @@ -0,0 +1,14 @@ +{ + "name": "ts-b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --loader=ts-node/esm --test test.ts" + }, + "dependencies": { + "ts-a": "^0.0.0" + }, + "devDependencies": { + "ts-node": "^10.9.1" + } +} diff --git a/tests/tests-workspaces/workspaces-ts/ts-b/test.ts b/tests/tests-workspaces/workspaces-ts/ts-b/test.ts new file mode 100644 index 00000000..6eb6f968 --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/ts-b/test.ts @@ -0,0 +1,19 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import esmock from '../../../../src/esmock.js' + +test('works ootb', async () => { + const sut = await esmock('./index.js', { + 'ts-a': { foo: () => 'bar' } + }) + + assert.equal(sut(), 'bar') +}) + +test('also mocks local file', async () => { + const { localfilewrap } = await esmock('./index.js', { + './local-file.js': () => 'local-value-mocked' + }) + + assert.equal(localfilewrap(), 'local-value-mocked') +}) diff --git a/tests/tests-workspaces/workspaces-ts/tsconfig.json b/tests/tests-workspaces/workspaces-ts/tsconfig.json new file mode 100644 index 00000000..5b17f4eb --- /dev/null +++ b/tests/tests-workspaces/workspaces-ts/tsconfig.json @@ -0,0 +1,8 @@ +{ + "esm": true, + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "module": "ESNext", + "moduleResolution": "node" + } +}