Unit testing resource-loader on node.js
See original GitHub issueSetup
I use pixi.js, which uses resource-loader. I have my own things in my app, and one of them is loader, which uses pixi.js loader. I use Jest for unit testing.
Issue
Because Jest works in node.js enviroment, I cannot get resource-loader to work. No errors, it just doesn’t finish loading (and callback is not called). My suspect is XHR requests are not working.
Moreover: mocking GET request with xhr-mock doesn’t work - request is not interrupted. I have even tested pure resource-loader with the following code:
const l = new Loader();
l.add('foo', 'data:image/gif;base64,R0lGODlhAQABAPAAAP8REf///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==');
l.onProgress.add(() => { console.log('progress'); });
l.onError.add(() => { console.log('error'); });
l.onLoad.add(() => { console.log('load'); });
l.onComplete.add(() => { console.log('complete'); });
l.load(() => {
done();
});
where that base64 is image taken from resource-loader tests. And no done()
is called, neither console logs are executed. Just nothing works. resource-loader tests are written using Karma, and seem to use Chrome as browser, to that may be, why node.js test don’t work.
And it’s not uncommon case to use some resource-loader on node.js. I imagine some node.js games.
Any ideas what can I do? It makes any unit testing pointless, if I cannot load resources.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (2 by maintainers)
I ran your code example and attached a node debugger. I stepped through resource-loader and everything is working correctly.
Image
just never calls the load or error callbacks.A quick search shows that jsdom doesn’t load anything unless you configure it to:
https://github.com/jsdom/jsdom/issues/1816#issuecomment-310106280
Closing this since it is a jsdom/Jest configuration issue.
You were extremelly generous to me. Thank You, sir!