Mocking document object with js ribbon

I am trying to unit test a module (using tapejs) that depends on another module that uses the document object and I get "ReferenceError: document not defined"

// My module i want to test
import { createUrl } from '../config/paths';
// Paths.js
export const url = document.location.toString(); // This is where i'm getting the error.

      

I tried proxyquire to proxy this dependency, but it doesn't do anything.

const store = proxyquire('../../../store/list-store', {
'../config/paths': {
    createUrl: stub(),
},
});

      

Any suggestions?

+3


source to share


1 answer


Using the proxyquire noCallThru method helped me achieve this.

According to the proxyquire docs:

By default, proxyquire calls the function defined in the original dependency when it is not found on the stub.

If you prefer stricter behavior, you can prevent callThru in a modular or contextual manner.



My decision:

const proxyquireStrict = proxyquire.noCallThru();
const Store = proxyquireStrict('../../../store/booking-add-store', {
    '../config/paths': {
        createUrl: () => stub().returns(''),
    },
}).default;

      

+1


source







All Articles