diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 3a5060d6..61fc9dfa 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -12,6 +12,7 @@ on: jobs: build: runs-on: ${{ matrix.os }} + timeout-minutes: 5 strategy: matrix: node-version: [12.x, 16.x, 18.x] @@ -24,6 +25,7 @@ jobs: uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} + - run: npm install -g npm@latest - run: npm install - run: npm run build --if-present - run: npm run test-ci diff --git a/package.json b/package.json index 201204e4..f82a4641 100644 --- a/package.json +++ b/package.json @@ -54,16 +54,11 @@ "dependencies": { "resolvewithplus": "^0.8.4" }, - "devDependencies": { - "run-script-os": "^1.1.6" - }, "scripts": { - "mini:default": "npx esbuild ./src/*js --minify --allow-overwrite --outdir=src", - "mini:win32": "cd src && forfiles /m \"*.js\" /c \"cmd /c npx esbuild @file --minify --allow-overwrite --outfile=@file\"", - "mini": "run-script-os", - "test-ci": "npm run mini && npm run test", - "prepublishOnly": "npm run test-ci; npm pkg delete scripts devDependencies", + "test": "cd tests && npm run test:all", "lint": "npx eslint . --ext .js,.mjs", - "test": "cd tests && npm install && npm test" + "test-ci": "cd tests && npm run install:all && npm run test:all-ci", + "mini:pkg": "npm pkg delete scripts devDependencies", + "prepublishOnly": "npm run lint && npm run test-ci && npm run mini:pkg" } } diff --git a/src/esmock.js b/src/esmock.js index 2ee77735..68fe328d 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -9,7 +9,7 @@ import { } from './esmockCache.js'; const esmock = async (modulePath, mockDefs, globalDefs, opt = {}, err) => { - const calleePath = (err || new Error()).stack.split('\n')[2] + const calleePath = (err || new Error).stack.split('\n')[2] .replace(/^.*file:\/\//, '') // rm every before filepath .replace(/:[\d]*:[\d]*.*$/, '') // rm line and row number .replace(/^.*:/, '') // rm windows-style drive location @@ -30,10 +30,10 @@ const esmock = async (modulePath, mockDefs, globalDefs, opt = {}, err) => { }; esmock.px = async (modulePath, mockDefs, globalDefs) => ( - esmock(modulePath, mockDefs, globalDefs, { partial : true }, new Error())); + esmock(modulePath, mockDefs, globalDefs, { partial : true }, new Error)); esmock.p = async (modulePath, mockDefs, globalDefs) => ( - esmock(modulePath, mockDefs, globalDefs, { purge : false }, new Error())); + esmock(modulePath, mockDefs, globalDefs, { purge : false }, new Error)); esmock.purge = mockModule => { if (mockModule && /object|function/.test(typeof mockModule) diff --git a/tests/local/babelGeneratedDoubleDefault/babelGeneratedDoubleDefault.js b/tests/local/babelGeneratedDoubleDefault/babelGeneratedDoubleDefault.js new file mode 100644 index 00000000..0be92c9e --- /dev/null +++ b/tests/local/babelGeneratedDoubleDefault/babelGeneratedDoubleDefault.js @@ -0,0 +1,19 @@ +// this undesirable output was commonly generated by babel w browser-breaking +// defineProperty and default.default definitions + +/* eslint-disable */ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports["default"] = void 0; + +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +var _default = function _default() { + return 'unmocked default.default'; +}; + +exports["default"] = _default; +/* eslint-enable */ diff --git a/tests/local/babelGeneratedDoubleDefault/package.json b/tests/local/babelGeneratedDoubleDefault/package.json new file mode 100644 index 00000000..7da14565 --- /dev/null +++ b/tests/local/babelGeneratedDoubleDefault/package.json @@ -0,0 +1,4 @@ +{ + "description": "problematic cjs file type generated by babel", + "main": "./babelGeneratedDoubleDefault.js" +} diff --git a/tests/local/mainUtil.js b/tests/local/mainUtil.js index 6cfc43bc..7631fe6e 100644 --- a/tests/local/mainUtil.js +++ b/tests/local/mainUtil.js @@ -1,4 +1,5 @@ import formurlencoded from 'form-urlencoded'; +import babelGeneratedDoubleDefault from 'babelGeneratedDoubleDefault'; import { mainUtilNamedExportOne, mainUtilNamedExportTwo @@ -6,12 +7,15 @@ import { const mainUtil = () => 'mainUtil'; -const createString = () => formurlencoded.default({ +const createString = () => formurlencoded({ mainUtil : 'a string', mainUtilNamedExportOneValue : mainUtilNamedExportOne(), mainUtilNamedExportTwoValue : mainUtilNamedExportTwo() }); +const callBabelGeneratedDoubleDefault = o => ( + babelGeneratedDoubleDefault.default(o)); + const causeRuntimeError = () => mainUtil()(); export default mainUtil; @@ -19,5 +23,6 @@ export default mainUtil; export { mainUtil, createString, - causeRuntimeError + causeRuntimeError, + callBabelGeneratedDoubleDefault }; diff --git a/tests/package.json b/tests/package.json index 2ff0f87a..32cc9cd0 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,5 +1,4 @@ { - "name": "esmock-tests", "type": "module", "license": "ISC", "description": "various esmock unit tests", @@ -7,27 +6,32 @@ "type": "git", "url": "https://github.com/iambumblehead/esmock.git" }, + "main": "package.json.esmock.export.js", "dependencies": { - "esmock": "file:..", - "ts-node": "^10.9.1", - "uvu": "0.5.3", - "ava": "^4.0.1", "eslint": "^8.12.0", - "ts-node": "^10.9.1", - "form-urlencoded": "4.2.1", + "sinon": "^12.0.1", + "form-urlencoded": "^6.0.7", "run-script-os": "^1.1.6", - "sinon": "^12.0.1" + "check-node-version": "^4.2.1", + "npm-run-all": "^4.1.5", + "babelGeneratedDoubleDefault": "file:./local/babelGeneratedDoubleDefault" }, "scripts": { - "test-node-esbuildts": "node --loader=@esbuild-kit/esm-loader --loader=esmock --test ./tests-nodets/*ts", - "test-ava": "npx ava --node-arguments=\"--loader=esmock\" ./tests-ava/*.spec.js", - "test-uvu": "node --loader=esmock ./node_modules/uvu/bin.js ./tests-uvu/", - "test-node": "node --no-warnings --loader=esmock --test ./tests-node/", - "test-node-ts": "node --loader=ts-node/esm --loader=esmock --test ./tests-tsnode/*ts", - "test-node18": "npm run test-node && npm run test-node-ts", - "test-nodeis18": "if (node -v | grep v18); then npm run test-node18; fi;", - "test:default": "npm run test-nodeis18 && npm run test-ava && npm run test-uvu", - "test:windows": "npm run test-ava && npm run test-uvu", - "test": "npm install && run-script-os" + "mini:default": "cd .. && npx esbuild ./src/*js --minify --allow-overwrite --outdir=src", + "mini:win32": "cd .. && cd src && forfiles /m \"*.js\" /c \"cmd /c npx esbuild @file --minify --allow-overwrite --outfile=@file\"", + "mini": "run-script-os", + "isnode18": "check-node-version --node 18", + "install:esmock": "cd .. && npm install", + "install:test-ava": "cd tests-ava && npm install", + "install:test-uvu": "cd tests-uvu && npm install", + "install:test-node": "cd tests-node && npm install", + "install:test-nodets": "cd tests-nodets && npm install", + "install:all": "npm install && npm-run-all install:test*", + "test:test-ava": "cd tests-ava && npm test", + "test:test-uvu": "cd tests-uvu && npm test", + "test:test-node": "npm run isnode18 && cd tests-node && npm test || true", + "test:test-nodets": "npm run isnode18 && cd tests-nodets && npm test || true", + "test:all": "npm-run-all test:test*", + "test:all-ci": "npm run mini && npm run test:all" } } diff --git a/tests/package.json.esmock.export.js b/tests/package.json.esmock.export.js new file mode 100644 index 00000000..24f5b8e0 --- /dev/null +++ b/tests/package.json.esmock.export.js @@ -0,0 +1,35 @@ +export {default, load, resolve, getSource} from '../src/esmockLoader.js'; + +// this file is used in tandem with two other things, +// +// 1. a "main" definition in ./package.json, +// ``` +// { +// "main": "tests.esmock.export.js" +// } +// ``` +// +// 2. an "esmock" definition in child ./tests-ava/package.json +// ``` +// { +// "dependencies": { +// "esmock": "file:.." +// } +// } +// ``` +// +// together, these allow child folder tests to, +// * import "esmock" rather than "../../src/esmock.js" +// * use "--loader=esmock" rather than "../../src/esmockLoader.js" +// +// Previously, there existed only one test folder and package.json, +// and during that time the package.json only needed to define +// the esmock dependency as a local file eg "esmock": "file:.." +// +// When subdirectories were introduced, "esmock": "file:../.." did not +// work with "--loader=esmock". The loader ignored the longer path and +// threw an error message, creating need for this file. +// +// Error [ERR_MODULE_NOT_FOUND]: Cannot find package \ +// '/root/esmock/tests/tests-ava/node_modules/esmock/' \ +// imported from /root/esmock/tests/tests-ava/ diff --git a/tests/tests-ava/package.json b/tests/tests-ava/package.json new file mode 100644 index 00000000..33cd885e --- /dev/null +++ b/tests/tests-ava/package.json @@ -0,0 +1,19 @@ +{ + "type": "module", + "description": "esmock unit tests, ava", + "repository": { + "type": "git", + "url": "https://github.com/iambumblehead/esmock.git" + }, + "dependencies": { + "ava": "^4.3.1", + "esmock": "file:..", + "sinon": "file:../node_modules/sinon", + "eslint": "file:../node_modules/eslint", + "form-urlencoded": "file:../node_modules/form-urlencoded", + "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" + }, + "scripts": { + "test": "ava --node-arguments=\"--loader=esmock\"" + } +} diff --git a/tests/tests-ava/esmock.ava.only.spec.js b/tests/tests-ava/spec/esmock.ava.only.spec.js similarity index 63% rename from tests/tests-ava/esmock.ava.only.spec.js rename to tests/tests-ava/spec/esmock.ava.only.spec.js index 8f599e85..e1b91149 100644 --- a/tests/tests-ava/esmock.ava.only.spec.js +++ b/tests/tests-ava/spec/esmock.ava.only.spec.js @@ -1,10 +1,12 @@ import test from 'ava'; -import esmock from '../../src/esmock.js'; +// import esmock from '../../../src/esmock.js'; +import esmock from 'esmock'; // this error can occur when sources do not define 'esmockloader' // on 'global' but use a process linked variable instead test.only('should not error when esmock used with ava.only', async t => { - await esmock('../local/mainUtil.js', { +// console.log( 'is?', global.esmockloader ); + await esmock('../../local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode' }); diff --git a/tests/tests-ava/esmock.ava.spec.js b/tests/tests-ava/spec/esmock.ava.spec.js similarity index 71% rename from tests/tests-ava/esmock.ava.spec.js rename to tests/tests-ava/spec/esmock.ava.spec.js index 20c4318d..912a98d4 100644 --- a/tests/tests-ava/esmock.ava.spec.js +++ b/tests/tests-ava/spec/esmock.ava.spec.js @@ -1,9 +1,9 @@ import test from 'ava'; -import esmock from '../../src/esmock.js'; +import esmock from 'esmock'; import sinon from 'sinon'; test('should return un-mocked file', async t => { - const main = await esmock('../local/main.js'); + const main = await esmock('../../local/main.js'); const mainqs = [ 'a+string', 'mainUtilNamedExportOneValue=namedExportOne', @@ -14,8 +14,8 @@ test('should return un-mocked file', async t => { }); test('should mock a local file', async t => { - const main = await esmock.px('../local/main.js', { - '../local/mainUtil.js' : { + const main = await esmock.px('../../local/main.js', { + '../../local/mainUtil.js' : { createString : () => 'test string' } }); @@ -33,27 +33,35 @@ test.serial('should throw error if !esmockloader', async t => { }); test('should throw error if local file not found', async t => { - await t.throwsAsync(() => esmock('../local/not/found.js', { - '../local/mainUtil.js' : { + const err = await t.throwsAsync(() => esmock('../../local/not/found.js', { + '../../local/mainUtil.js' : { createString : () => 'test string' } }), { - message : 'modulePath not found: "../local/not/found.js"' + instanceOf : Error, + name : 'Error' }); + + t.true(err.message.startsWith( + 'modulePath not found: "../../local/not/found.js"')); }); test('should throw error if local definition file not found', async t => { - await t.throwsAsync(() => esmock('../local/not/found.js', { - '../local/not/found.js' : { + const err = await t.throwsAsync(() => esmock('../../local/not/found.js', { + '../../local/not/found.js' : { createString : () => 'test string' } }), { - message : /not a valid path: \"..\/local\/not\/found.js\" \(used by/ + instanceOf : Error, + name : 'Error' }); + + t.true(err.message.startsWith( + 'not a valid path: "../../local/not/found.js"')); }); test('should mock a module', async t => { - const main = await esmock('../local/mainUtil.js', { + const main = await esmock('../../local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode' }); @@ -62,8 +70,8 @@ test('should mock a module', async t => { }); test('should mock a module, globally', async t => { - const main = await esmock('../local/main.js', { - '../local/mainUtilNamedExports.js' : { + const main = await esmock('../../local/main.js', { + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : 'mocked' } }, { @@ -85,8 +93,8 @@ test('should mock a module, globally', async t => { }); test('should purge local and global mocks', async t => { - await esmock('../local/main.js', { - '../local/mainUtilNamedExports.js' : { + await esmock('../../local/main.js', { + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : 'mocked' } }, { @@ -110,13 +118,13 @@ test('should purge local and global mocks', async t => { }); test('should mock a module, many times differently', async t => { - const mainfoo = await esmock('../local/mainUtil.js', { + const mainfoo = await esmock('../../local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode foo' }); - const mainbar = await esmock('../local/mainUtil.js', { + const mainbar = await esmock('../../local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode bar' }); - const mainbaz = await esmock('../local/mainUtil.js', { + const mainbaz = await esmock('../../local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode baz' }); t.is(typeof mainfoo, 'function'); @@ -126,7 +134,7 @@ test('should mock a module, many times differently', async t => { }); test('should return un-mocked file (again)', async t => { - const main = await esmock('../local/main.js'); + const main = await esmock('../../local/main.js'); const mainqs = [ 'a+string', 'mainUtilNamedExportOneValue=namedExportOne', @@ -137,8 +145,8 @@ test('should return un-mocked file (again)', async t => { }); test('should mock local file', async t => { - const mainUtil = await esmock.px('../local/mainUtil.js', { - '../local/mainUtilNamedExports.js' : { + const mainUtil = await esmock.px('../../local/mainUtil.js', { + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : () => 'foobar' } }); @@ -153,9 +161,9 @@ test('should mock local file', async t => { }); test('should mock module and local file at the same time', async t => { - const mainUtil = await esmock.px('../local/mainUtil.js', { + const mainUtil = await esmock.px('../../local/mainUtil.js', { 'form-urlencoded' : o => JSON.stringify(o), - '../local/mainUtilNamedExports.js' : { + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : () => 'foobar' } }); @@ -168,24 +176,20 @@ test('should mock module and local file at the same time', async t => { }); test('__esModule definition, inconsequential', async t => { - const mainUtil = await esmock.px('../local/mainUtil.js', { - 'form-urlencoded' : o => JSON.stringify(o), - '../local/mainUtilNamedExports.js' : { + const mainUtil = await esmock.px('../../local/mainUtil.js', { + 'babelGeneratedDoubleDefault' : o => o, + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : () => 'foobar', __esModule : true } }); - t.is(mainUtil.createString(), JSON.stringify({ - mainUtil : 'a string', - mainUtilNamedExportOneValue : 'foobar', - mainUtilNamedExportTwoValue : 'namedExportTwo' - })); + t.is(mainUtil.callBabelGeneratedDoubleDefault('mocked'), 'mocked'); }); test('should work well with sinon', async t => { - const mainUtil = await esmock.px('../local/mainUtil.js', { - '../local/mainUtilNamedExports.js' : { + const mainUtil = await esmock.px('../../local/mainUtil.js', { + '../../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : sinon.stub().returns('foobar') } }); @@ -198,24 +202,24 @@ test('should work well with sinon', async t => { }); test('should mock an mjs file', async t => { - const main = await esmock('../local/usesmjsModule.js', { - '../local/exampleMJS.mjs' : () => 'first mocked' + const main = await esmock('../../local/usesmjsModule.js', { + '../../local/exampleMJS.mjs' : () => 'first mocked' }); t.is(main.verifyImportedMock(), 'first mocked'); }); test('should mock an mjs file, again', async t => { - const main = await esmock('../local/usesmjsModule.js', { - '../local/exampleMJS.mjs' : () => 'second mocked' + const main = await esmock('../../local/usesmjsModule.js', { + '../../local/exampleMJS.mjs' : () => 'second mocked' }); t.is(main.verifyImportedMock(), 'second mocked'); }); test('should mock an exported constant values', async t => { - const main = await esmock('../local/usesmjsModule.js', { - '../local/env.js' : { + const main = await esmock('../../local/usesmjsModule.js', { + '../../local/env.js' : { TESTCONSTANT : 'hello world' } }); @@ -224,7 +228,7 @@ test('should mock an exported constant values', async t => { }); test('should mock core module', async t => { - const usesCoreModule = await esmock('../local/usesCoreModule.js', { + const usesCoreModule = await esmock('../../local/usesCoreModule.js', { fs : { existsSync : () => true, readFileSync : filepath => filepath === 'checkfilepath.js' @@ -237,8 +241,8 @@ test('should mock core module', async t => { }); test('should apply third parameter "global" definitions', async t => { - const main = await esmock.px('../local/main.js', { - '../local/mainUtil.js' : { + const main = await esmock.px('../../local/main.js', { + '../../local/mainUtil.js' : { exportedFunction : () => 'foobar' } }, { @@ -254,7 +258,7 @@ test('should apply third parameter "global" definitions', async t => { }); test('returns spread-imported [object Module] default export', async t => { - const main = await esmock('../local/usesObjectModule.js', { + const main = await esmock('../../local/usesObjectModule.js', { fs : { exportedFunction : () => 'foobar' } @@ -264,7 +268,7 @@ test('returns spread-imported [object Module] default export', async t => { }); test('mocks inline `async import("name")`', async t => { - const writeJSConfigFile = await esmock.p('../local/usesInlineImport.mjs', { + const writeJSConfigFile = await esmock.p('../../local/usesInlineImport.mjs', { eslint : { ESLint : function (...o) { this.stringify = () => JSON.stringify(...o); @@ -295,7 +299,7 @@ test('mocks inline `async import("name")`', async t => { }); test('should have small querystring in stacktrace filename', async t => { - const { causeRuntimeError } = await esmock('../local/mainUtil.js'); + const { causeRuntimeError } = await esmock('../../local/mainUtil.js'); try { causeRuntimeError(); @@ -309,8 +313,8 @@ test('should have small querystring in stacktrace filename', async t => { test('should have small querystring in stacktrace filename, deep', async t => { const { causeRuntimeErrorFromImportedFile - } = await esmock.px('../local/main.js', {}, { - '../local/mainUtil.js' : { + } = await esmock.px('../../local/main.js', {}, { + '../../local/mainUtil.js' : { causeRuntimeError : () => { t.nonexistantmethod(); } @@ -330,8 +334,8 @@ test('should have small querystring in stacktrace filename, deep', async t => { test('should have small querystring in stacktrace filename, deep2', async t => { const causeDeepErrorParent = - await esmock.px('../local/causeDeepErrorParent.js', {}, { - '../local/causeDeepErrorGrandChild.js' : { + await esmock.px('../../local/causeDeepErrorParent.js', {}, { + '../../local/causeDeepErrorGrandChild.js' : { what : 'now' } }); @@ -352,23 +356,23 @@ test('should have small querystring in stacktrace filename, deep2', async t => { }); test('should merge "default" value, when safe', async t => { - const main = await esmock('../local/main.js'); + const main = await esmock('../../local/main.js'); t.is(main(), main.default()); - const mockMainA = await esmock('../local/exportsMain.js', { - '../local/main.js' : () => 'mocked main' + const mockMainA = await esmock('../../local/exportsMain.js', { + '../../local/main.js' : () => 'mocked main' }); - const mockMainB = await esmock('../local/exportsMain.js', { - '../local/main.js' : { default : () => 'mocked main' } + const mockMainB = await esmock('../../local/exportsMain.js', { + '../../local/main.js' : { default : () => 'mocked main' } }); t.is(mockMainA(), mockMainB()); }); test('should not error when mocked file has space in path', async t => { - const main = await esmock('../local/main.js', { - '../local/space in path/wild-file.js' : { + const main = await esmock('../../local/main.js', { + '../../local/space in path/wild-file.js' : { default : 'tamed' } }); diff --git a/tests/tests-node/package.json b/tests/tests-node/package.json new file mode 100644 index 00000000..0abfe517 --- /dev/null +++ b/tests/tests-node/package.json @@ -0,0 +1,18 @@ +{ + "type": "module", + "description": "esmock unit tests, node native runner", + "repository": { + "type": "git", + "url": "https://github.com/iambumblehead/esmock.git" + }, + "dependencies": { + "esmock": "file:..", + "sinon": "file:../node_modules/sinon", + "eslint": "file:../node_modules/eslint", + "form-urlencoded": "file:../node_modules/form-urlencoded", + "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" + }, + "scripts": { + "test": "node --loader=esmock --test" + } +} diff --git a/tests/tests-tsnode/esmock.node-ts.test.ts b/tests/tests-nodets/esmock.node-ts.test.ts similarity index 88% rename from tests/tests-tsnode/esmock.node-ts.test.ts rename to tests/tests-nodets/esmock.node-ts.test.ts index 8adb1ff4..4c938782 100644 --- a/tests/tests-tsnode/esmock.node-ts.test.ts +++ b/tests/tests-nodets/esmock.node-ts.test.ts @@ -1,6 +1,6 @@ import test from 'node:test'; import assert from 'assert'; -import esmock from '../../src/esmock.js'; +import esmock from 'esmock'; test('should mock ts when using node-ts', { only : true }, async () => { const main = await esmock('../local/main.ts', { diff --git a/tests/tests-nodets/package.json b/tests/tests-nodets/package.json new file mode 100644 index 00000000..948e3b04 --- /dev/null +++ b/tests/tests-nodets/package.json @@ -0,0 +1,19 @@ +{ + "type": "module", + "description": "esmock unit tests, ts-node/esm with node native runner", + "repository": { + "type": "git", + "url": "https://github.com/iambumblehead/esmock.git" + }, + "dependencies": { + "ts-node": "^10.9.1", + "esmock": "file:..", + "sinon": "file:../node_modules/sinon", + "eslint": "file:../node_modules/eslint", + "form-urlencoded": "file:../node_modules/form-urlencoded", + "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" + }, + "scripts": { + "test": "node --loader=ts-node/esm --loader=esmock --test *test.ts" + } +} diff --git a/tests/tsconfig.json b/tests/tests-nodets/tsconfig.json similarity index 100% rename from tests/tsconfig.json rename to tests/tests-nodets/tsconfig.json diff --git a/tests/tests-uvu/esmock.uvu.spec.js b/tests/tests-uvu/esmock.uvu.spec.js index d48c34dd..77e3c8e1 100644 --- a/tests/tests-uvu/esmock.uvu.spec.js +++ b/tests/tests-uvu/esmock.uvu.spec.js @@ -1,8 +1,7 @@ import { test } from 'uvu'; import * as assert from 'uvu/assert'; import sinon from 'sinon'; - -import esmock from '../../src/esmock.js'; +import esmock from 'esmock'; test('should return un-mocked file', async () => { const main = await esmock('../local/main.js'); @@ -179,18 +178,14 @@ test('should mock module and local file at the same time', async () => { test('__esModule definition, inconsequential', async () => { const mainUtil = await esmock.px('../local/mainUtil.js', { - 'form-urlencoded' : o => JSON.stringify(o), + 'babelGeneratedDoubleDefault' : o => o, '../local/mainUtilNamedExports.js' : { mainUtilNamedExportOne : () => 'foobar', __esModule : true } }); - assert.is(mainUtil.createString(), JSON.stringify({ - mainUtil : 'a string', - mainUtilNamedExportOneValue : 'foobar', - mainUtilNamedExportTwoValue : 'namedExportTwo' - })); + assert.is(mainUtil.callBabelGeneratedDoubleDefault('mocked'), 'mocked'); }); test('should work well with sinon', async () => { diff --git a/tests/tests-uvu/package.json b/tests/tests-uvu/package.json new file mode 100644 index 00000000..025fdc77 --- /dev/null +++ b/tests/tests-uvu/package.json @@ -0,0 +1,19 @@ +{ + "type": "module", + "description": "esmock unit tests, uvu", + "repository": { + "type": "git", + "url": "https://github.com/iambumblehead/esmock.git" + }, + "dependencies": { + "uvu": "^0.5.6", + "esmock": "file:..", + "sinon": "file:../node_modules/sinon", + "eslint": "file:../node_modules/eslint", + "form-urlencoded": "file:../node_modules/form-urlencoded", + "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" + }, + "scripts": { + "test": "node --loader=esmock ./node_modules/uvu/bin.js" + } +}