|
| 1 | +import { createProjectBuildProvider, serveDir, serveToolboxRunner } from '@ph.fritsche/toolbox' |
| 2 | +import { NodeTestConductor } from '@ph.fritsche/toolbox/dist/conductor/NodeTestConductor.js' |
| 3 | +import { ChromeTestConductor } from '@ph.fritsche/toolbox/dist/conductor/ChromeTestConductor.js' |
| 4 | +import { ConsoleReporter } from '@ph.fritsche/toolbox/dist/reporter/ConsoleReporter.js' |
| 5 | +import { ReporterServer } from '@ph.fritsche/toolbox/dist/reporter/ReporterServer.js' |
| 6 | +import { TestRunStack } from '@ph.fritsche/toolbox/dist/reporter/TestRunStack.js' |
| 7 | + |
| 8 | +import IstanbulLibCoverage from 'istanbul-lib-coverage' |
| 9 | +import IstanbulLibReport from 'istanbul-lib-report' |
| 10 | +import IstanbulLibSourceMaps from 'istanbul-lib-source-maps' |
| 11 | +import IstanbulReports from 'istanbul-reports' |
| 12 | + |
| 13 | +const tsConfigFile = './tests/tsconfig.json' |
| 14 | + |
| 15 | +const toolbox = await serveToolboxRunner() |
| 16 | +const env = await serveDir('testenv') |
| 17 | + |
| 18 | +const { buildProvider, fileProvider, fileServer, onBuildDone } = createProjectBuildProvider([ |
| 19 | + 'src', |
| 20 | + 'tests', |
| 21 | +], { |
| 22 | + tsConfigFile, |
| 23 | + globals: { |
| 24 | + '@testing-library/dom': 'DomTestingLibrary', |
| 25 | + '@testing-library/react': 'ReactTestingLibrary', |
| 26 | + 'react': 'React', |
| 27 | + 'react-dom': 'ReactDom', |
| 28 | + } |
| 29 | +}) |
| 30 | + |
| 31 | +for (const { builder } of buildProvider.builders) { |
| 32 | + builder.emitter.addListener('start', ({ type, buildId, inputFiles }) => console.log(builder.id, { type, buildId, inputFiles: inputFiles.size })) |
| 33 | + builder.emitter.addListener('complete', ({ type, buildId, inputFiles, outputFiles }) => console.log(builder.id, { type, buildId, inputFiles: inputFiles.size, outputFiles: outputFiles.size })) |
| 34 | + builder.emitter.addListener('error', ({ type, buildId, error }) => console.log(builder.id, { type, buildId, error })) |
| 35 | + builder.emitter.addListener('done', ({ type, buildId }) => console.log(builder.id, { type, buildId })) |
| 36 | +} |
| 37 | +buildProvider.getBuilder('dependencies').builder.emitter.addListener('start', ({inputFiles}) => console.log('dependencies', inputFiles.keys())) |
| 38 | + |
| 39 | +const filter = (f) => f.startsWith('tests') |
| 40 | + && /(?<!\.json)\.js$/.test(f) |
| 41 | + && !/\/_.+(?<!\.test)\.[jt]sx?$/.test(f) |
| 42 | + |
| 43 | +const reporterServer = new ReporterServer() |
| 44 | +await reporterServer.registerFileServer(toolbox.server) |
| 45 | +await reporterServer.registerFileServer(env.server) |
| 46 | +await reporterServer.registerFileServer(fileServer) |
| 47 | + |
| 48 | +const consoleReporter = new ConsoleReporter() |
| 49 | +consoleReporter.config.result = !!process.env.CI |
| 50 | +consoleReporter.connect(reporterServer) |
| 51 | + |
| 52 | +const conductors = [ |
| 53 | + new ChromeTestConductor(reporterServer, toolbox.url, 'Chrome, DTL8, React18', [ |
| 54 | + {server: env.url, paths: ['browser.bundle.js']}, |
| 55 | + {server: env.url, paths: [ |
| 56 | + 'libs/dom8/index.bundle.js', |
| 57 | + 'libs/react18/index.bundle.js', |
| 58 | + ]} |
| 59 | + ]), |
| 60 | + new NodeTestConductor(reporterServer, toolbox.url, 'Node, DTL8, React18', [ |
| 61 | + {server: new URL(`file://${env.provider.origin}`), paths: ['node.js']}, |
| 62 | + {server: env.url, paths: [ |
| 63 | + 'libs/dom8/index.bundle.js', |
| 64 | + 'libs/react18/index.bundle.js', |
| 65 | + ]}, |
| 66 | + ]), |
| 67 | +] |
| 68 | + |
| 69 | +if (process.env.CI) { |
| 70 | + conductors.push( |
| 71 | + new NodeTestConductor(reporterServer, toolbox.url, 'Node, DTL8, React17', [ |
| 72 | + {server: new URL(`file://${env.provider.origin}`), paths: ['node.js'] }, |
| 73 | + {server: env.url, paths: [ |
| 74 | + 'libs/dom8/index.bundle.js', |
| 75 | + 'libs/react17/index.bundle.js', |
| 76 | + ]}, |
| 77 | + ]) |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +onBuildDone(async () => { |
| 82 | + const files = { |
| 83 | + server: await fileServer.url, |
| 84 | + paths: Array.from(fileProvider.files.keys()).filter(filter), |
| 85 | + } |
| 86 | + const runs = conductors.map(c => c.createTestRun(files)) |
| 87 | + const stack = new TestRunStack(runs.map(r => r.run)) |
| 88 | + |
| 89 | + for (const r of runs) { |
| 90 | + await r.exec() |
| 91 | + } |
| 92 | + |
| 93 | + await stack.then() |
| 94 | + |
| 95 | + const coverageMap = IstanbulLibCoverage.createCoverageMap() |
| 96 | + for (const run of stack.runs) { |
| 97 | + for (const coverage of run.coverage.values()) { |
| 98 | + coverageMap.merge(coverage) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const sourceStore = IstanbulLibSourceMaps.createSourceMapStore() |
| 103 | + const reportContext = IstanbulLibReport.createContext({ |
| 104 | + coverageMap: await sourceStore.transformCoverage(coverageMap), |
| 105 | + dir: fileProvider.origin, |
| 106 | + sourceFinder: sourceStore.sourceFinder, |
| 107 | + defaultSummarizer: 'nested', |
| 108 | + watermarks: { |
| 109 | + branches: [80, 100], |
| 110 | + functions: [80, 100], |
| 111 | + lines: [80, 100], |
| 112 | + statements: [80, 100], |
| 113 | + }, |
| 114 | + }) |
| 115 | + |
| 116 | + IstanbulReports.create('text').execute(reportContext) |
| 117 | + |
| 118 | + if (process.env.CI) { |
| 119 | + toolbox.server.close() |
| 120 | + env.server.close() |
| 121 | + fileServer.close() |
| 122 | + buildProvider.close() |
| 123 | + reporterServer.close() |
| 124 | + conductors.forEach(c => c.close()) |
| 125 | + } |
| 126 | +}) |
0 commit comments