window-mock icon indicating copy to clipboard operation
window-mock copied to clipboard

new WindowMock() is not a constructor

Open khylias opened this issue 6 years ago • 2 comments

Hi @sbstnmsch,

I search a lib to mock window and document object into a Webpack config file and I found your lib. My webpack file is write into Javascript ES5, like below :

const path = require('path');
const webpack = require('webpack');
const WindowMock = require('window-mock');

const fakeWindow = new WindowMock();
...

new webpack.DefinePlugin({
   window: fakeWindow,
   document: fakeWindow.document,
    location: JSON.stringify({
      protocol: 'https',
      host: `localhost`
  })
})

I set my constant fakeWindow as your example into README : var windowMock = new WindowMock(); but I keep a error on build: TypeError: WindowMock is not a constructor

Environments :

"webpack": "^4.20.2",
"window-mock": "^0.0.13"

khylias avatar Oct 16 '18 20:10 khylias

Hi, When looking into the code WindowMock is exported as exports.default = WindowMock;. In ES6 it is interpreted as a named export, so in order to access the default export you need to: const WindowMock = require('window-mock').default;

Hope it helps.

teef avatar Mar 21 '19 16:03 teef

@khylias sorry I completely missed this issue. And thanks to @teef for clarification!

sbstnmsch-zz avatar Mar 21 '19 20:03 sbstnmsch-zz