【发布时间】:2018-10-19 07:25:25
【问题描述】:
我是 jest 的新手,在确定如何测试嵌套在 Promise 中的结果时遇到了麻烦。具体来说:
myMethod: function (e) {
let self = this
self.resetErrors()
Parser.parseFile(this.form.uploadFile).then(res => {
const hasErrors = self.validFile(res)
if (!hasErrors) {
self.processFile(res)
}
})
}
我想测试以确保,假设 hasErrors 为假,self.processFile 会触发。这是我目前(失败)的最大努力:
describe("if the provided data is valid", () => {
it('runs processFile', () => {
const mockProcessFile = jest.fn()
mockParser = jest.fn(() => {
new Promise((resolve, reject) => {
return ValidMockData
}).then((loanData) => {
expect(mockProcessFile).toBeCalled()
})
})
CsvParser.parseFile = mockParser
wrapper.vm.validFile = jest.fn(true)
wrapper.vm.processFile = mockProcessFile
wrapper.vm.store().resolve((data) => {
expect(mockProcessFile).toBeCalled()
})
})
})
目前我收到Cannot read property 'then' of undefined 错误 - 这是有道理的,但我不确定在 then() 调用中我应该如何破解期望。任何想法表示赞赏
【问题讨论】:
-
我注意到的第一件事
new Promise((resolve, reject) => { return ValidMockData })...承诺需要解决或拒绝...返回一些 ValidMockData 不会这样做
标签: javascript promise jestjs