It seems no work when use mocha-jsdom when depend lib have 'window' variable.
See original GitHub issueHI,
I have a JS file import other libs which use the window
variable and I want to test it by mocha.
I use ES6 and I must compile the test JS file by babel
, I use the command mocha --compilers js:babel-core/register
, but when I compile the JS there are some error as follow.
I use mocha-jsdom but it seems no work.
How I use mocha-jsdom to mock the ‘window’ variable? Thanks.
JS file something like this:
import xxx from 'xxx'; // use the 'window' variable
export default function foo() {
xxx.bar();
}
test file something like this:
import jsdom from 'mocha-jsdom';
import foo from './foo';
describe('test' () => {
jsdom();
it(' test test', () => {
foo();
});
});
error:
> mocha --compilers js:babel-core/register --recursive
.../node_modules/velocity-animate/velocity.js:403
})(window);
^
ReferenceError: window is not defined
at Object.<anonymous> (...node_modules/velocity-animate/velocity.js:403:4)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (.../node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (.../node_modules/rc-collapse/lib/openAnimation.js:9:24)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (.../node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (.../node_modules/rc-collapse/lib/Collapse.js:17:22)
......
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Mocha: Can't seem to call module's methods that use jQuery ...
So, I am using Mocha/Chai for some testing and the item I am testing depends on jQuery. I am using jsdom to help...
Read more >How to mock global window variables with Jest
Mocking non-existent globals. In case of variables which are globally available but are not provided by jsdom by default e.g. various API ......
Read more >Get Better at Unit Testing with Mocha, Chai, and JSDOM
Unit testing isn't a chore: it actually has great value! Learn its benefits and how you can improve unit testing through outlining, mocking, ......
Read more >Configuration - Quokka.js
The settings in the global config file are applied to all Quokka files, no matter if you are running Quokka in an opened...
Read more >An Overview of JavaScript Testing in 2022 - Medium
Jsdom simulates whatever you get when you run your JS inside the browser like window, document, body, location, cookies, selectors but it doesn' ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For those running into the same problem, I found an alternate solution which seems to work better.
npm uninstall mocha-jsdom
npm install jsdom
setup.js
file somewhere and put this code in it:mocha -r test/setup.js
Then you can import everything without issue and jsdom will be automatically loaded for all your tests.
You’ll need to import it inside a
before()
block.