Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add useDashedArgs workspace setting #103

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add some test case to ensure user passed in args (options.args.args) will also be converted. Also add test cases to ensure if any of the arguments are already in dashed style, it will not be altered.


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
);
};