Skip to content

Commit

Permalink
fixup! fixup! fixup! test: improve jest execution speeds by removing …
Browse files Browse the repository at this point in the history
…-i and --coverage from v8,v0
  • Loading branch information
Hotell committed Apr 25, 2023
1 parent 1a64fd5 commit 7239b37
Showing 1 changed file with 24 additions and 51 deletions.
75 changes: 24 additions & 51 deletions scripts/tasks/src/jest.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import * as path from 'path';

import { JestTaskOptions, jestTask } from 'just-scripts';
import { JestTaskOptions, logger } from 'just-scripts';
// eslint-disable-next-line import/no-extraneous-dependencies
import { spawn } from 'just-scripts-utils';
import unparse from 'yargs-unparser';

import { JustArgs, getJustArgv } from './argv';

const commonJestTask = (options: JestTaskOptions = {}) => {
const {
// runInBand = !!(process.env.TF_BUILD || process.env.LAGE_PACKAGE_NAME),
runInBand,
passWithNoTests = true,
nodeArgs,
_ = [],
// args for our just preset which should not be passed through to jest
Expand All @@ -26,47 +20,22 @@ const commonJestTask = (options: JestTaskOptions = {}) => {
...otherArgs
} = getJustArgv() as JustArgs & JestTaskOptions;

return jestRaw({ runInBand, ...otherArgs } as unknown as JestPluginConfig);

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,
});
return jestTask({ ...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 });
};

export type JestPluginConfig = {
config: string;
export type JestTaskConfig = {
config?: string;
passWithNoTests?: boolean;
cache?: boolean;
clearCache?: boolean;
coverage?: boolean;
detectLeaks?: boolean;
maxWorkers?: number;
Expand All @@ -79,28 +48,32 @@ export type JestPluginConfig = {
watch?: boolean;
};

const jestRaw = (config: JestPluginConfig) => () => {
/**
*
* 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;
// process.env.NODE_ENV = 'test';
// Alias env variables as Azure Pipelines do not set it
// process.env.CI = process.env.TF_BUILD ? 'true' : undefined;

// in watch mode jest never exits
// let the gulp task complete to prevent blocking subsequent tasks
const passWithNoTests = config.passWithNoTests !== undefined ? config.passWithNoTests : true;
const args = [
// `jest --config ${config.config}`,
cache === false && '--no-cache',
passWithNoTests && '--passWithNoTests',
config.config && `--config ${config.config}`,
config.rootDir && `--rootDir ${config.rootDir}`,
config.watch && '--watch',
config.coverage && '--coverage',
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.rootDir && `--rootDir ${config.rootDir}`,
config.verbose && '--verbose',
cache === false && '--no-cache',
config.testFilePattern, // !!! THIS ITEM MUST GO LAST IN THE ARRAY !!!
config.testFilePattern,
].filter(Boolean) as string[];
console.log(`jest ${args.join(' ')}`);
return spawn('jest', args, { stdio: 'inherit' });

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

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

0 comments on commit 7239b37

Please sign in to comment.