Skip to content

Commit d8344f7

Browse files
authored
feat: add 'resetEnvironmentBetweenTestFiles' config property (#11)
This pull request adds a new configuration property that allows you to opt out of test file-based isolation and run multiple test files in a single JavaScript environment. This can speed up test execution at the cost of isolation, so if your tests modify the global context in any way, you should not enable it.
1 parent 12ccf5f commit d8344f7

5 files changed

Lines changed: 19 additions & 3 deletions

File tree

apps/playground/rn-harness.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const config = {
2020
],
2121
defaultRunner: 'android',
2222
bridgeTimeout: 120000,
23+
24+
resetEnvironmentBetweenTestFiles: true,
2325
unstable__skipAlreadyIncludedModules: false,
2426
};
2527

packages/cli/src/commands/test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const runTests = async (
103103
context: TestRunContext,
104104
options: TestFilterOptions = {}
105105
): Promise<void> => {
106-
const { bridge, environment, testFiles } = context;
106+
const { bridge, environment, testFiles, config } = context;
107107
assert(bridge != null, 'Bridge not initialized');
108108
assert(environment != null, 'Environment not initialized');
109109
assert(testFiles != null, 'Test files not initialized');
@@ -141,7 +141,11 @@ const runTests = async (
141141

142142
const result = await client.runTests(testFile, executionOptions);
143143
context.results = [...(context.results ?? []), ...result.suites];
144-
shouldRestart = true;
144+
145+
if (config.resetEnvironmentBetweenTestFiles) {
146+
shouldRestart = true;
147+
}
148+
145149
runSpinner.stop(`Test file ${testFile} completed`);
146150
}
147151

packages/config/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export const ConfigSchema = z
8181
.number()
8282
.min(1000, 'Bridge timeout must be at least 1 second')
8383
.default(60000),
84+
85+
resetEnvironmentBetweenTestFiles: z.boolean().optional().default(true),
8486
unstable__skipAlreadyIncludedModules: z.boolean().optional().default(false),
8587
})
8688
.refine(

packages/jest/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default class JestHarness implements CallbackTestRunnerInterface {
7373
tests,
7474
watcher,
7575
harness,
76+
harnessConfig,
7677
onStart,
7778
onResult,
7879
onFailure
@@ -87,6 +88,7 @@ export default class JestHarness implements CallbackTestRunnerInterface {
8788
tests: Array<Test>,
8889
watcher: TestWatcher,
8990
harness: Harness,
91+
harnessConfig: HarnessConfig,
9092
onStart: OnTestStart,
9193
onResult: OnTestSuccess,
9294
onFailure: OnTestFailure
@@ -103,7 +105,10 @@ export default class JestHarness implements CallbackTestRunnerInterface {
103105
throw new CancelRun();
104106
}
105107

106-
if (!isFirstTest) {
108+
if (
109+
harnessConfig.resetEnvironmentBetweenTestFiles &&
110+
!isFirstTest
111+
) {
107112
await new Promise((resolve) => {
108113
harness.bridge.once('ready', resolve);
109114
harness.environment.restart();

website/src/docs/getting-started/configuration.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ AppRegistry.registerComponent('MyApp', () => App);
8989

9090
// Optional: Bridge timeout in milliseconds (default: 60000)
9191
bridgeTimeout?: number;
92+
93+
// Optional: Reset environment between test files (default: true)
94+
resetEnvironmentBetweenTestFiles?: boolean;
9295
}
9396
```
9497

0 commit comments

Comments
 (0)