Skip to content

Commit

Permalink
Merge pull request #80 from iambumblehead/mv-tests-to-subfolders-with…
Browse files Browse the repository at this point in the history
…-own-packagejson

Mv tests to subfolders with own packagejson
  • Loading branch information
iambumblehead authored Jul 23, 2022
2 parents 908907d + a554762 commit 137383a
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
jobs:
build:
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
matrix:
node-version: [12.x, 16.x, 18.x]
Expand All @@ -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
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 3 additions & 3 deletions src/esmock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 */
4 changes: 4 additions & 0 deletions tests/local/babelGeneratedDoubleDefault/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "problematic cjs file type generated by babel",
"main": "./babelGeneratedDoubleDefault.js"
}
9 changes: 7 additions & 2 deletions tests/local/mainUtil.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import formurlencoded from 'form-urlencoded';
import babelGeneratedDoubleDefault from 'babelGeneratedDoubleDefault';
import {
mainUtilNamedExportOne,
mainUtilNamedExportTwo
} from './mainUtilNamedExports.js';

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;

export {
mainUtil,
createString,
causeRuntimeError
causeRuntimeError,
callBabelGeneratedDoubleDefault
};
40 changes: 22 additions & 18 deletions tests/package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "esmock-tests",
"type": "module",
"license": "ISC",
"description": "various esmock unit tests",
"repository": {
"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"
}
}
35 changes: 35 additions & 0 deletions tests/package.json.esmock.export.js
Original file line number Diff line number Diff line change
@@ -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/
19 changes: 19 additions & 0 deletions tests/tests-ava/package.json
Original file line number Diff line number Diff line change
@@ -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\""
}
}
Original file line number Diff line number Diff line change
@@ -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'
});

Expand Down
Loading

0 comments on commit 137383a

Please sign in to comment.