首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oboe.js,承诺,和Jest

Oboe.js,承诺,和Jest
EN

Stack Overflow用户
提问于 2021-03-19 15:15:25
回答 1查看 85关注 0票数 2

我使用的是Oboe.js双簧管-承诺 (承诺中封装双簧管调用)和取笑。我可能在做傻事。有什么建议吗?谢谢。

我的代码

代码语言:javascript
复制
"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

我的测试文件

代码语言:javascript
复制
"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 ms超时内调用”。我添加了jest.setTimeout(60000)并得到了相同的结果。我还有其他Jest测试,测试异步非双簧管代码,它们使用异步/等待很好地工作。

代码语言:javascript
复制
test('run', async () => {
    expect(await <codeToTest>).toEqual(<expected>)
  })

模式。

如果我在Jest之外运行代码时,代码可以使用:

代码语言:javascript
复制
"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()
EN

回答 1

Stack Overflow用户

发布于 2021-03-20 19:39:02

我从Jest转到Mocha,现在测试成功了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66710835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档