Skip to content

Commit

Permalink
add useDashedArgs workspace setting (#103)
Browse files Browse the repository at this point in the history
* add useDashedArgs workspace setting

* add more tests

* use map instead of forEach
  • Loading branch information
mjamin committed Jun 13, 2023
1 parent a6d6186 commit 28a0be7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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;
}

Expand Down
51 changes: 51 additions & 0 deletions src/__tests__/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src/project_workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ProjectWorkspaceConfig {
debug?: boolean;
nodeEnv?: {[key: string]: string | undefined};
shell?: string | LoginShell;
useDashedArgs?: boolean;
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -141,6 +148,7 @@ export default class ProjectWorkspace {
this.debug = debug;
this.nodeEnv = nodeEnv;
this.shell = shell;
this.useDashedArgs = useDashedArgs;
}
}

Expand All @@ -160,6 +168,7 @@ export const createProjectWorkspace = (config: ProjectWorkspaceConfig): ProjectW
config.collectCoverage,
config.debug,
config.nodeEnv,
config.shell
config.shell,
config.useDashedArgs
);
};

0 comments on commit 28a0be7

Please sign in to comment.