Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: unify jest maxWorkers usage on CI and turn off code coverage #27680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/keyboard-key/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { createV8Config: createConfig } = require('@fluentui/scripts-jest');
Hotell marked this conversation as resolved.
Show resolved Hide resolved

const config = createConfig({
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.test.ts', '!node_modules/**'],
coverageDirectory: 'coverage',
coverageThreshold: {
Expand Down
5 changes: 3 additions & 2 deletions scripts/gulp/src/plugins/gulp-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sh } from '@fluentui/scripts-utils';

export type JestPluginConfig = {
config: string;
cache?: boolean;
coverage?: boolean;
detectLeaks?: boolean;
maxWorkers?: number;
Expand All @@ -25,12 +26,12 @@ const jest = (config: JestPluginConfig) => () => {
config.coverage && '--coverage',
config.watchAll && '--watchAll',
config.runInBand && '--runInBand',
// eslint-disable-next-line eqeqeq
config.maxWorkers != null && `--maxWorkers=${config.maxWorkers}`,
config.maxWorkers && `--maxWorkers=${config.maxWorkers}`,
config.detectLeaks && '--detectLeaks',
config.testNamePattern && `--testNamePattern="${config.testNamePattern}"`,
config.rootDir && `--rootDir ${config.rootDir}`,
config.verbose && '--verbose',
config.cache === true ? '' : '--no-cache',
config.testFilePattern, // !!! THIS ITEM MUST GO LAST IN THE ARRAY !!!
]
.filter(Boolean)
Expand Down
10 changes: 4 additions & 6 deletions scripts/gulp/src/tasks/test-unit.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { task, series } from 'gulp';
import { series, task } from 'gulp';
import yargs from 'yargs';

import jest, { JestPluginConfig } from '../plugins/gulp-jest';

const argv = yargs
.option('cache', {})
.option('runInBand', {})
.option('maxWorkers', {})
.option('detectLeaks', {})
.option('coverage', { default: true })
.option('coverage', {})
.option('testNamePattern', { alias: 't' })
.option('testFilePattern', { alias: 'F' }).argv;

const jestConfigFromArgv: Partial<JestPluginConfig> = {
cache: argv.cache as boolean,
runInBand: argv.runInBand as boolean,
coverage: argv.coverage as boolean,
maxWorkers: argv.maxWorkers as number,
Expand All @@ -20,10 +22,6 @@ const jestConfigFromArgv: Partial<JestPluginConfig> = {
testFilePattern: argv.testFilePattern as string,
};

if (process.env.TF_BUILD) {
jestConfigFromArgv.maxWorkers = 2;
}
Hotell marked this conversation as resolved.
Show resolved Hide resolved

task(
'test:jest',
jest({
Expand Down
3 changes: 3 additions & 0 deletions scripts/jest/src/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const isCI = Boolean(process.env.TF_BUILD);
Hotell marked this conversation as resolved.
Show resolved Hide resolved

exports.isCI = isCI;
4 changes: 4 additions & 0 deletions scripts/jest/src/jest.preset.v0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { getWorkspaceProjectsAliases } = require('@fluentui/scripts-monorepo');

const { isCI } = require('./environment');
const { workersConfig } = require('./shared');

// northstar packages should pull these from npm, not the repo
const excludedPackages = ['@fluentui/dom-utilities'];

Expand All @@ -18,6 +21,7 @@ const createConfig = (/** @type {import('@jest/types').Config.InitialOptions} */
testEnvironment: 'jsdom',
restoreMocks: true,
clearMocks: true,
...(isCI ? workersConfig : null),
...customConfig,
moduleNameMapper: {
...getWorkspaceProjectsAliases({
Expand Down
9 changes: 7 additions & 2 deletions scripts/jest/src/jest.preset.v8.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const fs = require('fs');
const path = require('path');

const { findRepoDeps } = require('@fluentui/scripts-monorepo');
const { findConfig, merge } = require('@fluentui/scripts-utils');
const fs = require('fs-extra');

const { isCI } = require('./environment');
const { workersConfig } = require('./shared');

const packageJsonPath = findConfig('package.json') ?? '';
const packageRoot = path.dirname(packageJsonPath);
Expand Down Expand Up @@ -31,7 +34,7 @@ const jestAliases = () => {
}

// Special aliases to look at src for the current package
const packageJson = fs.readJSONSync(packageJsonPath);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
aliases[`^${packageJson.name}$`] = '<rootDir>/src/';
aliases[`^${packageJson.name}/lib/(.*)$`] = '<rootDir>/src/$1';

Expand Down Expand Up @@ -81,6 +84,8 @@ const createConfig = (customConfig = {}) => {
restoreMocks: true,
clearMocks: true,

...(isCI ? workersConfig : null),

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
snapshotFormat: {
Expand Down
3 changes: 3 additions & 0 deletions scripts/jest/src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const workersConfig = { maxWorkers: 4 };

exports.workersConfig = workersConfig;
91 changes: 56 additions & 35 deletions scripts/tasks/src/jest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { jestTask, JestTaskOptions } from 'just-scripts';
import * as path from 'path';
import unparse from 'yargs-unparser';
import { getJustArgv, JustArgs } from './argv';
import { logger } from 'just-scripts';
// eslint-disable-next-line import/no-extraneous-dependencies
import { spawn } from 'just-scripts-utils';

const commonJestTask = (options: JestTaskOptions = {}) => {
import { JustArgs, getJustArgv } from './argv';

const commonJestTask = (options: JestTaskConfig = {}) => {
const {
runInBand = !!(process.env.TF_BUILD || process.env.LAGE_PACKAGE_NAME),
passWithNoTests = true,
nodeArgs,
_ = [],
// args for our just preset which should not be passed through to jest
Expand All @@ -19,40 +18,62 @@ const commonJestTask = (options: JestTaskOptions = {}) => {
registry,
// these args without explicit handling will be passed directly through to jest
...otherArgs
} = getJustArgv() as JustArgs & JestTaskOptions;

return jestTask({
runInBand,
passWithNoTests,
nodeArgs, // Just-specific config

_: [
// jestTask doesn't have explicit support for all jest args (https://jestjs.io/docs/en/cli),
// so unparse any extra args and pass them through here
...unparse({ ...otherArgs, _: [] }),
// and pass any positional args (to narrow down tests to be run)
..._.filter(arg => arg !== 'jest' && arg !== 'jest-watch'),
],

env: {
...process.env,
NODE_ENV: 'test',
PACKAGE_NAME: packageName,
},
...options,
});
} = getJustArgv() as JustArgs & JestTaskConfig;

return jestTask({ ...options, ...otherArgs });
};

export const jest = () => {
return commonJestTask();
};

export const jestDom = () =>
jestTask({
runInBand: true,
config: path.join(process.cwd(), 'jest.dom.config.js'),
});

export const jestWatch = () => {
return commonJestTask({ watch: true });
};

type JestTaskConfig = {
config?: string;
passWithNoTests?: boolean;
cache?: boolean;
clearCache?: boolean;
coverage?: boolean;
detectLeaks?: boolean;
maxWorkers?: number;
rootDir?: string;
runInBand?: boolean;
testNamePattern?: string;
testFilePattern?: string;
verbose?: boolean;
watchAll?: boolean;
watch?: boolean;
};

/**
*
* custom jest task as just-scripts jest task doesn't support maxWorkers setup and others
*/
const jestTask = (config: JestTaskConfig) => () => {
const cmd = 'jest';
const cache = config.cache !== undefined ? config.cache : true;
const passWithNoTests = config.passWithNoTests !== undefined ? config.passWithNoTests : true;
const args = [
cache === false && '--no-cache',
passWithNoTests && '--passWithNoTests',
config.config && `--config ${config.config}`,
config.rootDir && `--rootDir ${config.rootDir}`,
config.watch && '--watch',
config.watchAll && '--watchAll',
config.clearCache && '--clearCache',
config.coverage && '--coverage',
config.runInBand && '--runInBand',
config.maxWorkers && `--maxWorkers=${config.maxWorkers}`,
config.detectLeaks && '--detectLeaks',
config.testNamePattern && `--testNamePattern="${config.testNamePattern}"`,
config.verbose && '--verbose',
config.testFilePattern,
].filter(Boolean) as string[];

logger.info(cmd, args.join(' '));

return spawn(cmd, args, { stdio: 'inherit' });
};
Loading