Friday 14 April 2017

Jest: resolves return undefined from Promise

I am using jest for testing. I want to test the return of my function:

const xml2js = require('xml2js')

const parser = new xml2js.Parser()

function opmlParser(xmlData) {
  return new Promise((resolve, reject) => {
    parser.parseString(xmlData, (err, data) => {
      if (err !== null && data !== null) reject(err)
      resolve({})
    })
  })
}

The promise resolves {} in order to be in simple test case.

So, I want to test to following function, and I expect the result to be a promise containing {}.
I have the following code:

const fs = require('fs');

test('minimal-opml', () => {
  const xmlData = fs.readFileSync('test/opml-parser/opml/test1.opml')
  return expect(opmlParser(xmlData)).resolves.toEqual({})
})

As the Jest documentation say, I shoud use resolves statement before matching result and use return.

But, I got an issue:

TypeError: Cannot read property 'toEqual' of undefined

resolves return undefined, so I can't continue to test values.

I tried to add global.Promise = require.requireActual('promise'), but stills not work.

Do you have any idea what wrong I am doing?



via Frilox

No comments:

Post a Comment