Skip to content

Commit

Permalink
Merge pull request #61 from iambumblehead/add-plain-tests
Browse files Browse the repository at this point in the history
Add plain tests
  • Loading branch information
iambumblehead authored Jul 17, 2022
2 parents 4734667 + 294657c commit 0407687
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 28 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# changelog

* 1.7.8 _Jul.16.2022_
* add tests using the node native test-runner
* update README to use node native test-runner
* 1.7.7 _Jul.14.2022_
* support node v18.6.0 loader changes, credit @swivelgames
* 1.7.6 _Jul.13.2022_
Expand All @@ -12,7 +15,7 @@
* 1.7.3 _Feb.02.2022_
* increment resolvewithplus to handle array esm export, used by 'yargs'
* 1.7.2 _Dec.15.2021_
* remove README at npm packagew
* remove README at npm package
* 1.7.1 _Dec.15.2021_
* increment resolvewithplus, better tests
* 1.7.0 _Dec.13.2021_
Expand Down
49 changes: 29 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
esmock
======
```diff
+ ██╗
+ ██████╗ ███████╗ █████═████╗ ██████╗ ██████╗██║ ██╗
+██╔═══██╗██╔═════╝██╔══██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝
+████████║╚██████╗ ██║ ██║ ██║██║ ██║██║ ██████╔╝
+██╔═════╝ ╚════██╗██║ ██║ ██║██║ ██║██║ ██╔══██╗
+╚███████╗███████╔╝██║ ██║ ██║╚██████╔╝╚██████╗██║ ╚██╗
+ ╚══════╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝
```
[![npm version](https://badge.fury.io/js/esmock.svg)](https://badge.fury.io/js/esmock) [![Build Status](https://github.com/iambumblehead/esmock/workflows/nodejs-ci/badge.svg)][2] [![install size](https://packagephobia.now.sh/badge?p=esmock)](https://packagephobia.now.sh/result?p=esmock) [![downloads](https://badgen.now.sh/npm/dm/esmock)](https://npmjs.org/package/esmock)

**esmock provides native ESM import mocking for unit tests.** Use examples below as a quick-start guide or use the [descriptive and friendly esmock guide here.][10]
Expand All @@ -11,15 +18,17 @@ esmock



`esmock` must be used with node's experimental --loader
`esmock` must be used with node's --loader
``` json
{
"name": "give-esmock-a-star",
"type": "module",
"scripts": {
"test-uvu": "node --loader=esmock ./node_modules/uvu/bin.js ./spec/",
"test-ava": "ava --node-arguments=\"--loader=esmock\"",
"test-mocha": "mocha --loader=esmock --no-warnings"
"test": "node --loader=esmock --test",
"test-tap": "NODE_OPTIONS=--loader=esmock tap",
"test-uvu": "NODE_OPTIONS=--loader=esmock uvu ./spec/",
"test-ava": "NODE_OPTIONS=--loader=esmock ava",
"test-mocha": "mocha --loader=esmock"
}
}
```
Expand All @@ -29,16 +38,16 @@ esmock
await esmock(
'./to/module.js', // path to target module being tested
{ ...childmocks }, // mock definitions imported by target module
{ ...globalmocks } // mock definitions imported everywhere
);
{ ...globalmocks }) // mock definitions imported everywhere
```

`esmock` demonstrated with `ava` unit test examples
`esmock` demonstrated with unit test examples
``` javascript
import test from 'ava';
import test from 'node:test';
import assert from 'node:assert/strict';
import esmock from 'esmock';

test('should mock local files and packages', async t => {
test('should mock local files and packages', async () => {
const main = await esmock('../src/main.js', {
stringifierpackage : JSON.stringify,
'../src/hello.js' : {
Expand All @@ -47,45 +56,45 @@ test('should mock local files and packages', async t => {
}
});

t.is(main(), JSON.stringify({ test : 'world foobar' }));
assert.strictEqual(main(), JSON.stringify({ test : 'world foobar' }));
});

test('should do global instance mocks —third param', async t => {
test('should do global instance mocks —third param', async () => {
const { getFile } = await esmock('../src/main.js', {}, {
fs : {
readFileSync : () => 'returns this globally';
}
});

t.is(getFile(), 'returns this globally');
assert.strictEqual(getFile(), 'returns this globally');
});

test('should mock "await import()" using esmock.p', async t => {
test('should mock "await import()" using esmock.p', async () => {
// using esmock.p, mock definitions are kept in cache
const doAwaitImport = await esmock.p('../awaitImportLint.js', {
eslint : { ESLint : cfg => cfg }
});

// mock definition is there, in cache, when import is called
t.is(await doAwaitImport('cfg'), 'cfg');
assert.strictEqual(await doAwaitImport('cfg'), 'cfg');

esmock.purge(doAwaitImport); // clear cache, if you wish
});

test('should merge "default" value, when safe', async t => {
test('should merge "default" value, when safe', async () => {
const main = await esmock('../src/main.js');

t.is(main(), main.default()); // use the form you prefer
assert.strictEqual(main(), main.default());
});

test('should use implicit "default"', async t => {
test('should use implicit "default"', async () => {
const mainA = await esmock('../src/exportsMain.js', {
'../src/main.js' : () => 'mocked main' // short-hand, smaller
});
const mainB = await esmock('../src/exportsMain.js', {
'../src/main.js' : { default : () => 'mocked main' }
});

t.is(mainA(), mainB());
assert.strictEqual(mainA(), mainB());
});
```
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esmock",
"version": "1.7.7",
"version": "1.7.8",
"license": "MIT",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down Expand Up @@ -59,12 +59,17 @@
"eslint": "^8.12.0",
"esmock": "file:.",
"form-urlencoded": "4.2.1",
"run-script-os": "^1.1.6",
"sinon": "^12.0.1"
},
"scripts": {
"test-node": "node --no-warnings --loader=esmock --test ./spec/node/",
"test-node18": "if (node -v | grep v18); then npm run test-node; fi;",
"test-ava": "npx ava --node-arguments=\"--loader=esmock\" ./spec/ava/*.spec.js",
"test-uvu": "node --no-warnings --loader=esmock ./node_modules/uvu/bin.js ./spec/uvu/",
"test": "npm run test-ava && npm run test-uvu",
"test-uvu": "node --loader=esmock ./node_modules/uvu/bin.js ./spec/uvu/",
"test:default": "npm run test-node18 && npm run test-ava && npm run test-uvu",
"test:windows": "npm run test-ava && npm run test-uvu",
"test": "run-script-os",
"test-no-warn": "npx ava --node-arguments=\"--loader=esmock --no-warnings\"",
"lint": "npx eslint src/*js spec"
}
Expand Down
11 changes: 11 additions & 0 deletions spec/node/esmock.node.noloader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import esmock from '../../src/esmock.js';

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 spec/node/esmock.node.only.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'node:test';
import assert from 'assert';
import esmock from '../../src/esmock.js';

// this error can occur when sources do not define 'esmockloader'
// on 'global' but use a process linked variable instead
test('should not error when esmock used with ava.only', {
only : true
}, async () => {
await esmock('../local/mainUtil.js', {
'form-urlencoded' : () => 'mock encode'
});

assert.ok(true);
});
Loading

0 comments on commit 0407687

Please sign in to comment.