diff --git a/packages/reassure-cli/src/commands/measure.ts b/packages/reassure-cli/src/commands/measure.ts index 66e2dded..d87291e1 100644 --- a/packages/reassure-cli/src/commands/measure.ts +++ b/packages/reassure-cli/src/commands/measure.ts @@ -10,7 +10,10 @@ import { applyCommonOptions, CommonOptions } from '../options'; import { getGitBranch, getGitCommitHash } from '../utils/git'; import { configureLoggerOptions } from '../utils/logger'; +const RUNNERS: string[] = ['jest', 'vitest'] as const; + export interface MeasureOptions extends CommonOptions { + runner: string; // default value is provided so not marking as optional baseline?: boolean; compare?: boolean; branch?: string; @@ -40,10 +43,17 @@ export async function run(options: MeasureOptions) { const header = { metadata }; writeFileSync(outputFile, JSON.stringify(header) + '\n'); - const testRunnerPath = process.env.TEST_RUNNER_PATH ?? getJestBinPath(); + if (!RUNNERS.includes(options.runner)) { + logger.error(`❌ Invalid runner provided: ${options.runner}. Valid options are 'jest' and 'vitest'`); + process.exitCode = 1; + return; + } + + const testRunnerPath = process.env.TEST_RUNNER_PATH ?? getBinPath(options.runner); + if (!testRunnerPath) { logger.error( - `❌ Unable to find Jest binary path. Pass explicit $TEST_RUNNER_PATH env variable to resolve the issue.` + `❌ Unable to find test runner binary path for ${options.runner}. Pass explicit $TEST_RUNNER_PATH env variable to resolve the issue.` ); process.exitCode = 1; return; @@ -145,17 +155,22 @@ export const command: CommandModule<{}, MeasureOptions> = { type: 'string', default: undefined, describe: 'Run performance tests for a specific test file', + }) + .option('runner', { + type: 'string', + default: 'jest', + describe: 'Specify which test runner to use. Valid values are: jest (default) and vitest.', }); }, handler: (args) => run(args), }; -export function getJestBinPath() { +export function getBinPath(runner: string) { try { // eslint-disable-next-line import/no-extraneous-dependencies - const jestPackageJson = require('jest/package.json'); - const jestPackagePath = dirname(require.resolve('jest/package.json')); - return resolve(jestPackagePath, jestPackageJson.bin.jest || jestPackageJson.bin); + const runnerPackageJson = require(`${runner}/package.json`); + const runnerPackagePath = dirname(require.resolve(`${runner}/package.json`)); + return resolve(runnerPackagePath, runnerPackageJson.bin['runner'] || runnerPackageJson.bin); } catch (error) { return null; }