Skip to content

Commit 38b7a6e

Browse files
author
Szymon.Poltorak
committed
refactor(testing): consolidate global setup logic and remove redundant files
1 parent 7300e94 commit 38b7a6e

File tree

9 files changed

+25
-80
lines changed

9 files changed

+25
-80
lines changed

e2e/ci-e2e/global-setup.ts

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

e2e/ci-e2e/tsconfig.test.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"vitest.e2e.config.ts",
99
"tests/**/*.e2e.test.ts",
1010
"tests/**/*.d.ts",
11-
"mocks/**/*.ts",
12-
"global-setup.ts"
11+
"mocks/**/*.ts"
1312
]
1413
}

e2e/ci-e2e/vitest.e2e.config.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
/// <reference types="vitest" />
2-
import type { UserConfig } from 'vite';
32
import { createE2ETestConfig } from '../../testing/test-setup-config/src/index.js';
43

5-
const baseConfig = createE2ETestConfig('ci-e2e', {
4+
export default createE2ETestConfig('ci-e2e', {
65
testTimeout: 60_000,
7-
disableCoverage: true,
86
});
9-
10-
export default {
11-
...baseConfig,
12-
test: {
13-
...(baseConfig as any).test,
14-
globalSetup: ['./global-setup.ts'],
15-
},
16-
} as UserConfig;

global-setup.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1-
export async function setup() {
1+
/* eslint-disable functional/immutable-data */
2+
3+
const originalCI = process.env['CI'];
4+
5+
export function setup() {
26
process.env.TZ = 'UTC';
7+
8+
// package is expected to run in CI environment
9+
process.env['CI'] = 'true';
10+
}
11+
12+
export function teardown() {
13+
if (originalCI === undefined) {
14+
delete process.env['CI'];
15+
} else {
16+
process.env['CI'] = originalCI;
17+
}
318
}

testing/test-setup-config/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default createE2ETestConfig('my-e2e');
3030
// With options:
3131
export default createE2ETestConfig('my-e2e', {
3232
testTimeout: 60_000,
33-
disableCoverage: true,
3433
});
3534
```
3635

testing/test-setup-config/src/lib/vitest-config-factory.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import path from 'node:path';
2-
import { type UserConfig as ViteUserConfig, defineConfig } from 'vite';
31
import type { CoverageOptions, InlineConfig } from 'vitest';
2+
import { type UserConfig as ViteUserConfig, defineConfig } from 'vitest/config';
43
import { getSetupFiles } from './vitest-setup-files.js';
54
import { tsconfigPathAliases } from './vitest-tsconfig-path-aliases.js';
65

76
export type TestKind = 'unit' | 'int' | 'e2e';
87

98
export type E2ETestOptions = {
109
testTimeout?: number;
11-
disableCoverage?: boolean;
1210
};
1311

1412
export type VitestConfig = ViteUserConfig & { test?: InlineConfig };
@@ -34,9 +32,8 @@ function getGlobalSetup(kind: TestKind): string[] | undefined {
3432
function buildCoverageConfig(
3533
projectKey: string,
3634
kind: TestKind,
37-
disableCoverage?: boolean,
3835
): CoverageOptions | undefined {
39-
if (disableCoverage || kind === 'e2e') {
36+
if (kind === 'e2e') {
4037
return undefined;
4138
}
4239

@@ -55,11 +52,7 @@ export function createVitestConfig(
5552
kind: TestKind,
5653
options?: E2ETestOptions,
5754
): ViteUserConfig {
58-
const coverage = buildCoverageConfig(
59-
projectKey,
60-
kind,
61-
options?.disableCoverage,
62-
);
55+
const coverage = buildCoverageConfig(projectKey, kind);
6356

6457
const config: VitestConfig = {
6558
cacheDir: `../../node_modules/.vite/${projectKey}`,

testing/test-setup-config/src/lib/vitest-config-factory.unit.test.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { defineConfig } from 'vite';
21
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { defineConfig } from 'vitest/config';
33
import type { E2ETestOptions, TestKind } from './vitest-config-factory.js';
44
import { createVitestConfig } from './vitest-config-factory.js';
55

6-
vi.mock('vite', async importOriginal => {
7-
const actual = await importOriginal<typeof import('vite')>();
6+
vi.mock('vitest/config', async importOriginal => {
7+
const actual = await importOriginal<typeof import('vitest/config')>();
88
return {
99
...actual,
1010
defineConfig: vi.fn(config => config),
@@ -215,17 +215,9 @@ describe('createVitestConfig', () => {
215215
expect((config as any).test.testTimeout).toBe(60_000);
216216
});
217217

218-
it('should support disableCoverage option', () => {
219-
const options: E2ETestOptions = { disableCoverage: true };
220-
const config = createVitestConfig('test-package', 'e2e', options);
221-
222-
expect((config as any).test.coverage).toBeUndefined();
223-
});
224-
225218
it('should support multiple options together', () => {
226219
const options: E2ETestOptions = {
227220
testTimeout: 30_000,
228-
disableCoverage: true,
229221
};
230222
const config = createVitestConfig('test-package', 'e2e', options);
231223

testing/test-setup-config/src/lib/vitest-setup-presets.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UserConfig as ViteUserConfig } from 'vite';
1+
import type { UserConfig as ViteUserConfig } from 'vitest/config';
22
import {
33
type E2ETestOptions,
44
createVitestConfig,
@@ -49,7 +49,6 @@ export function createIntTestConfig(projectKey: string): ViteUserConfig {
4949
* // With options
5050
* export default createE2ETestConfig('my-e2e', {
5151
* testTimeout: 60_000,
52-
* disableCoverage: true,
5352
* });
5453
*
5554
* // Override any config using spread operator

testing/test-setup-config/src/lib/vitest-setup-presets.unit.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ describe('vitest-setup-presets', () => {
8080
it('should pass options to createVitestConfig', () => {
8181
const options = {
8282
testTimeout: 60_000,
83-
disableCoverage: true,
8483
};
8584

8685
createE2ETestConfig(MOCK_PROJECT_KEY, options);
@@ -101,31 +100,6 @@ describe('vitest-setup-presets', () => {
101100
{ testTimeout: 30_000 },
102101
);
103102
});
104-
105-
it('should handle disableCoverage option', () => {
106-
createE2ETestConfig(MOCK_PROJECT_KEY, { disableCoverage: true });
107-
108-
expect(configFactory.createVitestConfig).toHaveBeenCalledWith(
109-
MOCK_PROJECT_KEY,
110-
'e2e',
111-
{ disableCoverage: true },
112-
);
113-
});
114-
115-
it('should handle multiple options', () => {
116-
const options = {
117-
testTimeout: 45_000,
118-
disableCoverage: false,
119-
};
120-
121-
createE2ETestConfig(MOCK_PROJECT_KEY, options);
122-
123-
expect(configFactory.createVitestConfig).toHaveBeenCalledWith(
124-
MOCK_PROJECT_KEY,
125-
'e2e',
126-
options,
127-
);
128-
});
129103
});
130104

131105
describe('function naming', () => {

0 commit comments

Comments
 (0)