Skip to content

Commit 4fb2ca7

Browse files
author
Szymon.Poltorak
committed
refactor: unify e2e test setup
1 parent ffbe58d commit 4fb2ca7

File tree

16 files changed

+345
-294
lines changed

16 files changed

+345
-294
lines changed

e2e/ci-e2e/mocks/setup.ts

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

e2e/ci-e2e/tests/basic.e2e.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import {
88
type RunResult,
99
runInCI,
1010
} from '@code-pushup/ci';
11-
import { TEST_SNAPSHOTS_DIR } from '@code-pushup/test-utils';
11+
import {
12+
TEST_SNAPSHOTS_DIR,
13+
type TestEnvironmentWithGit,
14+
setupTestEnvironment,
15+
} from '@code-pushup/test-utils';
1216
import { MOCK_API, MOCK_COMMENT } from '../mocks/api.js';
13-
import { type TestRepo, setupTestRepo } from '../mocks/setup.js';
1417

1518
describe('CI - standalone mode', () => {
16-
let repo: TestRepo;
19+
let repo: TestEnvironmentWithGit;
1720
let git: SimpleGit;
1821
let options: Options;
1922

2023
beforeEach(async () => {
21-
repo = await setupTestRepo('basic');
24+
repo = await setupTestEnvironment(['..', 'mocks', 'fixtures', 'basic'], {
25+
callerUrl: import.meta.url,
26+
git: true,
27+
});
2228
git = repo.git;
2329
options = {
2430
directory: repo.baseDir,

e2e/ci-e2e/tests/npm-workspaces.e2e.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ import {
99
type RunResult,
1010
runInCI,
1111
} from '@code-pushup/ci';
12-
import { TEST_SNAPSHOTS_DIR } from '@code-pushup/test-utils';
12+
import {
13+
TEST_SNAPSHOTS_DIR,
14+
type TestEnvironmentWithGit,
15+
setupTestEnvironment,
16+
} from '@code-pushup/test-utils';
1317
import { readJsonFile } from '@code-pushup/utils';
1418
import { MOCK_API, MOCK_COMMENT } from '../mocks/api.js';
15-
import { type TestRepo, setupTestRepo } from '../mocks/setup.js';
1619

1720
describe('CI - monorepo mode (npm workspaces)', () => {
18-
let repo: TestRepo;
21+
let repo: TestEnvironmentWithGit;
1922
let git: SimpleGit;
2023
let options: Options;
2124

2225
beforeEach(async () => {
23-
repo = await setupTestRepo('npm-workspaces');
26+
repo = await setupTestEnvironment(
27+
['..', 'mocks', 'fixtures', 'npm-workspaces'],
28+
{
29+
callerUrl: import.meta.url,
30+
git: true,
31+
},
32+
);
2433
git = repo.git;
2534
options = {
2635
monorepo: true,

e2e/ci-e2e/tests/nx-monorepo.e2e.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ import {
99
type RunResult,
1010
runInCI,
1111
} from '@code-pushup/ci';
12-
import { TEST_SNAPSHOTS_DIR } from '@code-pushup/test-utils';
12+
import {
13+
TEST_SNAPSHOTS_DIR,
14+
type TestEnvironmentWithGit,
15+
setupTestEnvironment,
16+
} from '@code-pushup/test-utils';
1317
import { readJsonFile } from '@code-pushup/utils';
1418
import { MOCK_API, MOCK_COMMENT } from '../mocks/api.js';
15-
import { type TestRepo, setupTestRepo } from '../mocks/setup.js';
1619

1720
describe('CI - monorepo mode (Nx)', () => {
18-
let repo: TestRepo;
21+
let repo: TestEnvironmentWithGit;
1922
let git: SimpleGit;
2023
let options: Options;
2124

2225
beforeEach(async () => {
23-
repo = await setupTestRepo('nx-monorepo');
26+
repo = await setupTestEnvironment(
27+
['..', 'mocks', 'fixtures', 'nx-monorepo'],
28+
{
29+
callerUrl: import.meta.url,
30+
git: true,
31+
},
32+
);
2433
git = repo.git;
2534
options = {
2635
monorepo: true,

e2e/cli-e2e/tests/collect.e2e.test.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { cp } from 'node:fs/promises';
21
import path from 'node:path';
32
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
4-
import { nxTargetProject } from '@code-pushup/test-nx-utils';
53
import {
6-
E2E_ENVIRONMENTS_DIR,
7-
TEST_OUTPUT_DIR,
8-
restoreNxIgnoredFiles,
4+
type TestEnvironment,
5+
setupTestEnvironment,
96
teardownTestFolder,
107
} from '@code-pushup/test-utils';
118
import {
@@ -19,29 +16,24 @@ import { dummyPluginSlug } from '../mocks/fixtures/dummy-setup/dummy.plugin';
1916
describe('CLI collect', () => {
2017
const dummyPluginTitle = 'Dummy Plugin';
2118
const dummyAuditTitle = 'Dummy Audit';
22-
const fixtureDummyDir = path.join(
23-
'e2e',
24-
nxTargetProject(),
25-
'mocks',
26-
'fixtures',
27-
'dummy-setup',
28-
);
29-
const testFileDir = path.join(
30-
E2E_ENVIRONMENTS_DIR,
31-
nxTargetProject(),
32-
TEST_OUTPUT_DIR,
33-
'collect',
34-
);
35-
const dummyDir = path.join(testFileDir, 'dummy-setup');
36-
const dummyOutputDir = path.join(dummyDir, '.code-pushup');
19+
20+
let testEnv: TestEnvironment;
21+
let dummyDir: string;
22+
let dummyOutputDir: string;
3723

3824
beforeAll(async () => {
39-
await cp(fixtureDummyDir, dummyDir, { recursive: true });
40-
await restoreNxIgnoredFiles(dummyDir);
25+
testEnv = await setupTestEnvironment(
26+
['..', 'mocks', 'fixtures', 'dummy-setup'],
27+
{
28+
callerUrl: import.meta.url,
29+
},
30+
);
31+
dummyDir = testEnv.baseDir;
32+
dummyOutputDir = path.join(dummyDir, '.code-pushup');
4133
});
4234

4335
afterAll(async () => {
44-
await teardownTestFolder(dummyDir);
36+
await testEnv.cleanup();
4537
});
4638

4739
afterEach(async () => {

e2e/cli-e2e/tests/compare.e2e.test.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
import { cp } from 'node:fs/promises';
21
import path from 'node:path';
32
import { beforeAll } from 'vitest';
43
import type { ReportsDiff } from '@code-pushup/models';
5-
import { nxTargetProject } from '@code-pushup/test-nx-utils';
64
import {
7-
E2E_ENVIRONMENTS_DIR,
8-
TEST_OUTPUT_DIR,
9-
restoreNxIgnoredFiles,
10-
teardownTestFolder,
5+
type TestEnvironment,
6+
setupTestEnvironment,
117
} from '@code-pushup/test-utils';
128
import { executeProcess, readJsonFile, readTextFile } from '@code-pushup/utils';
139

1410
describe('CLI compare', () => {
15-
const fixtureDummyDir = path.join(
16-
'e2e',
17-
nxTargetProject(),
18-
'mocks',
19-
'fixtures',
20-
'existing-reports',
21-
);
22-
23-
const testFileDir = path.join(
24-
E2E_ENVIRONMENTS_DIR,
25-
nxTargetProject(),
26-
TEST_OUTPUT_DIR,
27-
'compare',
28-
);
29-
const existingDir = path.join(testFileDir, 'existing-reports');
30-
const existingOutputDir = path.join(existingDir, '.code-pushup');
11+
let testEnv: TestEnvironment;
12+
let existingDir: string;
13+
let existingOutputDir: string;
3114

3215
beforeAll(async () => {
33-
await cp(fixtureDummyDir, existingDir, { recursive: true });
34-
await restoreNxIgnoredFiles(existingDir);
16+
testEnv = await setupTestEnvironment(
17+
['..', 'mocks', 'fixtures', 'existing-reports'],
18+
{
19+
callerUrl: import.meta.url,
20+
},
21+
);
22+
existingDir = testEnv.baseDir;
23+
existingOutputDir = path.join(existingDir, '.code-pushup');
3524
});
3625

3726
afterAll(async () => {
38-
await teardownTestFolder(existingDir);
27+
await testEnv.cleanup();
3928
});
4029

4130
it('should compare report.json files and create report-diff.json and report-diff.md', async () => {

e2e/plugin-coverage-e2e/tests/collect.e2e.test.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
1-
import { cp } from 'node:fs/promises';
21
import path from 'node:path';
3-
import { simpleGit } from 'simple-git';
42
import { afterAll, afterEach, beforeAll } from 'vitest';
53
import { type Report, reportSchema } from '@code-pushup/models';
64
import { omitVariableReportData } from '@code-pushup/test-fixtures';
7-
import { nxTargetProject } from '@code-pushup/test-nx-utils';
85
import {
9-
E2E_ENVIRONMENTS_DIR,
10-
TEST_OUTPUT_DIR,
11-
initGitRepo,
12-
restoreNxIgnoredFiles,
6+
type TestEnvironmentWithGit,
7+
setupTestEnvironment,
138
teardownTestFolder,
149
} from '@code-pushup/test-utils';
1510
import { executeProcess, readJsonFile } from '@code-pushup/utils';
1611

1712
describe('PLUGIN collect report with coverage-plugin NPM package', () => {
18-
const envRoot = path.join(E2E_ENVIRONMENTS_DIR, nxTargetProject());
19-
const testFileDir = path.join(envRoot, TEST_OUTPUT_DIR, 'collect');
20-
21-
const basicDir = path.join(testFileDir, 'basic-setup');
22-
const existingDir = path.join(testFileDir, 'existing-report');
23-
24-
const fixtureDir = path.join('e2e', nxTargetProject(), 'mocks', 'fixtures');
13+
let basicEnv: TestEnvironmentWithGit;
14+
let existingEnv: TestEnvironmentWithGit;
2515

2616
beforeAll(async () => {
27-
await cp(fixtureDir, testFileDir, { recursive: true });
28-
await restoreNxIgnoredFiles(testFileDir);
29-
await initGitRepo(simpleGit, { baseDir: basicDir });
30-
await initGitRepo(simpleGit, { baseDir: existingDir });
17+
basicEnv = await setupTestEnvironment(
18+
['..', 'mocks', 'fixtures', 'basic-setup'],
19+
{
20+
callerUrl: import.meta.url,
21+
git: true,
22+
testId: 'plugin-coverage-basic',
23+
},
24+
);
25+
existingEnv = await setupTestEnvironment(
26+
['..', 'mocks', 'fixtures', 'existing-report'],
27+
{
28+
callerUrl: import.meta.url,
29+
git: true,
30+
testId: 'plugin-coverage-existing',
31+
},
32+
);
3133
});
3234

3335
afterAll(async () => {
34-
await teardownTestFolder(basicDir);
35-
await teardownTestFolder(existingDir);
36+
await basicEnv.cleanup();
37+
await existingEnv.cleanup();
3638
});
3739

3840
afterEach(async () => {
39-
await teardownTestFolder(path.join(basicDir, '.code-pushup'));
40-
await teardownTestFolder(path.join(existingDir, '.code-pushup'));
41+
await teardownTestFolder(path.join(basicEnv.baseDir, '.code-pushup'));
42+
await teardownTestFolder(path.join(existingEnv.baseDir, '.code-pushup'));
4143
});
4244

4345
it('should run Code coverage plugin which runs tests and creates report.json', async () => {
4446
const { code } = await executeProcess({
4547
command: 'npx',
4648
args: ['code-pushup', 'collect'],
47-
cwd: basicDir,
49+
cwd: basicEnv.baseDir,
4850
});
4951

5052
expect(code).toBe(0);
5153

5254
const report = await readJsonFile<Report>(
53-
path.join(basicDir, '.code-pushup', 'report.json'),
55+
path.join(basicEnv.baseDir, '.code-pushup', 'report.json'),
5456
);
5557

5658
expect(() => reportSchema.parse(report)).not.toThrow();
@@ -61,13 +63,13 @@ describe('PLUGIN collect report with coverage-plugin NPM package', () => {
6163
const { code } = await executeProcess({
6264
command: 'npx',
6365
args: ['@code-pushup/cli', 'collect'],
64-
cwd: existingDir,
66+
cwd: existingEnv.baseDir,
6567
});
6668

6769
expect(code).toBe(0);
6870

6971
const report = await readJsonFile<Report>(
70-
path.join(existingDir, '.code-pushup', 'report.json'),
72+
path.join(existingEnv.baseDir, '.code-pushup', 'report.json'),
7173
);
7274

7375
expect(() => reportSchema.parse(report)).not.toThrow();

0 commit comments

Comments
 (0)