Skip to content

Commit

Permalink
Merge pull request #243 from iambumblehead/add-workspace-tests
Browse files Browse the repository at this point in the history
added workspace test
  • Loading branch information
iambumblehead authored Sep 13, 2023
2 parents 8cb00f1 + 9591b4f commit 0b8888d
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -60,7 +60,7 @@
"node": ">=14.16.0"
},
"dependencies": {
"resolvewithplus": "^2.0.2"
"resolvewithplus": "^2.0.4"
},
"devDependencies": {
"c8": "^8.0.1",
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/tests-FAIL-swc/esmock.node-swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-FAIL-tsx/esmock.node.tsx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
// }
Expand Down
3 changes: 1 addition & 2 deletions tests/tests-nodets/esmock.node-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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')
})

4 changes: 2 additions & 2 deletions tests/tests-tsm/esmock.node-tsm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions tests/tests-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo () {
return 'foo'
}
6 changes: 6 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "js-a",
"version": "0.0.0",
"type": "module",
"main": "index.js"
}
4 changes: 4 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-b/esmock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from '../../../../src/esmock.js'
export {default} from '../../../../src/esmock.js'

console.log('exportngf')
5 changes: 5 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-b/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {foo} from 'js-a'

export default function test () {
return foo()
}
11 changes: 11 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "js-b",
"version": "0.0.0",
"type": "module",
"scripts": {
"test": "node --test"
},
"dependencies": {
"js-a": "^0.0.0"
}
}
11 changes: 11 additions & 0 deletions tests/tests-workspaces/workspaces-js/js-b/test.js
Original file line number Diff line number Diff line change
@@ -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')
})
3 changes: 3 additions & 0 deletions tests/tests-workspaces/workspaces-ts/ts-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo () {
return 'foo'
}
6 changes: 6 additions & 0 deletions tests/tests-workspaces/workspaces-ts/ts-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ts-a",
"version": "0.0.0",
"type": "module",
"main": "index.js"
}
8 changes: 8 additions & 0 deletions tests/tests-workspaces/workspaces-ts/ts-b/index.ts
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions tests/tests-workspaces/workspaces-ts/ts-b/local-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'local-value'
14 changes: 14 additions & 0 deletions tests/tests-workspaces/workspaces-ts/ts-b/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
19 changes: 19 additions & 0 deletions tests/tests-workspaces/workspaces-ts/ts-b/test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
8 changes: 8 additions & 0 deletions tests/tests-workspaces/workspaces-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"esm": true,
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"module": "ESNext",
"moduleResolution": "node"
}
}

0 comments on commit 0b8888d

Please sign in to comment.