【发布时间】:2021-06-17 00:27:46
【问题描述】:
我正在使用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', value: 5}}, {obj2: {value: 0, name: 'other'}}]))
})
})
当我运行我的 Jest 测试时,我得到“在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”我添加了 jest.setTimeout(60000) 并得到相同的结果。我还有其他测试异步非双簧管代码的 Jest 测试,它们使用 async/await 可以正常工作
test('run', async () => {
expect(await <codeToTest>).toEqual(<expected>)
})
模式。
如果我在 Jest 之外使用以下代码运行代码,则代码可以工作:
"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()
【问题讨论】:
标签: javascript jestjs oboe.js