1

too many divergent posts upon googling to choose a clear and up-to-date solution...

I wrote 3 tests to check different possibilities

===========. TEST 1 OK ================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
module.exports = sayHello;

// helloJestTest

const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

===========. TEST 2 FAILING ================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
export default { sayHello }; // <= changed

// helloJestTest

const sayHello = require('../../src/client/js/helloJest');
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

TypeError: sayHello is not a function

      3 |
      4 | test('string returning hello there jest', () => {//
    > 5 |   expect(sayHello()).toEqual('hello there jest');
        |          ^
      6 | });
      7 |

===========. TEST 3 FAILING ================

// helloJest.js

function sayHello() {
  return "hello there jest"
}
export default { sayHello }; //  <= changed

// helloJestTest

import { sayHello } from '../../src/client/js/helloJest'; // <= changed
test('string returning hello there jest', () => {// 
  expect(sayHello()).toEqual('hello there jest');
});

    TypeError: (0 , _helloJest.sayHello) is not a function

      3 |
      4 | test('string returning hello there jest', () => {//
    > 5 |   expect(sayHello()).toEqual('hello there jest');
        |          ^
      6 | });

How to pass the TEST 3 correctly ???

I am using the following packages

package.json

"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
...
"jest": {
    "moduleFileExtensions": ["js"],
    "transform": { "^.+\\.js?$": "babel-jest" },
    "testRegex": "/tests/.*\\.(js)$"
  }

and I have in

.babelrc

{
  "presets": ["env"]
}
3
  • What's the difference between 2 and 3? You only seem to have made half the change; in both cases the default export is an object, not just the function. Commented Jan 1, 2019 at 18:01
  • did you try in your test2 to do const { sayHello } = require('../../src/client/js/helloJest'); Commented Jan 1, 2019 at 18:03
  • In 2. ( export default ES6 , require() ) In 3. ( export default ES6, import ES6 ) Commented Jan 1, 2019 at 18:03

2 Answers 2

5

You're tripping up in a couple of places there. Primarily: You don't use {} with the default import/export.

This:

export default { sayHello };

exports an object as the default export of the module. The object has a single property, sayHello, referring to the function. To make the function the default export, don't use the {}:

export default sayHello;

Then, when importing, if you want the default import, don't use {}:

import sayHello from '../../src/client/js/helloJest';

If you want to export a named export, you do use {}:

export { sayHello };

and

import { sayHello } from '../../src/client/js/helloJest';

Examples of both on plunker: https://embed.plnkr.co/f8PhOhULphQUrVXwdXF3/

Sign up to request clarification or add additional context in comments.

3 Comments

it's ok with default... however it seems that Jest does not like import a named export ....
@erwin - You're sure you're exporting it correctly? It would be odd if Jest had a problem with named exports.
@erwin one thing that tripped me up for a while. {} is not destructuring. It is just importing named exports from the module.
1

TEST 2

You do export as default an object with a single property which is the sayHello function, so you should import it in jest through the following:

const { sayHello } = require('../../src/client/js/helloJest');

TEST 3

Again you do export as above. In this case you can import it as the following:

import Hello from '../../src/client/js/helloJest';

And then you should be able to use your func as:

Hello.sayHello

1 Comment

That's it !!! I got it right now in Test 3 ... and understood all 3 tests ... thanks a lot !!!