我正在嘗試設(shè)置Jest環(huán)境Node.js。以下是在名為 的文件中定義的函數(shù)controllers/a.js:function sum(a, b) { return a + b;}module.exports = sum;這是從tests.js文件中訪問的:const sum = require('../controllers/a');test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3);});這很好用: PASS tests/tests.js √ adds 1 + 2 to equal 3 (5 ms)但是,如果我修改a.js如下:function sum(a, b) { return a + b;}exports.sum = sum;然后我得到TypeError: FAIL tests/tests.js × adds 1 + 2 to equal 3 (3 ms) ● adds 1 + 2 to equal 3 TypeError: sum is not a function 2 | 3 | test('adds 1 + 2 to equal 3', () => { > 4 | expect(sum(1, 2)).toBe(3); | ^ 5 | });嘗試了以下幾種導(dǎo)出方式,均以TypeError上述方式失敗:module.exports.sum = sum;module.exports = { sum: sum };exports = sum;exports = { sum: sum };我需要導(dǎo)出多個函數(shù)以在Jest. Jest如果沒有module.exports按預(yù)期識別,我該怎么做?
JS 模塊導(dǎo)出不適用于 Jest
搖曳的薔薇
2022-12-02 15:57:51