Skip to content

Commit e3c485a

Browse files
committed
Fuzz tests now support timeout.
- Fix fuzz test pipeline by adding a proper timeout to a long-lasting test
1 parent 0b03e4a commit e3c485a

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

fuzztests/FuzzedDataProvider.fuzz.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ describe("FuzzedDataProvider", () => {
2323
// FuzzedDataProvider as possible, before invoking a terminating one
2424
// like consumeRemainingXY. Strange combinations of functions could produce a
2525
// one off error.
26-
it.fuzz("consumes the provided input", (data) => {
27-
const provider = new FuzzedDataProvider(data);
28-
const properties = Object.getOwnPropertyNames(
29-
Object.getPrototypeOf(provider)
30-
);
31-
const methodNames = properties
32-
.filter((p) => provider[p] instanceof Function)
33-
.filter((m) => provider[m].length === 0);
26+
it.fuzz(
27+
"consumes the provided input",
28+
(data) => {
29+
const provider = new FuzzedDataProvider(data);
30+
const properties = Object.getOwnPropertyNames(
31+
Object.getPrototypeOf(provider)
32+
);
33+
const methodNames = properties
34+
.filter((p) => provider[p] instanceof Function)
35+
.filter((m) => provider[m].length === 0);
3436

35-
let usedMethods = "";
36-
while (provider.remainingBytes > 0) {
37-
const methodName = provider.pickValue(methodNames);
38-
provider[methodName].call(provider);
39-
usedMethods += methodName;
40-
}
41-
jazzer.exploreState(hash(usedMethods), 31);
42-
});
37+
let usedMethods = "";
38+
while (provider.remainingBytes > 0) {
39+
const methodName = provider.pickValue(methodNames);
40+
provider[methodName].call(provider);
41+
usedMethods += methodName;
42+
}
43+
jazzer.exploreState(hash(usedMethods), 31);
44+
},
45+
40000
46+
);
4347
});
4448

4549
const hash = (str) => {

packages/jest-runner/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const defaultOptions: Options = {
2727
fuzzerOptions: [],
2828
sync: false,
2929
expectedErrors: [],
30-
timeout: 5000, // default Jest timeout
30+
timeout: undefined,
3131
coverage: false,
3232
coverageDirectory: "coverage",
3333
coverageReporters: ["json", "text", "lcov", "clover"], // default Jest reporters

packages/jest-runner/fuzz.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ export class FuzzerStartError extends FuzzerError {}
4242
// Use Jests global object definition.
4343
const g = globalThis as unknown as Global.Global;
4444

45-
export type FuzzTest = (name: Global.TestNameLike, fn: FuzzTarget) => void;
45+
export type FuzzTest = (
46+
name: Global.TestNameLike,
47+
fn: FuzzTarget,
48+
timeout?: number
49+
) => void;
4650

4751
export const skip: FuzzTest = (name) => {
4852
g.test.skip(toTestName(name), () => {
4953
return;
5054
});
5155
};
5256

53-
export const fuzz: FuzzTest = (name, fn) => {
57+
export const fuzz: FuzzTest = (name, fn, timeout = 5000) => {
5458
const testName = toTestName(name);
5559

5660
// Request the current test file path from the worker to create appropriate
@@ -64,15 +68,10 @@ export const fuzz: FuzzTest = (name, fn) => {
6468
// points to the element containing the fuzz function.
6569
const testStatePath = currentTestStatePath(testName);
6670

67-
// The timeout setting is extracted from the test state and defaults to 5s.
68-
// Setting timeouts on this level is necessary, as they apply to the
69-
// individual inputs and not the whole run.
70-
const timeout = currentTimeout();
71-
7271
const corpus = new Corpus(testFile, testStatePath);
7372

7473
const fuzzingConfig = loadConfig();
75-
fuzzingConfig.timeout = timeout;
74+
if (fuzzingConfig.timeout) timeout = fuzzingConfig.timeout;
7675

7776
if (fuzzingConfig.dryRun) {
7877
runInRegressionMode(name, fn, corpus, timeout);
@@ -231,7 +230,3 @@ const currentTestStatePath = (testName: string): string[] => {
231230
}
232231
return elements;
233232
};
234-
235-
const currentTimeout = (): number => {
236-
return circus.getState().testTimeout || 5000;
237-
};

0 commit comments

Comments
 (0)