How to correctly achieve such test suite archtecture with Jest?

Viewed 89

I have checked all the guides and examples on the Jest documentation website, but haven't found anything, that could provide an answer for my question.

I am looking for a pattern which allows me to run test cases for many different functions (in my case it's getters) separately, with Jest.

As for now, in my example.test.ts file, I have the following:

import { getterOne, getterTwo, getterThree, ... } from getters;

describe('test suite', () => {
  beforeAll(async () => {
    //connect to MongoDB
  })

  afterAll(async () => {
    //disconnect from MongoDB
  })

  //request token from DB, to check it
  it('key', async () => {
    const key = await KeysModel.findOne(_id: 'any').lean();
    expect(key).toMatchSnapshot({
      field: expect.any(String),
    });
  });

  /**
   * predefine key and some other things
   * but I can't call key here, because describe doesn't support
   * async/await, as it should be synchronous
   */


  //test separate getterOne with key variable as an argument

  //test separate getterTwo with key variable as an argument

  //test separate getterThree with key variable as an argument
})

But as we all know, I couldn't use the already defined key inside the it case. So I could define key once more on the top level, but:

  • Is it ok to re-define the key variable for each getter test case? If I have so many getters, I'll have xN absolutely useless requests.
  • As I understand it, I couldn't use async/await inside describe, because returning a Promise from "describe" is not supported. Tests must be defined synchronously.

So I am not asking, how to write code, but what is the correct pattern to writing a dependents test cases, in scenarios like this?

Could someone provide an example? Should I write more test inside describe or the common practice is a bit different from that?

What Jest methods should be used and in which order?

As for now, I am declaring empty variable via let keyword, and use them, like that:

describe('test suite', () => {
  beforeAll(async () => {
    //connect to MongoDB
  })

  afterAll(async () => {
    //disconnect from MongoDB
  })

  describe('inside', () => {
    let key;

    beforeAll(async () => {
      key = await KeyModel.findOne();
    })
   
    test('getterOne', async () => {
      const res = await getterOne(key);
      expect(res).toMatchSnapshot({
        id: expect.any(Number)
      });
    });

    //other test with keys..
  
  })
})

Does it consider as a good practice?

1 Answers

As for now, I am using the same structure, as was proposed. But I also thankful to Estus Flask for pointing me in this direction. I don't use mockups, mainly because I test not just functions, but also a DB presence and data persistence.

describe('test suite', () => {
  beforeAll(async () => {
    //connect to MongoDB
  })

  afterAll(async () => {
    //disconnect from MongoDB
  })

  /**
   * another describe stage below
   * with its own after and before and describe stages
   */

})

Related