Mohammed
Mohammed

Reputation: 457

Jest unit test module.export fails?

I have setup Jest for unit testing and it works great, but the functions do not work in the browser? I know it is because the way the functions are exported.

import $ from 'jquery';

function factBox() {
  console.log("GHEje fwe");
  $("body").css("background-color", "red");
}

function sum(a, b) {
  console.log("SUMMM");
  return a + b;
}

module.exports = factBox;
module.exports = sum;

On the other hand this works in the browser, but the test now fails?:

import $ from 'jquery';

function factBox() {
  console.log("GHEje fwe");
  $("body").css("background-color", "red");
}

function sum(a, b) {
  console.log("SUMMM");
  return a + b;
}

export {
  factBox,
  sum
};

enter image description here

Upvotes: 0

Views: 319

Answers (1)

Lin Du
Lin Du

Reputation: 102672

Here is a working example for your case:

index.js:

import $ from 'jquery';

function factBox() {
  console.log('GHEje fwe');
  $('body').css('background-color', 'red');
}

function sum(a, b) {
  console.log('SUMMM');
  return a + b;
}

export { factBox, sum };

index.spec.js:

import { sum } from '.';

test('add 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Unit test result:

 PASS  src/stackoverflow/58696477/index.spec.js (10.76s)
  ✓ add 1 + 2 to equal 3 (11ms)

  console.log src/stackoverflow/58696477/index.js:9
    SUMMM

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.303s

Upvotes: 1

Related Questions