Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking document object with tape js

I'm trying to unit test a module (using tapejs) that is dependent on another module that uses the document object and i get "ReferenceError: document is 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 seem to do anything.

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

Any suggestions?

like image 562
Kostas Sarantopoulos Avatar asked Mar 28 '26 23:03

Kostas Sarantopoulos


1 Answers

Using proxyquire's noCallThru method helped me achieve this.

According to proxyquire's docs:

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

If you prefer a more strict behavior you can prevent callThru on a per module or contextual basis.

My solution:

const proxyquireStrict = proxyquire.noCallThru();
const Store = proxyquireStrict('../../../store/booking-add-store', {
    '../config/paths': {
        createUrl: () => stub().returns(''),
    },
}).default;
like image 192
Kostas Sarantopoulos Avatar answered Mar 31 '26 05:03

Kostas Sarantopoulos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!