mocha command giving ReferenceError: window is not defined

后端 未结 3 792
暖寄归人
暖寄归人 2020-12-14 15:29

I am using command:

mocha --compilers :./test/babel-setup.js --recursive --watch

It is giving error:

ReferenceError: window         


        
相关标签:
3条回答
  • 2020-12-14 15:45

    To test frontend libraries with mocha you need the node modul mocha-jsdom you find it here: https://www.npmjs.com/package/mocha-jsdom

    0 讨论(0)
  • 2020-12-14 16:01

    I was able to use jsdom-global to fix this issue. Follow the instructions in that link to install. Specifically, run

    npm install --save-dev --save-exact jsdom jsdom-global
    

    then add -r jsdom-global/register to your mocha command-line. When you re-run your tests, the window is not defined error will go away.

    0 讨论(0)
  • 2020-12-14 16:02

    Since I was doing too much with the window object, I just used this code to determine if I was in mocha or not.

    function isMocha(){
        let context = (global || window);
        return ['afterEach','after','beforeEach','before','describe','it'].every(function(functionName){
            return context[functionName] instanceof Function;
        });
    }
    
    0 讨论(0)
提交回复
热议问题