diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index b8b843ad..57f34daf 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -12,18 +12,18 @@ on: jobs: build: runs-on: ${{ matrix.os }} - timeout-minutes: 5 + timeout-minutes: 12 strategy: matrix: node-version: [18.x] os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - - run: npm install -g npm@latest + # - run: npm install -g npm@latest - run: npm install - run: npm run build --if-present - run: | diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b0d3ad58..24f5b0b7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,9 +12,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: lint - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: '16.x' - run: npm install diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 724944e9..f26229b1 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -15,17 +15,17 @@ jobs: timeout-minutes: 12 strategy: matrix: - node-version: [14.x, 16.x, 18.x] + # node-14's npm must be updated, but no longer updatable due to: + # https://github.com/npm/cli/issues/2663 + # because of this, node-14 test strategy is removed :( + node-version: [16.x, 18.x] os: [ubuntu-latest, windows-latest] - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ - steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 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/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index e7939b6f..c39bbd8a 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -11,8 +11,8 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: 18 - run: npm install diff --git a/CHANGELOG.md b/CHANGELOG.md index 1646a61d..9ab203c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # changelog + * 2.0.8 _Nov.11.2025_ + * [use live default export](https://github.com/iambumblehead/esmock/pull/188) in prototype of returned mock definition to resolve class instance mocking + * [added CONTRIBUTING.md](https://github.com/iambumblehead/esmock/pull/188) * 2.0.7 _Oct.26.2022_ * [use export esmock.js,](https://github.com/iambumblehead/esmock/pull/182) rather than esmockLoader.js, as main package export * [embed resolvewithplus inside esmock,](https://github.com/iambumblehead/esmock/pull/181) to support yarn PnP, per @koshic diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..ede358b2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,8 @@ +`esmock` relies on unit-tests to verify its behaviour. To get started, clone esmock and add a test to [one of the test runner folders here.][0] From esmock's root folder use `npm install && npm run test:install` to fetch all dependencies then, from whichever test folder is being used, `npm test` will run tests. + +If `esmock` is failing for you, feel free to submit a PR that reproduces the issue with a failing unit-test. If available, I will try to resolve the issue and publish a new version of `esmock` with the solution. + +Please do not submit PRs to convert esmock to typescript or add build scripts. Typescript and build scripts are fine, however, esmock's current setup is ideal for me now. + + +[0]: https://github.com/iambumblehead/esmock/tree/master/tests diff --git a/package.json b/package.json index ce2003f7..3d758270 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.0.7", + "version": "2.0.8", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import mocking for unit tests", diff --git a/src/esmockModule.js b/src/esmockModule.js index 3415e7e2..2ccc701e 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -19,7 +19,8 @@ const nextId = ((id = 0) => () => ++id)() const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) => - (isObj(defLive) && isObj(def)) ? Object.assign({}, defLive, def) : def + (isObj(defLive) && isObj(def)) + ? Object.assign(Object.create(defLive), def) : def const esmockModuleApply = (defLive, def, fileURL) => { def = Object.assign({}, defLive || {}, { @@ -62,7 +63,7 @@ const esmockModuleImportedSanitize = (imported, esmkTreeId) => { if (/boolean|string|number/.test(typeof importedDefault)) return imported - + // ex, non-extensible "[object Module]": import * as fs from 'fs'; export fs; return Object.isExtensible(importedDefault) ? Object.assign(importedDefault, imported, { esmkTreeId }) diff --git a/tests/package.json b/tests/package.json index 23ca3d19..8b936552 100644 --- a/tests/package.json +++ b/tests/package.json @@ -36,9 +36,11 @@ "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:test-mocha": "cd tests-mocha && npm install", "install:all": "node --version && 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-mocha": "cd tests-mocha && npm test", "test:node18-test-tsm": "cd tests-tsm && npm test", "test:node18-test-node": "cd tests-node && npm test", "test:node18-test-jest": "cd tests-jest && npm test", diff --git a/tests/tests-mocha/package.json b/tests/tests-mocha/package.json new file mode 100644 index 00000000..a27d56c4 --- /dev/null +++ b/tests/tests-mocha/package.json @@ -0,0 +1,19 @@ +{ + "type": "module", + "description": "esmock unit tests, mocha", + "repository": { + "type": "git", + "url": "https://github.com/iambumblehead/esmock.git" + }, + "dependencies": { + "mocha": "^10.1.0", + "chai": "^4.3.7", + "chai-http": "^4.3.0", + "esmock": "file:..", + "express": "^4.18.2", + "passport": "^0.6.0" + }, + "scripts": { + "test": "mocha --loader=esmock" + } +} diff --git a/tests/tests-mocha/src/app.js b/tests/tests-mocha/src/app.js new file mode 100644 index 00000000..6ca8ed3e --- /dev/null +++ b/tests/tests-mocha/src/app.js @@ -0,0 +1,24 @@ +import express from 'express' +import passport from 'passport' + +const app = express() +const port = 7575 + +passport.use('bearerStrategy') + +// app.use(passport.initialize()) +if (typeof passport.initialize !== 'function') { + throw new Error('inavalid mock') +} + +app.get('/', (req, res) => { + res.send('Hello World!') +}) + +const server = app.listen(port, () => { + console.log(`Example app listening on port ${port}`) +}) + +export default Object.assign(app, { + close: () => server.close() +}) diff --git a/tests/tests-mocha/test/app.test.js b/tests/tests-mocha/test/app.test.js new file mode 100644 index 00000000..85002f2e --- /dev/null +++ b/tests/tests-mocha/test/app.test.js @@ -0,0 +1,29 @@ +import chai from 'chai' +import chaiHttp from 'chai-http' +import esmock from 'esmock' + +chai.use(chaiHttp) +chai.should() + +const app = await esmock('../src/app.js', { + passport: { + use: bearerStrategy => `mocked${bearerStrategy}` + } +}, {}) + +describe('/', () => { + it('should work', done => { + try { + chai + .request(app.default) + .get('/') + .end((err, res) => { + app.close() + res.should.have.status(200) + }) + done() + } catch (e) { + console.log(e) + } + }) +})