NetworkError when I use in jest unit test
I'm trying to write backend test cases attached with yml, when the yml file tries to load, getting NetworkError
The reason behind this is, jest having window object with the execution so ymljs trying to access file using xhr how to solve this issue?
I did window.XMLHttpRequest = false; on top of my test. But need a better way to solve this problem
You don't have to use yaml.load() actually. You could just fetch the raw yaml data yourself in whichever way you want (like, with fs.readFile() in node?) and parse it with yaml.parse(). That should give you more control
#104
You can just mock the whole YamlJs:
jest.mock(
'yamljs',
function createYamlJsMock() {
return {parse: jest.fn()};
}
);
import YAML from 'yamljs';
describe('Foo', function testFoo() {
it('Bar', function testBar() {
(new MyYamlReader())->loadAndParse("dummy");
// call in MyYamlReader class: YAML.parse
expect(YAML.parse).toHaveBeenCalled();
YAML.parse.mockReset(); // reset, because kinda global mocked ;)
});
});