Skip to content

Commit 9f8ac94

Browse files
author
Dan Foley
committed
mock-require, and a global Bootstrap module
1 parent 9fafda5 commit 9f8ac94

11 files changed

Lines changed: 135 additions & 14 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ insert_final_newline = false
1313

1414
[*.json]
1515
indent_style = space
16-
indent_size = 4
16+
indent_size = 2
1717
end_of_line = lf
1818
charset = utf-8
1919
trim_trailing_whitespace = true

Makefile

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,40 @@ MOCHA_OPTIONS = --recursive --ui bdd --timeout 1000
1212
.DEFAULT_GOAL: quality
1313

1414

15-
test: tape tape-esm mocha mocha-esm jest ava
15+
test: tape mocha jest ava
1616

1717

1818
# https://github.com/substack/tape
19-
tape:
20-
@NODE_ENV=test $(NODE_BIN)/tape 'test/tape/*.js'
21-
19+
tape-cjs:
20+
@NODE_ENV=test $(NODE_BIN)/tape 'test/bootstrap.js' 'test/tape/*.js'
2221
tape-esm:
23-
@NODE_ENV=test $(NODE_BIN)/tape -r esm 'test/tape/*.mjs'
22+
@NODE_ENV=test $(NODE_BIN)/tape -r esm 'test/bootstrap.js' 'test/tape/*.mjs'
23+
tape: tape-cjs tape-esm
24+
2425

2526
# https://mochajs.org/#usage
26-
mocha:
27+
mocha-cjs:
2728
@NODE_ENV=test $(NODE_BIN)/mocha $(MOCHA_OPTIONS) --reporter nyan \
28-
'./test/mocha/*.js'
29-
29+
'test/bootstrap.js' './test/mocha/*.js'
3030
mocha-esm:
3131
@NODE_ENV=test $(NODE_BIN)/mocha $(MOCHA_OPTIONS) --reporter dot \
3232
-r esm \
33-
'./test/mocha/*.mjs'
33+
'test/bootstrap.js' './test/mocha/*.mjs'
34+
mocha: mocha-cjs mocha-esm
35+
3436

3537
# https://jestjs.io/docs/en/cli
3638
# @see package.json + { jest }
3739
jest:
3840
@NODE_ENV=test $(NODE_BIN)/jest --config jest.config.js
41+
# TODO: ESM variant
42+
3943

4044
# https://jestjs.io/docs/en/cli
4145
# @see package.json + { ava }
4246
ava:
4347
@NODE_ENV=test $(NODE_BIN)/ava
48+
# TODO: ESM variant
4449

4550

4651
lint:
@@ -59,7 +64,7 @@ quality: test lint
5964
# at Function.Module._load (internal/modules/cjs/loader.js:530:3)
6065
# at Module.require (internal/modules/cjs/loader.js:637:17)
6166
# ```
62-
ci: tape mocha jest ava lint
67+
ci: tape-cjs mocha-cjs jest ava lint
6368

6469

6570
lock:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Because I proselytize that it's good practice to [learn a language through Asser
1818
&#x2605; [chai/expect](https://github.com/chaijs/chai) <br />
1919
&#x2605; [sinon](https://github.com/sinonjs/sinon) <br />
2020
&#x2605; [rewire](https://github.com/jhnns/rewire) <br />
21+
&#x2605; [mock-require](https://github.com/boblauer/mock-require) <br />
2122

2223
&#x2605; [eslint](https://github.com/eslint/eslint) <br />
2324

jest.config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ module.exports = {
2222
],
2323

2424
// // opinionated: go with 'test/__tests__'
25-
// roots: [ '<rootDir>/test/jest' ],
25+
// roots: [
26+
// 'test',
27+
// '<rootDir>/test/jest',
28+
// ],
29+
30+
// seems to run AFTER the Tests execute
31+
setupFiles: [
32+
'./test/bootstrap.js',
33+
],
34+
// setupTestFrameworkScriptFile: './test/bootstrap.js',
2635

