Skip to content

Commit e05c2ee

Browse files
committed
bugfix: TypeScript support for Jest now works in standalone projects
1 parent 95bfb25 commit e05c2ee

File tree

9 files changed

+27
-36
lines changed

9 files changed

+27
-36
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ following example:
9898

9999
```typescript
100100
// file: "Target.fuzz.ts"
101+
import "@jazzer.js/jest-runner";
101102
import * as target from "./target";
102103

103104
describe("Target", () => {

docs/jest-integration.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ To introduce the `fuzz` function types globally, add the following import to
127127
`globals.d.ts`. This could also be done in the individual test files.
128128

129129
```typescript
130-
import "@jazzer.js/jest-runner/jest-extension";
130+
import "@jazzer.js/jest-runner";
131131
```
132132

133133
To provide accurate coverage reports for TypeScript fuzz tests, make sure to
@@ -224,12 +224,8 @@ describe("My describe", () => {
224224
After the setup mentioned previously, Jest fuzz tests can be written in
225225
TypeScript, just as one would expect.
226226
227-
**Note**: To satisfy TypeScript's type checker, add an import of
228-
`@jazzer.js/jest-runner/jest-extension` in `globals.d.ts` or in the individual
229-
test file.
230-
231227
```typescript
232-
import "@jazzer.js/jest-runner/jest-extension";
228+
import "@jazzer.js/jest-runner";
233229
import * as target from "./target";
234230

235231
describe("Target", () => {

examples/jest_typescript_integration/globals.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/jest_typescript_integration/integration.fuzz.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Import the fuzz testing extension definition to compile TS code,
18-
// or import it globally in globals.d.ts, like in this example.
19-
// import "@jazzer.js/jest-runner/jest-extension";
20-
17+
import "@jazzer.js/jest-runner";
2118
import * as target from "./target";
2219

2320
describe("Target", () => {

packages/jest-runner/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { loadConfig } from "./config";
18+
import { cleanupJestRunnerStack } from "./errorUtils";
19+
import { FuzzTest } from "./fuzz";
20+
import { JazzerWorker } from "./worker";
21+
import { registerGlobals, initFuzzing } from "@jazzer.js/core";
1722
import {
18-
TestRunnerOptions,
19-
Test,
20-
TestRunnerContext,
21-
TestWatcher,
2223
CallbackTestRunner,
24+
OnTestFailure,
2325
OnTestStart,
2426
OnTestSuccess,
25-
OnTestFailure,
27+
Test,
28+
TestRunnerContext,
29+
TestRunnerOptions,
30+
TestWatcher,
2631
} from "jest-runner";
27-
2832
import { Config } from "@jest/types";
29-
import { JazzerWorker } from "./worker";
30-
import { registerGlobals, initFuzzing } from "@jazzer.js/core";
31-
import { loadConfig } from "./config";
32-
import { cleanupJestRunnerStack } from "./errorUtils";
3333
import * as reports from "istanbul-reports";
3434

3535
class FuzzRunner extends CallbackTestRunner {
@@ -97,3 +97,14 @@ class CancelRun extends Error {
9797
}
9898

9999
export default FuzzRunner;
100+
101+
// Global definition of the Jest fuzz test extension function.
102+
// This is required to allow the Typescript compiler to recognize it.
103+
declare global {
104+
// eslint-disable-next-line @typescript-eslint/no-namespace
105+
namespace jest {
106+
interface It {
107+
fuzz: FuzzTest;
108+
}
109+
}
110+
}

packages/jest-runner/jest-extension.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/jest-runner/worker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { inspect } from "util";
2727
import { fuzz, FuzzerStartError, skip } from "./fuzz";
2828
import { cleanupJestRunnerStack, removeTopFramesFromError } from "./errorUtils";
2929
import { createScriptTransformer } from "@jest/transform";
30-
import "./jest-extension";
3130

3231
function isGeneratorFunction(obj?: unknown): boolean {
3332
return (

tests/code_coverage/sample_fuzz_test/expected_coverage/fuzz+lib+otherCodeCoverage-fuzz.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"statementMap": {
44
"0": {
55
"start": { "line": 17, "column": 0 },
6-
"end": { "line": 17, "column": 47 }
6+
"end": { "line": 17, "column": 32 }
77
},
88
"1": {
99
"start": { "line": 18, "column": 0 },

tests/code_coverage/sample_fuzz_test/otherCodeCoverage.fuzz.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import "@jazzer.js/jest-runner/jest-extension";
17+
import "@jazzer.js/jest-runner";
1818
import { fuzz } from "./fuzz.js";
1919

2020
describe("My describe", () => {

0 commit comments

Comments
 (0)