jimhigson / oboe.js

A streaming approach to JSON. Oboe.js speeds up web applications by providing parsed objects before the response completes.
http://oboejs.com
Other
4.77k stars 209 forks source link

Oboe.js, Promises, and Jest #230

Closed davout1806 closed 3 years ago

davout1806 commented 3 years ago

I'm trying to use Jest to test my promisized oboe usage, using oboe-promise as mentioned in #168. My problem is Jest times out but running outside of Jest via node command line it works. Any ideas? Thanks.

My code

"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

My test file

"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'}}]))
  })
})

When I run my Jest test I get "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout." I've added jest.setTimeout(60000) and get same result. I have other Jest tests that test asynchronous non-oboe code and they work fine using the async/await

test('run', async () => {
    expect(await <codeToTest>).toEqual(<expected>)
  })

pattern.

The code works if I run the code outside of Jest using:

"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()
System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 7.6.3 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.4.1 => 26.6.3 
davout1806 commented 3 years ago

Well I switched from Jest to Mocha and now the tests work.