Here is my function
const FS = require('fs');
const PARSER = new $RefParser;
const dereference = (filePath)=>{
let parsedJSON = PARSER.dereference(filePath,{dereference: {circular: false}});
return parsedJSON.then(result =>{
const dereferencedJSON = JSON.stringify(result,null,2);
FS.writeFileSync(filePath,dereferencedJSON);
}).catch((error)=>{
console.log(error.message);
});
};
module.exports = {
dereferenceSchema,
PARSER
};
here is the test
const {dereferenceSchema,PARSER} = require('../../src/dereferenceService');
describe('derefrencing service test',() =>{
it('Should throw ReferenceError if circular reference detected', done => {
beforeEach(()=>{
PARSER.dereference = jest.fn();
});
dereferenceSchema(CIRCULAR_REF_FILE).then((data) => {
console.log(data) // this gives me undefined
done();
})
});
});
I also tried
describe('derefrencing service test',() =>{
it('Should throw ReferenceError if circular reference detected', async () => {
expect.assertions(1);
return dereferenceSchema(CIRCULAR_REF_FILE).catch((e)=>{
expect(e).toEqual('ReferenceError');
}
);
});
});
//This will have Error: Expected one assertion to be called but received zero assertion calls.
The implementation of test above is always pass no matter the what condition is, however, there still has the ReferenceError in console, which comes from my catch of derefrenceSchema function.
I am totally new to jest and I have tried different method, either using something working withdone() or jest.fn().
I would be really appreciate it if someone can help me here.