Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/jest-runner/fuzz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ export const skip: (globals: Global.Global) => FuzzTest =

export type JestTestMode = "skip" | "only" | "standard";

function printTestNameIfRequested(testStatePath: string[]) {
const full_name: string = testStatePath.join(" ");
if (process.env.JAZZER_LIST_FUZZTEST_NAMES) {
if (
process.env.JAZZER_LIST_FUZZTEST_NAMES_PATTERN == undefined ||
full_name.match(process.env.JAZZER_LIST_FUZZTEST_NAMES_PATTERN)

Check failure

Code scanning / CodeQL

Regular expression injection

This regular expression is constructed from a [environment variable](1).
) {
if (process.env.JAZZER_LIST_FUZZTEST_NAMES == "short") {
const short_name: string = testStatePath.pop() || "";
console.log(short_name);
} else if (process.env.JAZZER_LIST_FUZZTEST_NAMES == "split") {
const split_name: string = testStatePath.join(" / ");
console.log(split_name);
} else {
console.log(full_name);
}
}
}
}

export function fuzz(
globals: Global.Global,
testFile: string,
Expand All @@ -72,6 +92,9 @@ export function fuzz(
// only the requested tests are executed.
const testStatePath = currentTestStatePath(toTestName(name), state);
const testNamePattern = originalTestNamePattern();

printTestNameIfRequested(testStatePath);

const skip =
testStatePath !== undefined &&
testNamePattern != undefined &&
Expand Down
40 changes: 39 additions & 1 deletion tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class FuzzTest {
seed,
sync,
verbose,
listFuzzTestNames,
listFuzzTestNamesPattern,
coverage,
expectedErrors,
asJson,
Expand All @@ -68,6 +70,8 @@ class FuzzTest {
this.seed = seed;
this.sync = sync;
this.verbose = verbose;
this.listFuzzTestNames = listFuzzTestNames;
this.listFuzzTestNamesPattern = listFuzzTestNamesPattern;
this.coverage = coverage;
this.expectedErrors = expectedErrors;
this.asJson = asJson;
Expand Down Expand Up @@ -170,8 +174,17 @@ class FuzzTest {
"--no-colors",
this.asJson ? "--json" : "",
this.coverage ? "--coverage" : "",
this.listFuzzTestNames ? "--reporters=" : "",
];
this.runTest(cmd, options, { ...process.env });
let env = { ...process.env };
if (this.listFuzzTestNames) {
env.JAZZER_LIST_FUZZTEST_NAMES = "1";
}
if (this.listFuzzTestNamesPattern) {
env.JAZZER_LIST_FUZZTEST_NAMES_PATTERN = this.listFuzzTestNamesPattern;
}

this.runTest(cmd, options, env);
}

runTest(cmd, options, env) {
Expand Down Expand Up @@ -204,6 +217,8 @@ class FuzzTestBuilder {
_dryRun = undefined;
_runs = undefined;
_verbose = false;
_listFuzzTestNames = false;
_listFuzzTestNamesPattern = undefined;
_fuzzEntryPoint = "";
_dir = "";
_fuzzFile = "fuzz";
Expand Down Expand Up @@ -248,6 +263,27 @@ class FuzzTestBuilder {
return this;
}

/**
* @param {boolean} listFuzzTestNames - whether to list all fuzz test names on the console. True by
* default.
*/
listFuzzTestNames(listFuzzTestNames) {
this._listFuzzTestNames =
listFuzzTestNames === undefined ? true : listFuzzTestNames;
if (this._listFuzzTestNames) {
this.jestTestName("__NOT_AN_ACTUAL_TESTNAME__");
}
return this;
}

/**
* @param {boolean} listFuzzTestNamesPattern - pattern to filter the list of all fuzz test names.
*/
listFuzzTestNamesPattern(listFuzzTestNamesPattern) {
this._listFuzzTestNamesPattern = listFuzzTestNamesPattern;
return this;
}

/**
* @param {boolean} dryRun
*/
Expand Down Expand Up @@ -424,6 +460,8 @@ class FuzzTestBuilder {
this._seed,
this._sync,
this._verbose,
this._listFuzzTestNames,
this._listFuzzTestNamesPattern,
this._coverage,
this._expectedErrors,
this._asJson,
Expand Down
60 changes: 60 additions & 0 deletions tests/jest_integration/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,66 @@ describe("Jest integration", () => {
});
});
});

describe("List all fuzz tests", () => {
const listFuzzTestNamesTestBuilder = new FuzzTestBuilder()
.dir(projectDir)
.listFuzzTestNames()
.jestTestFile(jestTestFile + ".js");

it("lists fuzz tests", () => {
const listFuzzTestNames = listFuzzTestNamesTestBuilder.build();

listFuzzTestNames.execute();

expect(listFuzzTestNames.stdout).toContain(
"Jest Integration execute sync test",
);
expect(listFuzzTestNames.stdout).toContain(
"Run mode skip and standard standard test",
);
});

it("filters fuzz tests by name", () => {
const listFuzzTestNames = listFuzzTestNamesTestBuilder
.listFuzzTestNamesPattern("sync test")
.build();

listFuzzTestNames.execute();

expect(listFuzzTestNames.stdout).toContain(
"Jest Integration execute sync test",
);
expect(listFuzzTestNames.stdout).not.toContain(
"Run mode skip and standard standard test",
);
});

it("filters fuzz tests by describe name", () => {
const listFuzzTestNames = listFuzzTestNamesTestBuilder
.listFuzzTestNamesPattern("Jest")
.build();

listFuzzTestNames.execute();

expect(listFuzzTestNames.stdout).toContain(
"Jest Integration execute sync test",
);
expect(listFuzzTestNames.stdout).not.toContain(
"Run mode skip and standard standard test",
);
});

it("prints nothing else on stdout", () => {
const listFuzzTestNames = listFuzzTestNamesTestBuilder
.listFuzzTestNamesPattern("__NOT_AN_ACTUAL_TESTNAME__")
.build();

listFuzzTestNames.execute();

expect(listFuzzTestNames.stdout).toBe("");
});
});
});

// Deflake the "timeout after N seconds" test to be more tolerant to small variations of N (+-1).
Expand Down