Jest Testing导入基本功能并运行测试

时间:2019-05-31 15:43:06

标签: javascript testing jestjs

我想对一个用Javascript ES6编写的小项目实施笑话。在下面,您可以看到我如何尝试实现一个功能。您认为我在哪里做错了?

我有添加参数的函数:

function findTotal(a, b) {
  return a + b;
}

然后将其导出:

export default { findTotal };

然后将其导入我的测试文件中:

import { findTotal } from "../src/assessment";

然后测试本身:

import { findTotal } from "../src/assessment";


describe("The Total", () => {

  it("should find total", () => {

    expect(findTotal(1, 2)).toBe(3);
  });

});

这是我的结果:


     ● The total › should find total

    TypeError: (0 , _assessment.findTotal) is not a function

      at Object.<anonymous> (test/assessment.test.js:5:38)
          at new Promise (<anonymous>)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

1 个答案:

答案 0 :(得分:0)

function findTotal(a, b) {
  return a + b;
}

export { findTotal };

单元测试:

import { findTotal } from './';

describe('The Total', () => {
  it('should find total', () => {
    expect(findTotal(1, 2)).toBe(3);
  });
});

单元测试结果:

 PASS  src/stackoverflow/56398100/index.spec.ts
  The Total
    ✓ should find total (5ms)

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