Skip to content

Commit

Permalink
Merge pull request #188 from iambumblehead/add-mocha-tests-re-#186
Browse files Browse the repository at this point in the history
added mocha tests
  • Loading branch information
iambumblehead authored Nov 25, 2022
2 parents 4c8431d + 192be70 commit 32d80f5
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}, {
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 2 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions tests/tests-mocha/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
24 changes: 24 additions & 0 deletions tests/tests-mocha/src/app.js
Original file line number Diff line number Diff line change
@@ -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()
})
29 changes: 29 additions & 0 deletions tests/tests-mocha/test/app.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
})

0 comments on commit 32d80f5

Please sign in to comment.