Skip to content

Commit 577ad6f

Browse files
authored
chore(test): Clean up e2e test utils (#1182)
* chore(test): Clean up e2e test utils * WizardTestEnv -> ProcessRunner * lint
1 parent 8680583 commit 577ad6f

16 files changed

+157
-250
lines changed

CLAUDE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ Use `abortIfCancelled()` wrapper for all Clack prompts to handle Ctrl+C graceful
181181

182182
- `test-applications/`: Complete framework apps for testing
183183
- `tests/`: Test files that run wizard against test apps
184-
- `utils/`: Test helpers including `WizardTestEnv` class
184+
- `utils/`: Test helpers including assertion functions for reusable checks (e.g. `checkIfBuilds`)
185185
- Requires `.env` file with Sentry credentials
186186
- Use `yarn test:e2e [framework]` to run specific framework tests
187+
- Use `clifty` to define wizard run and interactions
187188

188189
## Special Considerations
189190

e2e-tests/README.md

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ modifiers that can be used in (`*.test.ts`).
3232

3333
#### Helpers
3434

35-
- `startWizardInstance` - Starts a new instance of `WizardTestEnv`.
36-
37-
- `initGit` - Initializes a temporary git repository in the test project.
38-
- `cleanupGit` - Cleans up the temporary git repository in the test project.
39-
- `revertLocalChanges` - Reverts local changes (git tracked or untracked) in the
40-
test project.
35+
- `createIsolatedTestEnv` - creates a new isolated test env by copying the test app to a temporary directory.
36+
Also initializes git in the tmp dir
4137

4238
- `createFile` - Creates a file (optionally with content) in the test project.
4339
- `modifyFile` - Modifies a file in the test project.
@@ -61,12 +57,6 @@ modifiers that can be used in (`*.test.ts`).
6157
- `checkSentryProperties` - Checks if the Flutter `sentry.properties` file
6258
contains the auth token
6359

64-
#### `WizardTestEnv`
65-
66-
`WizardTestEnv` is a class that can be used to run the Sentry Wizard in a test
67-
environment. It provides methods to run the wizard with specific arguments and
68-
stdio.
69-
7060
## Running Tests Locally
7161

7262
First, you need to create a `.env` file set the environment variables from the
@@ -82,6 +72,65 @@ To run a specific test application
8272

8373
## Writing Tests
8474

85-
Each test file should contain a single test suite that tests the Sentry Wizard
86-
for a specific framework. The test suite should contain a `beforeAll` and
87-
`afterAll` function that starts and stops the test application respectively.
75+
Each test file should test the Sentry Wizard for a specific framework and project.
76+
77+
The test suite may contain multiple wizard runs but for consistency, each scenario must be
78+
isolated via `createIsolatedTestEnv`. You can most easily do this by using a `describe` block
79+
per wizard run.
80+
81+
For every `describe` block, isolate the test, run the wizard in `beforeAll`, `test` what you
82+
want to test and clean up the tmp dir in `afterAll`:
83+
84+
```ts
85+
describe('no sentry files present', () => {
86+
const {projectDir, cleanup} = createIsolatedTestEnv();
87+
88+
beforeAll(() => {
89+
await runWizard(projectDir);
90+
});
91+
92+
afterAll(() => {
93+
cleanup()
94+
})
95+
})
96+
97+
describe('with sentry files present', () => {
98+
const {projectDir, cleanup} = createIsolatedTestEnv();
99+
100+
beforeAll(() => {
101+
addSentryFiles(projectDir);
102+
await runWizard(projectDir);
103+
});
104+
105+
afterAll(() => {
106+
cleanup()
107+
})
108+
})
109+
```
110+
111+
### Running the wizard
112+
113+
To define how a wizard run should look like (i.e. which responses the "user" makes) on
114+
wizard prompots, use `clifty`. Clifty's `run` method starts a new process to run the wizard
115+
with the predefined interaction and returns the processe's exit code.
116+
You can use this to check for a successful wizard run.
117+
118+
```ts
119+
import { KEYS, withEnv } from 'clifty';
120+
121+
const wizardExitCode = await withEnv({
122+
cwd: projectDir,
123+
})
124+
.defineInteraction()
125+
.whenAsked('Do you want to enable Tracing')
126+
.respondWith(KEYS.ENTER)
127+
.expectOutput('Added Sentry code to sentry.client.ts')
128+
.run(getWizardCommand(Integrations.nextjs));
129+
130+
// ...
131+
132+
test('wizard ran successfully', () => {
133+
expect(wizardExitCode).toBe(0);
134+
})
135+
```
136+

e2e-tests/tests/angular-17.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function checkAngularProject(
171171
},
172172
) {
173173
test('package.json is updated correctly', () => {
174-
checkPackageJson(projectDir, integration);
174+
checkPackageJson(projectDir, '@sentry/angular');
175175

176176
const packageJsonFile = path.resolve(projectDir, 'package.json');
177177
checkFileContents(packageJsonFile, [

e2e-tests/tests/angular-19.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function checkAngularProject(
170170
},
171171
) {
172172
test('package.json is updated correctly', () => {
173-
checkPackageJson(projectDir, integration);
173+
checkPackageJson(projectDir, '@sentry/angular');
174174

175175
const packageJsonFile = path.resolve(projectDir, 'package.json');
176176
checkFileContents(packageJsonFile, [

e2e-tests/tests/cloudflare-worker.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('cloudflare-worker', () => {
1919
const { projectDir, cleanup } = createIsolatedTestEnv('cloudflare-test-app');
2020

2121
beforeAll(async () => {
22-
2322
// Capture the date before running the wizard (wizard runs in subprocess)
2423
expectedCompatibilityDate = new Date().toISOString().slice(0, 10);
2524

@@ -61,7 +60,7 @@ describe('cloudflare-worker', () => {
6160
});
6261

6362
it('adds the SDK dependency to package.json', () => {
64-
checkPackageJson(projectDir, integration);
63+
checkPackageJson(projectDir, '@sentry/cloudflare');
6564
});
6665

6766
it('builds correctly', async () => {

e2e-tests/tests/nextjs-14.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('NextJS-14', () => {
6565
});
6666

6767
test('package.json is updated correctly', () => {
68-
checkPackageJson(projectDir, integration);
68+
checkPackageJson(projectDir, '@sentry/nextjs');
6969
});
7070

7171
test('.env-sentry-build-plugin is created and contains the auth token', () => {

e2e-tests/tests/nextjs-15.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
checkIfRunsOnProdMode,
1515
checkPackageJson,
1616
getWizardCommand,
17-
initGit,
1817
} from '../utils';
1918
import { describe, beforeAll, afterAll, test, expect } from 'vitest';
2019

@@ -72,7 +71,7 @@ describe('NextJS-15', () => {
7271
});
7372

7473
test('package.json is updated correctly', () => {
75-
checkPackageJson(projectDir, integration);
74+
checkPackageJson(projectDir, '@sentry/nextjs');
7675
});
7776

7877
test('.env-sentry-build-plugin is created and contains the auth token', () => {
@@ -146,13 +145,12 @@ export const onRequestError = Sentry.captureRequestError;`,
146145

147146
describe('NextJS-15 Spotlight', () => {
148147
const integration = Integration.nextjs;
148+
let wizardExitCode: number;
149149

150150
const { projectDir, cleanup } = createIsolatedTestEnv('nextjs-15-test-app');
151151

152152
beforeAll(async () => {
153-
initGit(projectDir);
154-
155-
await withEnv({
153+
wizardExitCode = await withEnv({
156154
cwd: projectDir,
157155
})
158156
.defineInteraction()
@@ -189,8 +187,12 @@ describe('NextJS-15 Spotlight', () => {
189187
cleanup();
190188
});
191189

190+
test('exits with exit code 0', () => {
191+
expect(wizardExitCode).toBe(0);
192+
});
193+
192194
test('package.json is updated correctly', () => {
193-
checkPackageJson(projectDir, integration);
195+
checkPackageJson(projectDir, '@sentry/nextjs');
194196
});
195197

196198
test('.env-sentry-build-plugin should NOT exist in spotlight mode', () => {

e2e-tests/tests/nextjs-16.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
checkPackageJson,
99
createIsolatedTestEnv,
1010
getWizardCommand,
11-
initGit,
12-
revertLocalChanges,
1311
} from '../utils';
1412
import { describe, beforeAll, afterAll, test, expect } from 'vitest';
1513

@@ -23,9 +21,6 @@ describe('NextJS-16 with Prettier, Biome, and ESLint', () => {
2321
const { projectDir, cleanup } = createIsolatedTestEnv('nextjs-16-test-app');
2422

2523
beforeAll(async () => {
26-
initGit(projectDir);
27-
revertLocalChanges(projectDir);
28-
2924
wizardExitCode = await withEnv({
3025
cwd: projectDir,
3126
})
@@ -75,7 +70,7 @@ describe('NextJS-16 with Prettier, Biome, and ESLint', () => {
7570
});
7671

7772
test('package.json is updated correctly', () => {
78-
checkPackageJson(projectDir, integration);
73+
checkPackageJson(projectDir, '@sentry/nextjs');
7974
});
8075

8176
test('config files created', () => {

e2e-tests/tests/nuxt-3.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('Nuxt-3', () => {
7373
});
7474

7575
test('package.json is updated correctly', () => {
76-
checkPackageJson(projectDir, Integration.nuxt);
76+
checkPackageJson(projectDir, '@sentry/nuxt');
7777
});
7878

7979
test('.env-sentry-build-plugin is created and contains the auth token', () => {

e2e-tests/tests/nuxt-4.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('Nuxt-4', () => {
7373
});
7474

7575
test('package.json is updated correctly', () => {
76-
checkPackageJson(projectDir, Integration.nuxt);
76+
checkPackageJson(projectDir, '@sentry/nuxt');
7777
});
7878

7979
test('.env-sentry-build-plugin is created and contains the auth token', () => {

0 commit comments

Comments
 (0)