diff --git a/src/Runner.js b/src/Runner.js index f530331..8f0440f 100644 --- a/src/Runner.js +++ b/src/Runner.js @@ -61,7 +61,7 @@ export default class Runner extends EventEmitter { // Handle the arg change on v18 const belowEighteen = this.workspace.localJestMajorVersion < 18; const outputArg = belowEighteen ? '--jsonOutputFile' : '--outputFile'; - const args = ['--testLocationInResults', '--json', '--useStderr', outputArg, this.outputPath]; + let args = ['--testLocationInResults', '--json', '--useStderr', outputArg, this.outputPath]; if (this.watchMode) { args.push(this.watchAll ? '--watchAll' : '--watch'); } @@ -88,6 +88,12 @@ export default class Runner extends EventEmitter { if (this.options.args) { args.push(...this.options.args.args); } + if (this.workspace.useDashedArgs) { + args = args.map((arg) => + arg && arg.startsWith('--') && arg.length > 2 ? arg.replace(/(\B)([A-Z])/gm, '-$2').toLowerCase() : arg + ); + } + return args; } diff --git a/src/__tests__/runner.test.js b/src/__tests__/runner.test.js index f1f98fc..23107a1 100644 --- a/src/__tests__/runner.test.js +++ b/src/__tests__/runner.test.js @@ -302,6 +302,57 @@ describe('Runner', () => { const lastIndex = args.lastIndexOf('--reporters'); expect(args[lastIndex + 1]).toBe(expected[1]); }); + + it('calls createProcess with camel cased args by default', () => { + const expected = '--testLocationInResults'; + + const workspace: any = {}; + const options = {}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + expect(args[0]).toBe(expected); + }); + + it('calls createProcess with dashed args when provided', () => { + const expected = '--test-location-in-results'; + + const workspace: any = {useDashedArgs: true}; + const options = {}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + expect(args[0]).toBe(expected); + }); + + it('converts user passed in args', () => { + const expected = '--foo-bar-baz'; + + const workspace: any = {useDashedArgs: true}; + const options = {args: {args: ['--fooBarBaz']}}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + const index = args.indexOf(expected); + expect(index).not.toBe(-1); + }); + + it('does not alter already dashed args when provided', () => { + const expected = '--foo-bar-baz'; + + const workspace: any = {useDashedArgs: true}; + const options = {args: {args: [expected]}}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + const index = args.indexOf(expected); + expect(index).not.toBe(-1); + }); + describe('RunArgs', () => { it.each` runArgs | containedArgs diff --git a/src/project_workspace.ts b/src/project_workspace.ts index df8981c..df91e75 100644 --- a/src/project_workspace.ts +++ b/src/project_workspace.ts @@ -26,6 +26,7 @@ export interface ProjectWorkspaceConfig { debug?: boolean; nodeEnv?: {[key: string]: string | undefined}; shell?: string | LoginShell; + useDashedArgs?: boolean; } /** @@ -121,6 +122,11 @@ export default class ProjectWorkspace { */ shell?: string | LoginShell; + /** + * Wether dashed args should be used for the jest command line. Default is false. + */ + useDashedArgs?: boolean; + constructor( rootPath: string, jestCommandLine: string, @@ -130,7 +136,8 @@ export default class ProjectWorkspace { collectCoverage?: boolean, debug?: boolean, nodeEnv?: {[key: string]: string | undefined}, - shell?: string | LoginShell + shell?: string | LoginShell, + useDashedArgs?: boolean ) { this.rootPath = rootPath; this.jestCommandLine = jestCommandLine; @@ -141,6 +148,7 @@ export default class ProjectWorkspace { this.debug = debug; this.nodeEnv = nodeEnv; this.shell = shell; + this.useDashedArgs = useDashedArgs; } } @@ -160,6 +168,7 @@ export const createProjectWorkspace = (config: ProjectWorkspaceConfig): ProjectW config.collectCoverage, config.debug, config.nodeEnv, - config.shell + config.shell, + config.useDashedArgs ); };