问题描述
我正在使用 Oboe.js、oboe-promise(在承诺中包装双簧管调用)和 Jest。我可能正在做一些愚蠢的事情。有什么建议么?谢谢。
我的代码
"use strict";
const oboe = require('oboe-promise')
const { Readable } = require('stream')
class Example {
async run() {
const json = JSON.stringify([{obj1: {name: 'example',value: 5}},{obj2: {type: 'other',value: 0}}])
const strm = Readable.from(json)
return await oboe(strm)
.node('{type}',(node) => {
node.name = node.type
delete node.type
return node
})
.run()
}
}
module.exports = Example
我的测试文件
"use strict"
// Classes
let Example // class under test
// Objects
let example // object under test
describe('Example',() => {
beforeEach(() => {
Example = require('../../src/Example')
example = new Example()
})
test('run',async () => {
expect(await example.run()).toEqual(JSON.stringify([{obj1: {name: 'example',{obj2: {value: 0,name: 'other'}}]))
})
})
当我运行 Jest 测试时,我收到“在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”我添加了 jest.setTimeout(60000) 并得到相同的结果。我还有其他测试异步非双簧管代码的 Jest 测试,它们使用 async/await 可以正常工作
test('run',async () => {
expect(await <codetoTest>).toEqual(<expected>)
})
模式。
"use strict";
const Example = require('./Example')
function runIt() {
const ex = new Example()
ex.run()
.then(r => {console.log(r)})
.catch(e => {console.log(e)})
}
runIt()
解决方法
好吧,我从 Jest 切换到 Mocha,现在测试正常了。