Skip to content

Commit b4be336

Browse files
authored
test: cover env, redirect and reload gaps (#650)
1 parent c4b7e7e commit b4be336

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎__tests__/instrumentation.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { register } from '../instrumentation';
2+
3+
const validEnv = {
4+
DATABASE_URL: 'postgresql://test-user:test-pass@localhost:5432/test',
5+
NEXTAUTH_URL: 'http://localhost:3001',
6+
NEXTAUTH_SECRET: 'example-not-for-production'
7+
};
8+
9+
describe('register', () => {
10+
const originalEnv = process.env;
11+
12+
beforeEach(() => {
13+
process.env = { ...validEnv, NODE_ENV: 'test', NEXT_RUNTIME: 'nodejs' };
14+
vi.spyOn(console, 'error').mockImplementation(() => {});
15+
vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
16+
});
17+
18+
afterEach(() => {
19+
process.env = originalEnv;
20+
vi.restoreAllMocks();
21+
});
22+
23+
it('exits with code 1 when the environment is invalid', async () => {
24+
delete process.env.DATABASE_URL;
25+
26+
await register();
27+
28+
expect(process.exit).toHaveBeenCalledWith(1);
29+
});
30+
31+
it('starts when the environment is valid', async () => {
32+
await register();
33+
34+
expect(process.exit).not.toHaveBeenCalled();
35+
});
36+
37+
it('skips the check outside the Node.js runtime', async () => {
38+
process.env.NEXT_RUNTIME = 'edge';
39+
delete process.env.DATABASE_URL;
40+
41+
await register();
42+
43+
expect(process.exit).not.toHaveBeenCalled();
44+
});
45+
});

‎__tests__/utils/env.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ describe('findEnvProblems', () => {
3939
}
4040
);
4141

42+
it('treats an unset NODE_ENV as development', () => {
43+
const env: NodeJS.ProcessEnv = { ...developmentEnv };
44+
Reflect.deleteProperty(env, 'NODE_ENV');
45+
expect(findEnvProblems(env)).toEqual([]);
46+
});
47+
4248
it('treats an empty string as not set', () => {
4349
const env: NodeJS.ProcessEnv = { ...developmentEnv, DATABASE_URL: '' };
4450
expect(findEnvProblems(env)).toEqual(['DATABASE_URL is not set']);

‎__tests__/utils/safeCallbackPath.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ describe('safeCallbackPath', () => {
1717
['javascript:alert(1)', '/'],
1818
['/.//evil.example', '/'],
1919
['/..//evil.example', '/'],
20-
['http://localhost:3001//evil.example', '/']
20+
['http://localhost:3001//evil.example', '/'],
21+
['/%2F%2Fevil.example', '/%2F%2Fevil.example'],
22+
['%2F%2Fevil.example', '/%2F%2Fevil.example'],
23+
['/%5Cevil.example', '/%5Cevil.example'],
24+
['/%2e%2e/%2F%2Fevil.example', '/%2F%2Fevil.example'],
25+
['http%3A%2F%2Fevil.example', '/http%3A%2F%2Fevil.example']
2126
])('maps %j to %j', (value, expected) => {
2227
expect(safeCallbackPath(value, base)).toBe(expected);
2328
});

‎e2e/smoke.spec.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ test('teacher creates a class', async ({ page }) => {
5757

5858
await expect(dialog).toBeHidden();
5959
await expect(page.getByText(className, { exact: true })).toBeVisible();
60+
61+
await page.reload();
62+
await expect(page.getByText(className, { exact: true })).toBeVisible();
6063
});
6164

6265
test('teacher opens the class dashboard', async ({ page }) => {

0 commit comments

Comments
 (0)