Reputation: 855
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?
Upvotes: 0
Views: 3840
Reputation: 855
The resolves
methode is avalaible from 20.0.0+. So it is not already avalable. source
This is why it return undefined
.
Upvotes: 3
Reputation: 2407
You have a few issues in checking for an error in the opmlParser
function. You want to check if there is an error and reject, else resolve.
Try this (brackets for clarity, and you may need to refine this to your specific case):
if (err !== null) {
reject(err)
} else {
resolve({})
}
Upvotes: 2