esmock
(c)Bumblehead
![Build Status](https://travis-ci.org/iambumblehead/esmock.svg?branch=master)
This package must be used with node's experimental --loader,
ava --node-arguments="--loader=esmock"
mocha --loader=esmock
This package must be used with "module" type packages. Add the type to your package.json,
{
"name": "my-module",
"type": "module",
"scripts" : {
"unit-test": "ava --node-arguments=\"--loader=esmock\""
}
}
And use it
import test from 'ava';
import esmock from 'esmock';
test('should mock module and local file at the same time', async t => {
const main = await esmock('./local/main.js', {
'astringifierpackage' : o => JSON.stringify(o),
'./local/util.js' : {
exportedFunction : () => 'foobar'
}
});
t.is(main(), 'foobar, ' + JSON.stringify({ test: 'object' }));
});
test('should apply third parameter "global" definitions', async t => {
const main = await esmock('./local/main.js', {
'./local/mainUtil.js' : {
exportedFunction : () => 'foobar'
}
}, {
fs : {
readFileSync : () => {
return 'this value anywhere the instance imports fs, global';
}
}
});
const tplStr = main.readTemplateFile();
t.is(tplStr, 'this value anywhere the instance imports fs, global');
});
changelog
- 0.1.0 Apr.10.2021
- adds support for native esm modules
- 0.2.0 Apr.10.2021
- adds support for mocking core modules such as fs and path
- 0.3.0 Apr.10.2021
- adds support for mocking modules 'globally' for the instance