0

I am working on a test suite and I came across this dilemma:

Importing ES6 js from a file with require or with import?

I am currently using mocha and chai for testing, with babel already configured.

Now I came up with a solution but I believe it is not "good practice", in fact after some trial and mainly one error stating that TypeError: Object #<Object> has no method 'getUsers' I found that if I:

export default Utils;

where Utils is an object, and inside my (ES5) test suite I

var util = require('./../src/utils/Utils');

it triggers the mentioned error. Same if I try the ES6 version:

import Utils, * as util from './../src/utils/Utils';

While if I

import Utils from './../src/utils/Utils';
var util = Utils;

it works, can someone explain me why is that?

I probably need to specify that util get used by calling one of the functions defined inside the Util object exported, like: util.functionName();

iomv
  • 2,409
  • 18
  • 28
  • Have you tried to look at what's imported? `util` should be an object with named exports and `default` property for default export, `Utils` should be default export. [`import ..., * as ...` is there](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Importing_defaults) exactly to cover this case. – Estus Flask Sep 08 '16 at 13:31
  • I feel bad for not having logged it before, thanks for the inputs guys. It is indeed a duplicate of that question. At least I know why now. feel free to remove this question! – iomv Sep 08 '16 at 13:44

0 Answers0