Skip to content

Commit 41e1670

Browse files
committed
refactor: wip e2e test libs
1 parent 58e56e6 commit 41e1670

25 files changed

+332
-89
lines changed

e2e/plugin-js-benchmark-e2e/mocks/fixtures/default-setup/_package.json

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,97 @@
1-
import { cp } from 'node:fs/promises';
1+
import {cp} from 'node:fs/promises';
22
import path from 'node:path';
3-
import { afterAll, beforeAll, expect } from 'vitest';
4-
import { type Report, reportSchema } from '@code-pushup/models';
5-
import { nxTargetProject } from '@code-pushup/test-nx-utils';
3+
import {afterAll, beforeAll, expect} from 'vitest';
4+
import {type Report, reportSchema} from '@code-pushup/models';
5+
import {nxTargetProject} from '@code-pushup/test-nx-utils';
66
import {
7-
E2E_ENVIRONMENTS_DIR,
8-
TEST_OUTPUT_DIR,
9-
omitVariableReportData,
10-
restoreNxIgnoredFiles,
11-
teardownTestFolder,
7+
E2E_ENVIRONMENTS_DIR,
8+
TEST_OUTPUT_DIR,
9+
omitVariableReportData,
10+
restoreNxIgnoredFiles,
11+
teardownTestFolder,
1212
} from '@code-pushup/test-utils';
13-
import { executeProcess, readJsonFile } from '@code-pushup/utils';
13+
import {executeProcess, readJsonFile} from '@code-pushup/utils';
1414

