Given the following test code:
import { MyParentClass } from "MyParentClass";
import { MyClass } from "MyClass";
MyClass.prototype.post = jest.fn(() => Promise.resolve({token: '12345'}));
it('first test: this test will be successful tested ✓', async() => {
const myParentClass = new MyParentClass();
await expect(myParentClass.run()).toEqual({token: '12345'});
})
it('second test: this test will fail ×', async () => {
MyClass.prototype.post = jest.fn(() => Promise.reject({message: 'An error ocurred'}));
const myParentClass = new MyParentClass();
await expect(myParentClass.run()).rejects.toEqual({message: 'success'});
})
The "run
" method internally makes use of the 'MyClass
' class and its "post
" method. When I run a code like this, for my second test, I get a message like the following:
"Received promise resolved instead of rejected"
I understand that when the answer for the "post
" method is globally defined, it will always take that value, but I also understand that in the second test I am overwriting
the behavior of that method, why don't you take it into account?
I know I can also use jest.doMock
, but I don't understand the documentation well, could someone help me understand it to apply it to my example?
Aucun commentaire:
Enregistrer un commentaire