Skip to content

Commit

Permalink
chore: improve jest speed on both local and CI (#32096)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell authored Jul 25, 2024
1 parent 1e212a4 commit daafbc0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
13 changes: 13 additions & 0 deletions jest.preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const tsPathAliases = pathsToModuleNameMapper(tsConfig.compilerOptions.paths, {
prefix: `<rootDir>/${path.relative(process.cwd(), __dirname)}/`,
});

const isCI = Boolean(process.env.TF_BUILD);

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
Expand All @@ -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 = {
Expand Down
3 changes: 0 additions & 3 deletions scripts/jest/src/environment.js

This file was deleted.

3 changes: 1 addition & 2 deletions scripts/jest/src/jest.preset.v0.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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({
Expand Down
3 changes: 1 addition & 2 deletions scripts/jest/src/jest.preset.v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') ?? '';
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion scripts/jest/src/shared.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit daafbc0

Please sign in to comment.