Wednesday, 27 November 2019

mocking es6 modules in unit test

Suppose we have a file(source.js) to test:

// source.js
import x from './x';

export default () => x();

And the unit test code is very simple:

// test.js
import test from 'ava';
import source from './source'

test("OK", t => {
  source();
  t.pass();
});

But this is the tricky thing. The file "./x" does not exist in the test environment. Since it is a unit test, we do not need "./x". We can mock the function "x()" anyhow(using sinon or something else). But the unit test tool, ava, keeps showing "Error: Cannot find module './x'";

Is there any way to run the unit test without the file "./x"?



from mocking es6 modules in unit test

No comments:

Post a Comment