Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use one regexp to detect loader call #131

Merged
merged 2 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/esmock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import esmockIsLoader from './esmockIsLoader.js'

import {
esmockModuleMock,
esmockModuleImportedPurge,
Expand Down Expand Up @@ -28,7 +30,7 @@ const _esmock = async (argsObj, err) => {
.replace(/^.*:/, '') // rm windows-style drive location
.replace(/.*at [^(]*\(/, '') // rm ' at TestContext.<anonymous> ('

if (!global.esmockloader)
if (!esmockIsLoader())
throw new Error('process must be started with --loader=esmock')

const modulePathKey = await esmockModuleMock(
Expand Down
9 changes: 5 additions & 4 deletions src/esmockIsLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default process.execArgv
.some(args => args.startsWith('--loader=') && args.includes('esmock'))
|| /(?:^|\s)?--(?:experimental-)?loader=(["']*)esmock\1(?:\s|$)/
.test(process.env.NODE_OPTIONS)
const isloaderRe = /--(experimental-)?loader=(["']*)esmock/

export default (pr = process) =>
isloaderRe.test(pr.execArgv) ||
isloaderRe.test(pr.env.NODE_OPTIONS)
3 changes: 0 additions & 3 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import process from 'process'
import esmock from './esmock.js'
import esmockIsLoader from './esmockIsLoader.js'
import urlDummy from './esmockDummy.js'

global.esmockloader = esmockIsLoader

export default esmock

const [ major, minor ] = process.versions.node.split('.').map(it => +it)
Expand Down
2 changes: 2 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"install:test-node": "cd tests-node && npm install",
"install:test-jest": "cd tests-jest && npm install",
"install:test-source-map": "cd tests-source-map && npm install",
"install:test-no-loader": "cd tests-no-loader && npm install",
"install:test-nodets": "cd tests-nodets && npm install",
"install:all": "node --version && npm install && npm-run-all install:test*",
"test:test-ava": "cd tests-ava && npm test",
Expand All @@ -44,6 +45,7 @@
"test:node18-test-jest": "cd tests-jest && npm test",
"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: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
2 changes: 0 additions & 2 deletions tests/tests-ava/spec/esmock.ava.only.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import test from 'ava'
// 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 => {
// console.log( 'is?', global.esmockloader );
await esmock('../../local/mainUtil.js', {
'form-urlencoded': () => 'mock encode'
})
Expand Down
8 changes: 0 additions & 8 deletions tests/tests-ava/spec/esmock.ava.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ test('should mock a local file', async t => {
t.is(main(), 'main string, test string')
})

test.serial('should throw error if !esmockloader', async t => {
global.esmockloader = false
await t.throwsAsync(() => esmock('./to/module.js'), {
message: 'process must be started with --loader=esmock'
})
global.esmockloader = true
})

test('should throw error if local file not found', async t => {
const err = await t.throwsAsync(() => esmock('../../local/not/found.js', {
'../../local/mainUtil.js': {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import esmock from '../../src/esmock.js'
import esmock from 'esmock'

test('should throw error if !esmockloader', async () => {
global.esmockloader = false
await assert.rejects(() => esmock('./to/module.js'), {
message: 'process must be started with --loader=esmock'
})
global.esmockloader = true
})
15 changes: 15 additions & 0 deletions tests/tests-no-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "module",
"description": "esmock unit tests, no loader should error",
"repository": {
"type": "git",
"url": "https://github.com/iambumblehead/esmock.git"
},
"dependencies": {
"esmock": "file:.."
},
"scripts": {
"test1": "node --test",
"test": "npm run test1"
}
}
12 changes: 0 additions & 12 deletions tests/tests-uvu/esmock.uvu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ test('should mock a local file', async () => {
assert.is(main(), 'main string, test string')
})

test('should throw error if !esmockloader', async () => {
setTimeout(async () => {
global.esmockloader = false
try {
await esmock('./to/module.js')
} catch (e) {
assert.is(e.message, 'process must be started with --loader=esmock')
}
global.esmockloader = true
}, 500)
})

test('should throw error if local file not found', async () => {
try {
await esmock('../local/not/found.js', {
Expand Down