diff --git a/tests/tests-workspaces/package.json b/tests/tests-workspaces/package.json new file mode 100644 index 00000000..074e1d1f --- /dev/null +++ b/tests/tests-workspaces/package.json @@ -0,0 +1,8 @@ +{ + "name": "esmock unit tests, workspaces", + "workspaces": ["packages/*"], + "esmock": "file:..", + "scripts": { + "test": "npm --prefix packages/b test" + } +} diff --git a/tests/tests-workspaces/packages/a/index.js b/tests/tests-workspaces/packages/a/index.js new file mode 100644 index 00000000..c970c823 --- /dev/null +++ b/tests/tests-workspaces/packages/a/index.js @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-workspaces/packages/a/package.json b/tests/tests-workspaces/packages/a/package.json new file mode 100644 index 00000000..e9ec9a18 --- /dev/null +++ b/tests/tests-workspaces/packages/a/package.json @@ -0,0 +1,6 @@ +{ + "name": "a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-workspaces/packages/b/index.js b/tests/tests-workspaces/packages/b/index.js new file mode 100644 index 00000000..cd0f9ecb --- /dev/null +++ b/tests/tests-workspaces/packages/b/index.js @@ -0,0 +1,5 @@ +import {foo} from 'a' + +export default function test () { + return foo() +} diff --git a/tests/tests-workspaces/packages/b/package.json b/tests/tests-workspaces/packages/b/package.json new file mode 100644 index 00000000..acc4f3be --- /dev/null +++ b/tests/tests-workspaces/packages/b/package.json @@ -0,0 +1,14 @@ +{ + "name": "b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --loader=esmock --test" + }, + "dependencies": { + "a": "^0.0.0" + }, + "devDependencies": { + "esmock": "^2.3.8" + } +} diff --git a/tests/tests-workspaces/packages/b/test.js b/tests/tests-workspaces/packages/b/test.js new file mode 100644 index 00000000..0d566e4b --- /dev/null +++ b/tests/tests-workspaces/packages/b/test.js @@ -0,0 +1,24 @@ +import esmock from 'esmock' +import module from 'module' +import test from 'node:test' +import assert from 'node:assert/strict' + +test('works ootb', async () => { + const sut = await esmock('./index.js', { + a: { foo: () => 'bar' } + }) + + assert.equal(sut(), 'bar') +}) + +test('works roubdabout way', async () => { + const require = module.createRequire(import.meta.url) + + const sut = await esmock('./index.js', { + [require.resolve('a')]: { + foo: () => 'bar' + } + }) + + assert.equal(sut(), 'bar') +})