2736
// // not sure how this helps us at all
2837
// moduleFileExtensions: [ 'js', 'mjs', 'jsx' ],
@@ -35,6 +44,9 @@ module.exports = {
3544
// // opinionated: always showed a notification on success :(
3645
// notify: true,
3746
// notifyMode: 'failure',
47+
roots: [
48+
'test',
49+
],
3850
runner: 'jest-runner',
3951
testRunner: 'jasmine2',
4052
timers: 'real',

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"tap": false,
3131
"compileEnhancements": false,
3232
"require": [
33-
"esm"
33+
"esm",
34+
"./test/bootstrap.js"
3435
],
3536
"extensions": [],
3637
"babel": {}
@@ -44,8 +45,9 @@
4445
"eslint": "^4.19.1",
4546
"jest": "^23.2.0",
4647
"mocha": "^5.1.1",
48+
"mock-require": "^3.0.2",
4749
"rewire": "^4.0.1",
4850
"sinon": "^6.0.1",
4951
"tape": "^4.9.0"
5052
}
51-
}
53+
}

test/__tests__/jest-bootstrap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* global test expect */
2+
3+
// // "Cannot find module 'mock-require-from-bootstrap' from 'bootstrap.js'"
4+
// const { mockRequireExport } = require('mock-require-from-bootstrap');
5+
// const { mockRequireProperty } = require('mock-require-from-bootstrap').default;
6+
7+
8+
test('BOOTSTRAP_LOADED', () => {
9+
const { BOOTSTRAP_LOADED } = global;
10+
11+
expect(BOOTSTRAP_LOADED).toBe(true);
12+
});
13+
14+
test.skip('mock-require', () => {
15+
// expect(mockRequireExport).toBe('EXPORT');
16+
// expect(mockRequireProperty).toBe('PROPERTY');
17+
});

test/ava/ava-bootstrap.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const test = require('ava');
2+
3+
const { mockRequireExport } = require('mock-require-from-bootstrap');
4+
const { mockRequireProperty } = require('mock-require-from-bootstrap').default;
5+
6+
7+
test('BOOTSTRAP_LOADED', (t) => {
8+
const { BOOTSTRAP_LOADED } = global;
9+
10+
t.true(BOOTSTRAP_LOADED, true);
11+
});
12+
13+
test('mock-require', (t) => {
14+
t.plan(2);
15+
16+
t.is(mockRequireExport, 'EXPORT');
17+
t.is(mockRequireProperty, 'PROPERTY');
18+
});

test/bootstrap.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const mockRequire = require('mock-require');
2+
3+
mockRequire('mock-require-from-bootstrap', {
4+
mockRequireExport: 'EXPORT',
5+
default: {
6+
mockRequireProperty: 'PROPERTY',
7+
},
8+
});
9+
10+
11+
global.BOOTSTRAP_LOADED = true;

test/mocha/mocha-bootstrap.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { expect } = require('chai');
2+
3+
const { mockRequireExport } = require('mock-require-from-bootstrap');
4+
const { mockRequireProperty } = require('mock-require-from-bootstrap').default;
5+
6+
7+
describe('bootstrap', () => {
8+
it('mockRequireExport', () => {
9+
const { BOOTSTRAP_LOADED } = global;
10+
11+
expect(BOOTSTRAP_LOADED).to.equal(true);
12+
});
13+
14+
it('mock-require', () => {
15+
expect(mockRequireExport).to.equal('EXPORT');
16+
expect(mockRequireProperty).to.equal('PROPERTY');
17+
});
18+
});

test/tape/tape-bootstrap.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const test = require('tape');
2+
3+
const { mockRequireExport } = require('mock-require-from-bootstrap');
4+
const { mockRequireProperty } = require('mock-require-from-bootstrap').default;
5+
6+
7+
test('BOOTSTRAP_LOADED', (t) => {
8+
const { BOOTSTRAP_LOADED } = global;
9+
t.plan(1);
10+
11+
t.equal(BOOTSTRAP_LOADED, true);
12+
13+
t.end();
14+
});
15+
16+
test('mock-require', (t) => {
17+
t.plan(2);
18+
19+
t.equal(mockRequireExport, 'EXPORT');
20+
t.equal(mockRequireProperty, 'PROPERTY');
21+
22+
t.end();
23+
});

0 commit comments

Comments
 (0)