开玩笑的单元测试module.export失败?

时间:2019-11-04 15:28:51

标签: javascript unit-testing jestjs

我已经为单元测试设置了Jest,并且效果很好,但是功能在浏览器中不起作用?我知道这是因为函数的导出方式。

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;

另一方面,这在浏览器中有效,但是测试现在失败了吗?:

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

1 个答案:

答案 0 :(得分:1)

以下是您的案例的有效示例:

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);
});

单元测试结果:

 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