From daafbc06490019171a0ca3151b5590ba337a698b Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Thu, 25 Jul 2024 14:54:42 +0200 Subject: [PATCH] chore: improve jest speed on both local and CI (#32096) --- jest.preset.js | 13 +++++++++++++ scripts/jest/src/environment.js | 3 --- scripts/jest/src/jest.preset.v0.js | 3 +-- scripts/jest/src/jest.preset.v8.js | 3 +-- scripts/jest/src/shared.js | 12 +++++++++++- 5 files changed, 26 insertions(+), 8 deletions(-) delete mode 100644 scripts/jest/src/environment.js diff --git a/jest.preset.js b/jest.preset.js index 1a4492c352a21..c7f77c03de37d 100644 --- a/jest.preset.js +++ b/jest.preset.js @@ -9,6 +9,8 @@ const tsPathAliases = pathsToModuleNameMapper(tsConfig.compilerOptions.paths, { prefix: `/${path.relative(process.cwd(), __dirname)}/`, }); +const isCI = Boolean(process.env.TF_BUILD); + /** * @type {import('@jest/types').Config.InitialOptions} */ @@ -29,6 +31,17 @@ const baseConfig = { escapeString: true, printBasicPrototype: true, }, + /** + * **Local Machine:** + * + * jest is resource greedy. on local machine it will spawn workers into all your CPU threads which will provide additional heat up of your machine and everything else will be blocked. + * Based on tests on local machine, 50% of CPU threads is the best value for local development ( also the fastest). + * + * **CI:** + * + * based on testing not spawning additional workers and rely on task orchestrator (NX) parallelization is fastest on our CI env atm ( 8 Core machine, 16GB RAM) + */ + maxWorkers: isCI ? 1 : '50%', }; module.exports = { diff --git a/scripts/jest/src/environment.js b/scripts/jest/src/environment.js deleted file mode 100644 index d74fcd0a0386d..0000000000000 --- a/scripts/jest/src/environment.js +++ /dev/null @@ -1,3 +0,0 @@ -const isCI = Boolean(process.env.TF_BUILD); - -exports.isCI = isCI; diff --git a/scripts/jest/src/jest.preset.v0.js b/scripts/jest/src/jest.preset.v0.js index fb69b222cd275..49d114d83535a 100644 --- a/scripts/jest/src/jest.preset.v0.js +++ b/scripts/jest/src/jest.preset.v0.js @@ -1,6 +1,5 @@ const { getWorkspaceProjectsAliases } = require('@fluentui/scripts-monorepo'); -const { isCI } = require('./environment'); const { workersConfig } = require('./shared'); // northstar packages should pull these from npm, not the repo @@ -21,7 +20,7 @@ const createConfig = (/** @type {import('@jest/types').Config.InitialOptions} */ testEnvironment: 'jsdom', restoreMocks: true, clearMocks: true, - ...(isCI ? workersConfig : null), + ...workersConfig, ...customConfig, moduleNameMapper: { ...getWorkspaceProjectsAliases({ diff --git a/scripts/jest/src/jest.preset.v8.js b/scripts/jest/src/jest.preset.v8.js index ac3924e0633f9..84697ed4a7407 100644 --- a/scripts/jest/src/jest.preset.v8.js +++ b/scripts/jest/src/jest.preset.v8.js @@ -4,7 +4,6 @@ const path = require('path'); const { findRepoDeps } = require('@fluentui/scripts-monorepo'); const { findConfig, merge } = require('@fluentui/scripts-utils'); -const { isCI } = require('./environment'); const { workersConfig } = require('./shared'); const packageJsonPath = findConfig('package.json') ?? ''; @@ -84,7 +83,7 @@ const createConfig = (customConfig = {}) => { restoreMocks: true, clearMocks: true, - ...(isCI ? workersConfig : null), + ...workersConfig, watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'], // OLD format for migration to jest 29 - TODO: migrate to new format . https://jestjs.io/blog/2022/04/25/jest-28#future diff --git a/scripts/jest/src/shared.js b/scripts/jest/src/shared.js index 56ff838a94135..fc5c5cc4a9423 100644 --- a/scripts/jest/src/shared.js +++ b/scripts/jest/src/shared.js @@ -1,3 +1,13 @@ -const workersConfig = { maxWorkers: 4 }; +/** + * **Local Machine:** + * + * jest is resource greedy. on local machine it will spawn workers into all your CPU threads which will provide additional heat up of your machine and everything else will be blocked. + * Based on tests on local machine, 50% of CPU threads is the best value for local development ( also the fastest). + * + * **CI:** + * + * based on testing spawning only 50% of available workers is fastest on both Local Machine and CI env atm ( 8 Core machine, 16GB RAM) + */ +const workersConfig = { maxWorkers: '50%' }; exports.workersConfig = workersConfig;