我对NodeJS和单元测试都很陌生。
我使用Option Explicit
Sub getnum()
Dim position As Variant
Dim cell As Range
With Worksheets("Orig") ' change it to your actual sheet name
With Intersect(.UsedRange, Columns("J"))
.Replace what:="P/O", replacement:="P/O ", lookat:=xlPart
For Each cell In .Cells
position = InStr(cell.Text, " 1")
If position > 0 Then cell.Value = Mid(cell.Value, position + 1, 7)
Next
End With
End With
End Sub
,但它应该是相同的"问题"使用Jest
或Mocha
或Ava
...因为我的问题似乎是whatever
/ export
...
我有一个带有一些功能的文件import
learning.js
...和// learning.js
function sum(a, b) {
return a + b
}
const multiply = (a, b) => a * b
module.exports = { sum: sum, multiply: multiply }
文件:
some.test.js
此时,一切都很完美,我的测试运行并通过。
但是,我有一个名为// some.test.js
const { sum, multiply } = require('./learning')
// const { sum, multiply } = require('./another')
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3)
})
test('multiplies 2 x 2 to equal 4', () => {
expect(multiply(2, 2)).toBe(4)
})
的第三个文件(我使用another.js
):
express
当我尝试从router.get('/another', async function(req, res) {
// TESTS
function sum(a, b) {
return a + b
}
const multiply = (a, b) => a * b
// DO SOME OTHER STUFF...
res.status(200).send('ok')
})
module.exports = { sum: sum, multiply: multiply }
//module.exports = router
运行相同的测试时(仅将some.test.js
语句更改为映射到require
),我无法使其正常工作。我的测试失败了:another.js
。
我尝试将TypeError multiply is not a function
移到其他地方,用export
重命名一些内容...我无法使其正常工作。
有任何线索吗?谢谢!
答案 0 :(得分:1)
您遇到范围问题 - sum
和multiply
超出module.exports
范围,因为您在路线中定义了这些问题处理程序。
为什么不试试这个:
创建新文件helpers.js
或services.js
- 但是您要描述自己的功能。
const sum = (a, b) => a + b
const multiply = (a, b) => a * b
module.exports = { sum, multiply }
然后在你的快递文件中:
const helpers = require('./helpers.js')
router.get('/another', (req, res) => {
helpers.sum(1,2)
helpers.multiply(3,4)
res.status(200).send('ok')
})
module.exports = router
然后在您的测试问题中,您可以以相同的方式要求helpers
并单独测试功能。