esmock
![Build Status](https://travis-ci.org/iambumblehead/esmock.svg?branch=master)
esmock must be used with node's experimental --loader
{
"name": "give-esmock-a-star",
"type": "module",
"scripts" : {
"unit-test-ava": "ava --node-arguments=\"--loader=esmock\"",
"unit-test-mocha": "mocha --loader=esmock"
}
}
Use it await esmock( './path/to/module.js', childmocks, globalmocks )
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 do "global" instance mocks (third parameter)', async t => {
const { getFile } = await esmock('./local/main.js', {}, {
fs : {
readFileSync : () => {
return 'this anywhere your instance imports fs for readFileSync';
}
}
});
t.is(getFile(), 'this anywhere your instance imports fs for readFileSync');
});
changelog
- 0.3.1 Apr.12.2021
- 0.3.0 Apr.10.2021
- adds support for mocking modules 'globally' for the instance
- 0.2.0 Apr.10.2021
- adds support for mocking core modules such as fs and path
- 0.1.0 Apr.10.2021
- adds support for native esm modules