3

I want to write tests for promise result and I dot'n want to resolve promise in each it/pit section.

I need smth like this:

describe('getData() results test', () => { 
  return getData().then(response =>  { 
    it('foo', () => expect(response.foo).toEqual(1));

    it('bar', () => expect(response.bar).toEqual(2));

    it('bar', () => expect(response.bar).toEqual(3));
  });
});

If use beforeEach - promise will be resolved as many times as number of it sections. I need to resolve it once and then test response. There are a lot of test cases so I want to split all tests into it sections

Rick Hanlon II
  • 20,549
  • 7
  • 47
  • 53
tuchk4
  • 2,270
  • 5
  • 21
  • 34
  • Related, if not duplicate: [How to pass variable from beforeEach hook to tests in jest?](https://stackoverflow.com/q/52397708/1048572), [How to share data between beforeAll / beforeEach and tests in Jest?](https://stackoverflow.com/q/48528502/1048572) – Bergi Nov 11 '21 at 22:51

4 Answers4

8

The beforeAll function is called only once before all the specs in describe are run.

When return promise, Jest will wait for the promise to resolve before letting the test run.

describe('getData() results test', () => { 
  let data = null;

  beforeAll(() => getData().then(response => {
    data = response;
  }));

  it('foo', () => expect(data.foo).toEqual(1));

  it('bar', () => expect(data.bar).toEqual(2));

  it('bar', () => expect(data.bar).toEqual(3));
});
tuchk4
  • 2,270
  • 5
  • 21
  • 34
1

Have a look at the Async tutorial from the Jest documentation. I believe you need something like this:

describe('getData() results test', () => { 
    var response;
    beforeEach(() => {
        response = getData();
    });

    it('foo', () => { return response.then(r => expect(r.foo).toEqual(1))});

    it('bar', () => { return response.then(r => expect(r.bar).toEqual(2))});

    it('bar', () => { return response.then(r => expect(r.bar).toEqual(3))});
});

Key bit of the docs:

The promise that is being tested should be returned.

tonicsoft
  • 1,736
  • 9
  • 22
  • in your example promise will be resolved 3 times. I need to resolve it once and then tests response. There are a lot of test cases so I want to split all tests into `it` sections – tuchk4 Sep 20 '16 at 06:52
  • `The promise that is being tested should be returned.` there was `pit` section for promises. But seems now `it` could be used – tuchk4 Sep 20 '16 at 06:54
1

This is not an answer to your question but it may help people comming from Google

Jest will await the promises returned from beforeAll or beforeEach as @tuchk4 mentioned.

In my case, I forgot the it function and implemented the test right into describe. Surprisingly enough, this "works" but the promises from beforeAll and beforeEach will not be awaited and this makes all sense

I said "works" on quote because failed expect() will indeed fail but the Promise they return will not be awaited which will cause a UnhandledPromiseRejectionWarning.

Andre Pena
  • 56,650
  • 48
  • 196
  • 243
-2

You can simply do the following

describe('some test suite title', () => {
   let response;

   beforeAll(async (done) => {
    // Do some Async. logic before running any test
    await require('./somefile');
    response = await getData();
    done();
  });

    // Now run tests => response have the data
    it('foo', () => expect(response.foo).toEqual(1));

    it('bar', () => expect(response.bar).toEqual(2));

    it('bar', () => expect(response.bar).toEqual(3));
  
});
lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
Ahmed Abdelrahman
  • 778
  • 3
  • 12
  • 30