|
| 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 | +}) |
0 commit comments