sbstnmsch-zz / window-mock

A light-weight window-mock for node/ io.js unit-testing written in ES6
MIT License
19 stars 5 forks source link

new WindowMock() is not a constructor #183

Open khylias opened 6 years ago

khylias commented 6 years ago

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"
teef commented 5 years ago

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.

sbstnmsch-zz commented 5 years ago

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