mercredi 24 juin 2020

Generating jest test suites as separate files instead of single file

I need to run similar tests on a bunch of different files, and I do so using a single test suite file.

Example:

for (const configName in defaults) {
  const config = defaults[configName];
  const onMobile = testMobile.includes(configName);
  const onDesktop = testDesktop.includes(configName);
  describe(`${configName}`, () => {
    const tests = (isDesktop:boolean) => {
      let defaultConfig: AttnConfig<MultiPageCreativeConfig>;
      function freshRender() {
        cleanup();
        return render(
          <TestWrapper isDesktop={isDesktop} layout={defaultConfig.creativeConfig.base.fields.layout.layout}>
            <ConfigCtx.Provider value={defaultConfig}>
              <App />
            </ConfigCtx.Provider>
          </TestWrapper>,
          {
            container: document.documentElement
          }
        );
      }

      beforeEach(() => {
        jest.clearAllMocks();
        mockedUseWaitForPageLoad.mockReturnValue(false);
        mockedUseResponsiveLayout.mockReturnValue([isDesktop]);
        defaultConfig = attnTestConfigWrapper(config);
      });
      afterEach(cleanup);

      for (let page = 0; page < config.pages.length ;page++) {
        describe(`Page ${page + 1}`, () => {
          beforeEach(() => {
            defaultConfig.overrides.currentPageIndex = page;
            freshRender();
          });
          itPassesVisualRegressionTests(!isDesktop);
        });
      }
    }
    if (onMobile) tests(false);
    if (onDesktop) tests(true);
  })
}

This way however, does not take advantage of multithreading. Since I will only ever be running these tests alone, it takes considerably longer (about two-three times longer) than writing a separate test file for each config.

As much as I would like to write the tests in individual files, this causes a lot of extra work if I need to refactor something (I've already had to change these files several times).

Is there a way generate test suites for jest to run in parallel or at least break out test logic into a shared utility function?

Aucun commentaire:

Enregistrer un commentaire