Skip to content

Commit

Permalink
Merge pull request #314 from iambumblehead/add-log-function-for-debug
Browse files Browse the repository at this point in the history
add log function for debugging loader
  • Loading branch information
iambumblehead authored Oct 17, 2024
2 parents 253eec5 + cddb5bc commit fc9f380
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# changelog

* 2.6.8 _tbd_
* [add log utility function](https://github.com/iambumblehead/esmock/pull/314) for debugging loader
* [dropped ava and jest](https://github.com/iambumblehead/esmock/pull/314) from test sequence, node v22 --loader issues
* 2.6.7 _Jul.16.2024_
* [add swc tests and remove swc caution from README](https://github.com/iambumblehead/esmock/pull/309) thanks @Brooooooklyn
* [unpin node 22.1 at test CI](https://github.com/iambumblehead/esmock/pull/309) and use latest 22.x
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

_**Note: For versions of node prior to v20.6.0,** "--loader" command line arguments must be used with `esmock` as demonstrated [in the wiki.][4] Current versions of node do not require "--loader"._

_**Note: due to --loader issues,** support for `ava` and `jest` are dropped._

`esmock` has the below signature
```js
await esmock(
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default [
rules: {
"@typescript-eslint/no-unused-expressions": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": 0,
"comma-dangle": "off"
}
},
Expand Down Expand Up @@ -47,6 +48,7 @@ export default [
"no-unused-vars": ["error", {
"vars": "all",
"args": "after-used",
"varsIgnorePattern": "log",
"ignoreRestSiblings": false
}],
"global-require": 0,
Expand Down
8 changes: 6 additions & 2 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs/promises'
import fs from 'node:fs'
import module from 'node:module'
import process from 'process'
import esmockErr from './esmockErr.js'
Expand Down Expand Up @@ -33,6 +33,10 @@ const moduleIdReCreate = (moduleid, treeid) => new RegExp(
const mockKeys = global.mockKeys = (global.mockKeys || {})
const mockKeysSource = global.mockKeysSource = (global.mockKeysSource || {})

// use fs when logging from hooks, console.log async unpredictable
const log = (...args) => (
fs.writeSync(1, JSON.stringify(args, null, ' ').slice(2, -1)))

// node v20.0-v20.6
const globalPreload = !module.register && (({ port }) => (
port.addEventListener('message', ev => (
Expand Down Expand Up @@ -170,7 +174,7 @@ const load = async (url, context, nextLoad) => {
const sourceIsNullLike = (
nextLoadRes.source === null || nextLoadRes.source === undefined)
const source = sourceIsNullLike
? String(await fs.readFile(new URL(url)))
? String(fs.readFileSync(new URL(url)))
: String(nextLoadRes.source)
const hbang = (source.match(hashbangRe) || [])[0] || ''
const sourcesafe = hbang ? source.replace(hashbangRe, '') : source
Expand Down
8 changes: 4 additions & 4 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
"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",
"test:test-ava": "cd tests-ava && npm test",
"test-dropped:test-ava": "cd tests-ava && npm test",
"test:test-tsx": "cd tests-tsx && npm test",
"test:test-swc": "cd tests-swc && npm test",
"test:node19-tsm": " cd tests-tsm && npm test",
"test:node18-test-tsm": "npm run isnodenight || npm run test:node19-tsm",
"test:node18-test-tsx": "cd tests-tsx && npm run test",
"test:node18-test-node": "cd tests-node && npm test",
"test:node18-test-jest": "cd tests-jest && npm test",
"test:node18-test-jest-ts": "cd tests-jest-ts && npm test",
"test-dropped:node18-test-jest": "cd tests-jest && npm test",
"test-dropped:node18-test-jest-ts": "cd tests-jest-ts && npm test",
"test:node18-test-nodets": "cd tests-nodets && npm test",
"test:node18-test-source-map": "cd tests-source-map && npm test",
"test-dropped: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*",
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-ava/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "git+https://github.com/iambumblehead/esmock.git"
},
"dependencies": {
"ava": "^5.3.1",
"ava": "^6.1.3",
"run-script-os": "^1.1.6",
"esmock": "file:..",
"sinon": "file:../node_modules/sinon",
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"jest": "^29.6.2",
"jest-light-runner": "0.5.0",
"jest-light-runner": "0.6.0",
"run-script-os": "^1.1.6",
"esmock": "file:..",
"sinon": "file:../node_modules/sinon",
Expand Down
10 changes: 5 additions & 5 deletions tests/tests-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"url": "git+https://github.com/iambumblehead/esmock.git"
},
"dependencies": {
"mocha": "^10.2.0",
"chai": "^4.3.7",
"chai-http": "^4.4.0",
"mocha": "^10.7.3",
"chai": "^5.1.1",
"chai-http": "^5.1.1",
"esmock": "file:..",
"express": "^4.18.2",
"passport": "^0.6.0"
"express": "^4.21.1",
"passport": "^0.7.0"
},
"scripts": {
"isnodelt20_6": "node -e \"(([mj, mn]) => (+mj < 20 || (+mj === 20 && +mn < 6)))(process.versions.node.split('.')) || process.exit(1)\"",
Expand Down
8 changes: 4 additions & 4 deletions tests/tests-mocha/test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chai from 'chai'
import chaiHttp from 'chai-http'
import * as chai from 'chai'
import {default as chaiHttp, request} from 'chai-http'
import esmock from 'esmock'

chai.use(chaiHttp)
Expand All @@ -14,8 +14,8 @@ const app = await esmock('../src/app.js', {
describe('/', () => {
it('should work', done => {
try {
chai
.request(app.default)
request
.execute(app)
.get('/')
.end((err, res) => {
app.close()
Expand Down
10 changes: 5 additions & 5 deletions tests/tests-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
},
"dependencies": {
"esmock": "file:..",
"@ava/typescript": "^4.1.0",
"@ava/typescript": "^5.0.0",
"@tsconfig/node16": "^16.1.0",
"@types/node": "^20.4.5",
"ava": "^5.3.1",
"@types/node": "^22.7.6",
"ava": "^6.1.3",
"cross-env": "^7.0.3",
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
"rimraf": "^6.0.1",
"typescript": "^5.6.3"
},
"scripts": {
"isnodelt20_6": "node -e \"(([mj, mn]) => (+mj < 20 || (+mj === 20 && +mn < 6)))(process.versions.node.split('.')) || process.exit(1)\"",
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"test": "npm run isloaderavailable && npm run test:loader || npm run test:current"
},
"devDependencies": {
"@types/node": "^20.14.2"
"@types/node": "^22.7.6"
}
}

0 comments on commit fc9f380

Please sign in to comment.