lundi 12 avril 2021

How pass a test about async method with Jest

I try to write and run a test about an asynchronous method doSomething that return a Promise<Foo[]>. I use NestJS for a backend application.

Accoring to the Jest documentation it is possible and it seems to be quite easy.

I do

test('Test something', () => {
      const oldFoo = getFoo(); // type Foo[]
      const newFoo = getFoo();
      const expect = getFoo();

      return doSomething(newFoo, oldFoo).then((result) => {
        const cmp: boolean = helper.equal(expect, result);
        expect(cmp).toEqual(true);
      });
    }, 5000);

I tried too a simplier way to write that

test('Test something', async () => {
      const oldFoo = getFoo(); // type Foo[]
      const newFoo = getFoo();
      const expect = getFoo();

      const result = await doSomething(newFoo, oldFoo);

      expect(helper.equal(expect, result)).toEqual(true);
      });
    }, 5000);

I get back this error :

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout

I got the same error whatever the value of the timeout.

My question : is there any explanation other than the tested method is really too long ? Because out of the test (during runtime) the method is quite fast.

Can anyone help me to find the way to make it working ?

Aucun commentaire:

Enregistrer un commentaire