Skip to content

Commit

Permalink
feat: pass test runner options (#511)
Browse files Browse the repository at this point in the history
* feat: pass arguments after `--` directly to Jest runner

* feat: passthrough test runner args after `--`

* refactor: rebase and tweaks
  • Loading branch information
mdjastrzebski committed Aug 28, 2024
1 parent cd71fa7 commit 018cd40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-ants-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@callstack/reassure-cli': minor
---

Passthrough args after -- to Jest
29 changes: 18 additions & 11 deletions packages/cli/src/commands/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export interface MeasureOptions extends CommonOptions {
commitHash?: string;
testMatch?: string[];
testRegex?: string[];
enableWasm?: boolean;
/** Rest argument used for flags after `--` separator, will be passed to test runner. */
_?: string[];
}

export async function run(options: MeasureOptions) {
Expand All @@ -46,6 +47,9 @@ export async function run(options: MeasureOptions) {
const header = { metadata };
writeFileSync(outputFile, JSON.stringify(header) + '\n');

const nodeMajorVersion = getNodeMajorVersion();
logger.verbose(`Node.js version: ${nodeMajorVersion} (${process.versions.node})`);

const testRunnerPath = process.env.TEST_RUNNER_PATH ?? getJestBinPath();
if (!testRunnerPath) {
logger.error(
Expand All @@ -55,12 +59,15 @@ export async function run(options: MeasureOptions) {
return;
}

const testRunnerArgs = process.env.TEST_RUNNER_ARGS ?? buildDefaultTestRunnerArgs(options);

const nodeMajorVersion = getNodeMajorVersion();
logger.verbose(`Node.js version: ${nodeMajorVersion} (${process.versions.node})`);
const baseTestRunnerArgs = process.env.TEST_RUNNER_ARGS ?? buildDefaultTestRunnerArgs(options);
const passthroughTestRunnerArgs = options._ ?? [];

const nodeArgs = [...getNodeFlags(nodeMajorVersion), testRunnerPath, testRunnerArgs];
const nodeArgs = [
...getNodeFlags(nodeMajorVersion),
testRunnerPath,
...baseTestRunnerArgs,
...passthroughTestRunnerArgs,
];
logger.verbose('Running tests using command:');
logger.verbose(`$ node \\\n ${nodeArgs.join(' \\\n ')}\n`);

Expand Down Expand Up @@ -145,23 +152,23 @@ export const command: CommandModule<{}, MeasureOptions> = {
handler: (args) => run(args),
};

function buildDefaultTestRunnerArgs(options: MeasureOptions) {
function buildDefaultTestRunnerArgs(options: MeasureOptions): string[] {
if (options.testMatch && options.testRegex) {
logger.error('Configuration options "testMatch" and "testRegex" cannot be used together.');
process.exit(1);
}

const commonArgs = '--runInBand';
const commonArgs = ['--runInBand'];

if (options.testMatch) {
return `${commonArgs} --testMatch=${toShellArray(options.testMatch)}`;
return [...commonArgs, `--testMatch=${toShellArray(options.testMatch)}`];
}

if (options.testRegex) {
return `${commonArgs} --testRegex=${toShellArray(options.testRegex)}`;
return [...commonArgs, `--testRegex=${toShellArray(options.testRegex)}`];
}

return `${commonArgs} --testMatch=${toShellArray(DEFAULT_TEST_MATCH)}`;
return [...commonArgs, `--testMatch=${toShellArray(DEFAULT_TEST_MATCH)}`];
}

function toShellArray(texts: string[]): string {
Expand Down

0 comments on commit 018cd40

Please sign in to comment.