在阅读Jest文档后,当它提到要从测试文件中导出单个函数时,它们会显示以下示例:
function sum(a, b) {
return a + b;
}
module.exports = sum;
现在,如果我有多个特定功能,我想在我的测试文件中导出,如下所示:
function sum(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
function subtract(a, b) {
return a - b;
}
module.exports = sum;
module.exports = multiply;
multiply
函数是唯一导出的函数。如何将这些功能导出?或者只是我文件的一部分?
答案 0 :(得分:2)
您可以这样做:
module.exports = {};
module.exports.sum = function sum(a, b) {
return a + b;
}
module.exports.multiply = function multiply(a, b) {
return a * b;
}
module.exports.subtract = function subtract(a, b) {
return a - b;
}
结束你这样使用它:
var MyMathModule = require('./my_math_module');
MyMathModule.sum(a, b);
MyMathModule.multiply(a, b);
MyMathModule.subtract(a, b);
答案 1 :(得分:1)
首先,在你的例子中,你所做的只是用函数覆盖 exports
对象(完全没问题)
exports
和module.exports
是一个对象,实际上是同一个对象(即module.exports === exports // true
)
要做你想做的事,你可以通过几种方式做到这一点:
exports.sum = sum
exports.multiply = multiply
或
module.exports = { sum: sum, multiply: multiply } // etc
或
module.exports.sum = sum
module.exports.multiply = multiply
答案 2 :(得分:0)
牢记这个问题的答案,我将在此处粘贴两种执行相同操作的方法。
例如,您有一个名为exercise5的JS文件,如下所示:
//You can create an object with functions, as follows:
const wordAnalysis = {
type: (word) => typeof (word),
whiteSpaces: (word) => {
let wordAnalysis = word.includes(' ')
if (wordAnalysis) {
return 'It has spaces'
} else {
return "It doesn't has spaces"
}
}
}
//Or you can create several single functions, like the following:
function numberAnalysis(word) {
let isANumber = typeof (word) === 'number' ? true : false
return isANumber
}
// în order to avoid overwriting the module.exports, it is needed to do one of the following (I chose the first one):
// 1)
module.exports.firstPlace = wordAnalysis
module.exports.secondPlace = numberAnalysis
// 2)
// module.exports = {
// functions: functions,
// isANumber: isANumber
// }
// 3)
// exports.functions = functions
// exports.isANumber = isANumber
// 4)
// exports = {
// functions: functions,
// isANumber: isANumber
// }
现在将文件测试命名为exercise5.test.js:
const wordAnalysis = require('./exercise5')
const numberAnalysis = require('./exercise5')
test('It should give me the type of what was typed', () => {
expect(wordAnalysis.firstPlace.type('teste')).toEqual('string')
})
test('It should give me the type of what was typed', () => {
expect(wordAnalysis.firstPlace.type(22)).toEqual('number')
})
test("It should give true if what is typed has at least a space or false if it doesn't", () => {
expect(wordAnalysis.firstPlace.whiteSpaces('Jon is cool')).toEqual('It has spaces');
})
test("It should give true if what is typed has at least a space or false if it doesn't", () => {
expect(wordAnalysis.firstPlace.whiteSpaces('AllTogetherNow')).toBe("It doesn't has spaces");
})
test('it should analyse if the given expression is a number or not', () => {
expect(numberAnalysis.secondPlace(2)).toBeTruthy()
})
test('it should analyse if the given expression is a number or not', () => {
expect(numberAnalysis.secondPlace('jon')).toBeFalsy()
})
您唯一需要了解的是导出/导入正确的对象/函数,当然在开发测试时会调用它。