【问题标题】:Oboe.js, Promises, and JestOboe.js、Promises 和 Jest
【发布时间】:2021-06-17 00:27:46
【问题描述】:

我正在使用Oboe.jsoboe-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


    【解决方案1】:

    好吧,我从 Jest 切换到 Mocha,现在测试工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2020-09-08
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      相关资源
      最近更新 更多