Ava+Sinon unite testing. ReferenceError: window is not defined

542 Views Asked by At

I tried to write a test file to test one of the moduel. The module has a line of code that access window.location.origin which is causing an error when running the unit test : ReferenceError: window is not defined.

The moduel under test and test case code:

myfile.js:

const isExternal =  window.location.origin.includes('.com');

const getAccess = () => {
    if(isExternal){
        //Do somthing
    }
}

myfile.test.js:

const test = require('ava');
const sinon = require('sinon');
const myfile = require('../../src/myfile');

test.serial('test myfile', t => {
    myfile.isExternal = true;
    //Assert below
})

My problem is when I have the line const myfile = require('../../src/myfile'); in the code and run npm run test, it will show an error:

ReferenceError: window is not defined
tests\myfile.test.js exited with a non-zero exit code: 1
0

There are 0 best solutions below