15+
// skip tests until js-benchmark-plugin is officially released
1516
describe('PLUGIN collect report with js-benchmark-plugin NPM package', () => {
16-
const testFileDir = path.join(
17-
E2E_ENVIRONMENTS_DIR,
18-
nxTargetProject(),
19-
TEST_OUTPUT_DIR,
20-
'collect',
21-
);
22-
const defaultSetupDir = path.join(testFileDir, 'default-setup');
23-
24-
const fixturesDir = path.join('e2e', nxTargetProject(), 'mocks/fixtures');
25-
26-
beforeAll(async () => {
27-
await cp(fixturesDir, testFileDir, { recursive: true });
28-
await restoreNxIgnoredFiles(testFileDir);
29-
});
30-
31-
afterAll(async () => {
32-
await teardownTestFolder(testFileDir);
33-
});
34-
35-
it('should run plugin over CLI and creates report.json', async () => {
36-
const { code, stdout } = await executeProcess({
37-
command: 'npx',
38-
// verbose exposes audits with perfect scores that are hidden in the default stdout
39-
args: ['@code-pushup/cli', 'collect', '--verbose'],
40-
cwd: defaultSetupDir,
17+
const envDir = path.join(
18+
E2E_ENVIRONMENTS_DIR,
19+
nxTargetProject()
20+
);
21+
const testFileDir = path.join(
22+
envDir,
23+
TEST_OUTPUT_DIR,
24+
'collect',
25+
);
26+
const defaultSetupDir = path.join(testFileDir);
27+
28+
const fixturesDir = path.join('e2e', nxTargetProject(), 'mocks/fixtures/default-setup');
29+
30+
beforeAll(async () => {
31+
await cp(fixturesDir, envDir, {recursive: true});
32+
await restoreNxIgnoredFiles(envDir);
4133
});
4234

43-
expect(code).toBe(0);
44-
expect(stdout).toContain('JS Benchmark audits');
35+
afterAll(async () => {
36+
// await teardownTestFolder(testFileDir);
37+
});
4538

46-
const report = await readJsonFile(
47-
path.join(defaultSetupDir, '.code-pushup', 'report.json'),
48-
);
49-
expect(() => reportSchema.parse(report)).not.toThrowError();
50-
expect(
51-
omitVariableReportData(report as Report, { omitAuditData: true }),
52-
).toMatchSnapshot();
53-
});
39+
it.skip('should run plugin over CLI and creates report.json', async () => {
40+
const {code, stdout} = await executeProcess({
41+
command: 'npx',
42+
// verbose exposes audits with perfect scores that are hidden in the default stdout
43+
args: ['@code-pushup/cli', 'collect', '--verbose'],
44+
cwd: envDir,
45+
});
46+
47+
expect(code).toBe(0);
48+
expect(stdout).toContain('JS Benchmark audits');
49+
50+
const report = await readJsonFile(
51+
path.join(envDir, '.code-pushup', 'report.json'),
52+
);
53+
expect(() => reportSchema.parse(report)).not.toThrowError();
54+
expect(
55+
omitVariableReportData(report as Report, {omitAuditData: true}),
56+
).toMatchSnapshot();
57+
});
58+
59+
it('should be able to import runner entry point', async () => {
60+
// Test that the runner entry point can be imported from the installed package
61+
const runnerModule = await import(
62+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/lib/runner/index.js')
63+
);
64+
65+
expect(runnerModule.createRunnerFunction).toBeDefined();
66+
expect(typeof runnerModule.createRunnerFunction).toBe('function');
67+
expect(runnerModule.toAuditSlug).toBeDefined();
68+
expect(typeof runnerModule.toAuditSlug).toBe('function');
69+
70+
// Test toAuditSlug function
71+
const slug = runnerModule.toAuditSlug('test-suite');
72+
expect(slug).toBe('js-benchmarking-test-suite');
73+
});
74+
75+
it('should be able to import suite runner entry points', async () => {
76+
// Test tinybench suite runner
77+
const tinybenchModule = await import(
78+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/tinybench.suite-runner.js')
79+
);
80+
expect(tinybenchModule.tinybenchRunner).toBeDefined();
81+
expect(typeof tinybenchModule.tinybenchRunner.run).toBe('function');
82+
83+
// Test benchmark suite runner
84+
const benchmarkModule = await import(
85+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benchmark.suite-runner.js')
86+
);
87+
expect(benchmarkModule.benchmarkRunner).toBeDefined();
88+
expect(typeof benchmarkModule.benchmarkRunner.run).toBe('function');
89+
90+
// Test benny suite runner
91+
const bennyModule = await import(
92+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benny.suite-runner.js')
93+
);
94+
expect(bennyModule.bennyRunner).toBeDefined();
95+
expect(typeof bennyModule.bennyRunner.run).toBe('function');
96+
});
5497
});
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import {cp} from 'node:fs/promises';
2+
import path from 'node:path';
3+
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
4+
import {nxTargetProject} from '@code-pushup/test-nx-utils';
5+
import {
6+
E2E_ENVIRONMENTS_DIR,
7+
TEST_OUTPUT_DIR,
8+
restoreNxIgnoredFiles,
9+
teardownTestFolder,
10+
} from '@code-pushup/test-utils';
11+
12+
describe('suite-runners', () => {
13+
const envDir = path.join(E2E_ENVIRONMENTS_DIR, nxTargetProject());
14+
const testFileDir = path.join(envDir, TEST_OUTPUT_DIR, 'suite-runners');
15+
const fixturesDir = path.join('e2e', nxTargetProject(), 'mocks/fixtures/default-setup');
16+
17+
const cases = [
18+
['fast-operation', () => 1 + 1],
19+
['slow-operation', () => {
20+
let total = 0;
21+
for (let i = 0; i < 50000; i++) {
22+
total += Math.sqrt(Math.log(i + 1) * Math.sin(i));
23+
}
24+
return total;
25+
}],
26+
]
27+
28+
beforeAll(async () => {
29+
await cp(fixturesDir, testFileDir, {recursive: true});
30+
await restoreNxIgnoredFiles(testFileDir);
31+
});
32+
33+
afterAll(async () => {
34+
await teardownTestFolder(testFileDir);
35+
});
36+
37+
it('should be able to import and use tinybench suite runner', async () => {
38+
const {tinybenchRunner, benchToBenchmarkResult} = await import(
39+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/tinybench.suite-runner.js')
40+
);
41+
42+
expect(tinybenchRunner).toBeDefined();
43+
expect(typeof tinybenchRunner.run).toBe('function');
44+
expect(benchToBenchmarkResult).toBeDefined();
45+
expect(typeof benchToBenchmarkResult).toBe('function');
46+
});
47+
48+
it('should run tinybench suite with simple test cases', async () => {
49+
const {tinybenchRunner} = await import(
50+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/tinybench.suite-runner.js')
51+
);
52+
53+
const suiteConfig = {
54+
suiteName: 'tinybench-simple-test',
55+
targetImplementation: 'fast-operation',
56+
cases,
57+
time: 1000
58+
};
59+
60+
await expect(tinybenchRunner.run(suiteConfig, {
61+
outputDir: testFileDir,
62+
outputFileName: 'tinybench-simple-test',
63+
})).resolves.toStrictEqual(expect.arrayContaining([
64+
expect.objectContaining({
65+
suiteName: 'tinybench-simple-test',
66+
name: 'fast-operation',
67+
hz: expect.any(Number),
68+
rme: expect.any(Number),
69+
samples: expect.any(Number),
70+
isTarget: true,
71+
isFastest: true,
72+
}),
73+
expect.objectContaining({
74+
suiteName: 'tinybench-simple-test',
75+
name: 'slow-operation',
76+
hz: expect.any(Number),
77+
rme: expect.any(Number),
78+
samples: expect.any(Number),
79+
isTarget: false,
80+
isFastest: false,
81+
})
82+
]));
83+
});
84+
85+
it('should be able to import and use benchmark suite runner', async () => {
86+
const {benchmarkRunner, benchToBenchmarkResult} = await import(
87+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benchmark.suite-runner.js')
88+
);
89+
90+
expect(benchmarkRunner).toBeDefined();
91+
expect(typeof benchmarkRunner.run).toBe('function');
92+
expect(benchToBenchmarkResult).toBeDefined();
93+
expect(typeof benchToBenchmarkResult).toBe('function');
94+
});
95+
96+
it('should run benchmark suite with simple test cases', async () => {
97+
const {benchmarkRunner} = await import(
98+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benchmark.suite-runner.js')
99+
);
100+
101+
const suiteConfig = {
102+
suiteName: 'benchmark-simple-test',
103+
targetImplementation: 'fast-operation',
104+
cases
105+
};
106+
107+
await expect(benchmarkRunner.run(suiteConfig, {
108+
outputDir: testFileDir,
109+
outputFileName: 'benchmark-simple-test',
110+
})).resolves.toStrictEqual(expect.arrayContaining([
111+
expect.objectContaining({
112+
suiteName: 'benchmark-simple-test',
113+
name: 'fast-operation',
114+
hz: expect.any(Number),
115+
rme: expect.any(Number),
116+
samples: expect.any(Number),
117+
isTarget: true,
118+
isFastest: true,
119+
}),
120+
expect.objectContaining({
121+
suiteName: 'benchmark-simple-test',
122+
name: 'slow-operation',
123+
hz: expect.any(Number),
124+
rme: expect.any(Number),
125+
samples: expect.any(Number),
126+
isTarget: false,
127+
isFastest: false,
128+
})
129+
]));
130+
});
131+
132+
it('should be able to import and use benny suite runner', async () => {
133+
const {bennyRunner, benchToBenchmarkResult} = await import(
134+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benny.suite-runner.js')
135+
);
136+
137+
expect(bennyRunner).toBeDefined();
138+
expect(typeof bennyRunner.run).toBe('function');
139+
expect(benchToBenchmarkResult).toBeDefined();
140+
expect(typeof benchToBenchmarkResult).toBe('function');
141+
});
142+
143+
it('should run benny suite with simple test cases', async () => {
144+
const {bennyRunner} = await import(
145+
path.resolve(envDir, 'node_modules/@code-pushup/js-benchmark-plugin/src/plugins/benny.suite-runner.js')
146+
);
147+
148+
const suiteConfig = {
149+
suiteName: 'benny-simple-test',
150+
targetImplementation: 'fast-operation',
151+
cases
152+
};
153+
154+
await expect(bennyRunner.run(suiteConfig, {
155+
outputDir: testFileDir,
156+
outputFileName: 'benny-simple-test',
157+
})).resolves.toStrictEqual(expect.arrayContaining([
158+
expect.objectContaining({
159+
suiteName: 'benny-simple-test',
160+
name: 'fast-operation',
161+
hz: expect.any(Number),
162+
rme: expect.any(Number),
163+
samples: expect.any(Number),
164+
isTarget: true,
165+
isFastest: true,
166+
}),
167+
expect.objectContaining({
168+
suiteName: 'benny-simple-test',
169+
name: 'slow-operation',
170+
hz: expect.any(Number),
171+
rme: expect.any(Number),
172+
samples: expect.any(Number),
173+
isTarget: false,
174+
isFastest: false,
175+
})
176+
]));
177+
});
178+
179+
})

packages/plugin-js-benchmak/package.json

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010
"types": "./src/index.d.ts",
1111
"import": "./src/index.js",
1212
"default": "./src/index.js"
13+
},
14+
"./src/lib/runner/index.js": {
15+
"types": "./src/lib/runner/index.d.ts",
16+
"import": "./src/lib/runner/index.js",
17+
"default": "./src/lib/runner/index.js"
18+
},
19+
"./src/plugins/tinybench.suite-runner.js": {
20+
"types": "./src/plugins/tinybench.suite-runner.d.ts",
21+
"import": "./src/plugins/tinybench.suite-runner.js",
22+
"default": "./src/plugins/tinybench.suite-runner.js"
23+
},
24+
"./src/plugins/benchmark.suite-runner.js": {
25+
"types": "./src/plugins/benchmark.suite-runner.d.ts",
26+
"import": "./src/plugins/benchmark.suite-runner.js",
27+
"default": "./src/plugins/benchmark.suite-runner.js"
28+
},
29+
"./src/plugins/benny.suite-runner.js": {
30+
"types": "./src/plugins/benny.suite-runner.d.ts",
31+
"import": "./src/plugins/benny.suite-runner.js",
32+
"default": "./src/plugins/benny.suite-runner.js"
1333
}
1434
},
1535
"dependencies": {
@@ -22,6 +42,21 @@
2242
"vitest": "^1.3.1",
2343
"zod": "^3.24.1"
2444
},
25-
"peerDependencies": {},
45+
"peerDependencies": {
46+
"tinybench": "^2.0.0",
47+
"benchmark": "^2.1.0",
48+
"benny": "^3.7.0"
49+
},
50+
"peerDependenciesMeta": {
51+
"tinybench": {
52+
"optional": true
53+
},
54+
"benchmark": {
55+
"optional": true
56+
},
57+
"benny": {
58+
"optional": true
59+
}
60+
},
2661
"devDependencies": {}
2762
}

packages/plugin-js-benchmak/project.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"targets": {
77
"build": {
88
"options": {
9-
"additionalEntryPoints": [
10-
"packages/plugin-js-benchmark/src/lib/reporter.ts"
11-
]
9+
"additionalEntryPoints": ["packages/plugin-js-benchmak/src/lib/runner/index.ts"]
1210
}
1311
},
1412
"lint": {},

0 commit comments

Comments
 (0)