@remix-run/test 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +430 -2
- package/dist/app/client/entry.d.ts +2 -0
- package/dist/app/client/entry.d.ts.map +1 -0
- package/dist/app/client/entry.js +324 -0
- package/dist/app/client/iframe.d.ts +2 -0
- package/dist/app/client/iframe.d.ts.map +1 -0
- package/dist/app/client/iframe.js +22 -0
- package/dist/app/server.d.ts +6 -0
- package/dist/app/server.d.ts.map +1 -0
- package/dist/app/server.js +303 -0
- package/dist/cli-entry.d.ts +3 -0
- package/dist/cli-entry.d.ts.map +1 -0
- package/dist/cli-entry.js +14 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +305 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/lib/colors.d.ts +2 -0
- package/dist/lib/colors.d.ts.map +1 -0
- package/dist/lib/colors.js +2 -0
- package/dist/lib/config.d.ts +91 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +255 -0
- package/dist/lib/context.d.ts +93 -0
- package/dist/lib/context.d.ts.map +1 -0
- package/dist/lib/context.js +65 -0
- package/dist/lib/coverage-loader.d.ts +16 -0
- package/dist/lib/coverage-loader.d.ts.map +1 -0
- package/dist/lib/coverage-loader.js +20 -0
- package/dist/lib/coverage.d.ts +28 -0
- package/dist/lib/coverage.d.ts.map +1 -0
- package/dist/lib/coverage.js +212 -0
- package/dist/lib/executor.d.ts +4 -0
- package/dist/lib/executor.d.ts.map +1 -0
- package/dist/lib/executor.js +128 -0
- package/dist/lib/fake-timers.d.ts +6 -0
- package/dist/lib/fake-timers.d.ts.map +1 -0
- package/dist/lib/fake-timers.js +45 -0
- package/dist/lib/framework.d.ts +107 -0
- package/dist/lib/framework.d.ts.map +1 -0
- package/dist/lib/framework.js +198 -0
- package/dist/lib/import-module.d.ts +2 -0
- package/dist/lib/import-module.d.ts.map +1 -0
- package/dist/lib/import-module.js +29 -0
- package/dist/lib/mock.d.ts +52 -0
- package/dist/lib/mock.d.ts.map +1 -0
- package/dist/lib/mock.js +61 -0
- package/dist/lib/normalize.d.ts +2 -0
- package/dist/lib/normalize.d.ts.map +1 -0
- package/dist/lib/normalize.js +18 -0
- package/dist/lib/playwright.d.ts +15 -0
- package/dist/lib/playwright.d.ts.map +1 -0
- package/dist/lib/playwright.js +81 -0
- package/dist/lib/reporters/dot.d.ts +9 -0
- package/dist/lib/reporters/dot.d.ts.map +1 -0
- package/dist/lib/reporters/dot.js +56 -0
- package/dist/lib/reporters/files.d.ts +9 -0
- package/dist/lib/reporters/files.d.ts.map +1 -0
- package/dist/lib/reporters/files.js +71 -0
- package/dist/lib/reporters/index.d.ts +13 -0
- package/dist/lib/reporters/index.d.ts.map +1 -0
- package/dist/lib/reporters/index.js +18 -0
- package/dist/lib/reporters/results.d.ts +30 -0
- package/dist/lib/reporters/results.d.ts.map +1 -0
- package/dist/lib/reporters/results.js +1 -0
- package/dist/lib/reporters/spec.d.ts +9 -0
- package/dist/lib/reporters/spec.d.ts.map +1 -0
- package/dist/lib/reporters/spec.js +153 -0
- package/dist/lib/reporters/tap.d.ts +9 -0
- package/dist/lib/reporters/tap.d.ts.map +1 -0
- package/dist/lib/reporters/tap.js +54 -0
- package/dist/lib/runner-browser.d.ts +21 -0
- package/dist/lib/runner-browser.d.ts.map +1 -0
- package/dist/lib/runner-browser.js +117 -0
- package/dist/lib/runner.d.ts +14 -0
- package/dist/lib/runner.d.ts.map +1 -0
- package/dist/lib/runner.js +118 -0
- package/dist/lib/runtime.d.ts +2 -0
- package/dist/lib/runtime.d.ts.map +1 -0
- package/dist/lib/runtime.js +2 -0
- package/dist/lib/ts-transform.d.ts +4 -0
- package/dist/lib/ts-transform.d.ts.map +1 -0
- package/dist/lib/ts-transform.js +29 -0
- package/dist/lib/watcher.d.ts +5 -0
- package/dist/lib/watcher.d.ts.map +1 -0
- package/dist/lib/watcher.js +39 -0
- package/dist/lib/worker-e2e.d.ts +2 -0
- package/dist/lib/worker-e2e.d.ts.map +1 -0
- package/dist/lib/worker-e2e.js +49 -0
- package/dist/lib/worker.d.ts +2 -0
- package/dist/lib/worker.d.ts.map +1 -0
- package/dist/lib/worker.js +57 -0
- package/dist/test/coverage/fixture.d.ts +5 -0
- package/dist/test/coverage/fixture.d.ts.map +1 -0
- package/dist/test/coverage/fixture.js +32 -0
- package/dist/test/coverage/test-browser.d.ts +2 -0
- package/dist/test/coverage/test-browser.d.ts.map +1 -0
- package/dist/test/coverage/test-browser.js +24 -0
- package/dist/test/coverage/test-e2e.d.ts +2 -0
- package/dist/test/coverage/test-e2e.d.ts.map +1 -0
- package/dist/test/coverage/test-e2e.js +60 -0
- package/dist/test/coverage/test-unit.d.ts +2 -0
- package/dist/test/coverage/test-unit.d.ts.map +1 -0
- package/dist/test/coverage/test-unit.js +27 -0
- package/dist/test/framework.test.browser.d.ts +2 -0
- package/dist/test/framework.test.browser.d.ts.map +1 -0
- package/dist/test/framework.test.browser.js +107 -0
- package/dist/test/framework.test.e2e.d.ts +2 -0
- package/dist/test/framework.test.e2e.d.ts.map +1 -0
- package/dist/test/framework.test.e2e.js +34 -0
- package/package.json +79 -5
- package/src/app/client/entry.ts +353 -0
- package/src/app/client/iframe.ts +18 -0
- package/src/app/server.ts +336 -0
- package/src/cli-entry.ts +15 -0
- package/src/cli.ts +384 -0
- package/src/index.ts +16 -0
- package/src/lib/colors.ts +3 -0
- package/src/lib/config.ts +377 -0
- package/src/lib/context.ts +168 -0
- package/src/lib/coverage-loader.ts +31 -0
- package/src/lib/coverage.ts +320 -0
- package/src/lib/executor.ts +145 -0
- package/src/lib/fake-timers.ts +64 -0
- package/src/lib/framework.ts +251 -0
- package/src/lib/import-module.ts +29 -0
- package/src/lib/mock.ts +89 -0
- package/src/lib/normalize.ts +22 -0
- package/src/lib/playwright.ts +100 -0
- package/src/lib/reporters/dot.ts +58 -0
- package/src/lib/reporters/files.ts +77 -0
- package/src/lib/reporters/index.ts +27 -0
- package/src/lib/reporters/results.ts +29 -0
- package/src/lib/reporters/spec.ts +174 -0
- package/src/lib/reporters/tap.ts +58 -0
- package/src/lib/runner-browser.ts +165 -0
- package/src/lib/runner.ts +189 -0
- package/src/lib/runtime.ts +2 -0
- package/src/lib/ts-transform.ts +36 -0
- package/src/lib/watcher.ts +46 -0
- package/src/lib/worker-e2e.ts +54 -0
- package/src/lib/worker.ts +50 -0
- package/src/test/coverage/fixture.ts +34 -0
- package/src/test/coverage/test-browser.ts +29 -0
- package/src/test/coverage/test-e2e.ts +70 -0
- package/src/test/coverage/test-unit.ts +32 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import type { createCoverageMap as CreateCoverageMap } from 'istanbul-lib-coverage'
|
|
2
|
+
import type { createContext as CreateContext } from 'istanbul-lib-report'
|
|
3
|
+
import type IstanbulReports from 'istanbul-reports'
|
|
4
|
+
import * as fsp from 'node:fs/promises'
|
|
5
|
+
import { createRequire } from 'node:module'
|
|
6
|
+
import * as path from 'node:path'
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
import { colors } from './colors.ts'
|
|
9
|
+
import { transformTypeScript } from './ts-transform.ts'
|
|
10
|
+
|
|
11
|
+
// Istanbul packages are loaded lazily so that FORCE_COLOR can be set based on
|
|
12
|
+
// the actual TTY state before supports-color caches its detection result.
|
|
13
|
+
let _istanbul:
|
|
14
|
+
| {
|
|
15
|
+
V8ToIstanbul: any
|
|
16
|
+
createCoverageMap: typeof CreateCoverageMap
|
|
17
|
+
createContext: typeof CreateContext
|
|
18
|
+
reports: typeof IstanbulReports
|
|
19
|
+
}
|
|
20
|
+
| undefined
|
|
21
|
+
|
|
22
|
+
function getIstanbul() {
|
|
23
|
+
if (!_istanbul) {
|
|
24
|
+
process.env.FORCE_COLOR ??= process.stdout.isTTY ? '1' : '0'
|
|
25
|
+
let require = createRequire(import.meta.url)
|
|
26
|
+
_istanbul = {
|
|
27
|
+
V8ToIstanbul: require('v8-to-istanbul'),
|
|
28
|
+
createCoverageMap: (
|
|
29
|
+
require('istanbul-lib-coverage') as { createCoverageMap: typeof CreateCoverageMap }
|
|
30
|
+
).createCoverageMap,
|
|
31
|
+
createContext: (require('istanbul-lib-report') as { createContext: typeof CreateContext })
|
|
32
|
+
.createContext,
|
|
33
|
+
reports: require('istanbul-reports') as typeof IstanbulReports,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return _istanbul
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface CoverageConfig {
|
|
40
|
+
dir: string
|
|
41
|
+
include?: string[]
|
|
42
|
+
exclude?: string[]
|
|
43
|
+
statements?: number
|
|
44
|
+
lines?: number
|
|
45
|
+
branches?: number
|
|
46
|
+
functions?: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface V8CoverageEntry {
|
|
50
|
+
url: string
|
|
51
|
+
source?: string
|
|
52
|
+
functions: Array<{
|
|
53
|
+
functionName: string
|
|
54
|
+
isBlockCoverage: boolean
|
|
55
|
+
ranges: Array<{ startOffset: number; endOffset: number; count: number }>
|
|
56
|
+
}>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type CoverageMap = ReturnType<typeof CreateCoverageMap>
|
|
60
|
+
|
|
61
|
+
function matchesGlobs(filePath: string, globs: string[]): boolean {
|
|
62
|
+
return globs.some((glob) => path.matchesGlob(filePath, glob))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function filterCoverageMap(
|
|
66
|
+
coverageMap: CoverageMap,
|
|
67
|
+
cwd: string,
|
|
68
|
+
config: CoverageConfig,
|
|
69
|
+
): CoverageMap {
|
|
70
|
+
let filtered = getIstanbul().createCoverageMap({})
|
|
71
|
+
for (let filePath of coverageMap.files()) {
|
|
72
|
+
let relative = path.relative(cwd, filePath)
|
|
73
|
+
|
|
74
|
+
if (config.include && config.include.length > 0) {
|
|
75
|
+
if (!matchesGlobs(relative, config.include)) continue
|
|
76
|
+
}
|
|
77
|
+
if (config.exclude && config.exclude.length > 0) {
|
|
78
|
+
if (matchesGlobs(relative, config.exclude)) continue
|
|
79
|
+
}
|
|
80
|
+
let fc = coverageMap.fileCoverageFor(filePath) as any
|
|
81
|
+
filtered.addFileCoverage({ ...fc.toJSON(), path: relative })
|
|
82
|
+
}
|
|
83
|
+
return filtered
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function checkThresholds(coverageMap: CoverageMap, config: CoverageConfig): boolean {
|
|
87
|
+
let { statements, lines, branches, functions } = config
|
|
88
|
+
if (
|
|
89
|
+
statements === undefined &&
|
|
90
|
+
lines === undefined &&
|
|
91
|
+
branches === undefined &&
|
|
92
|
+
functions === undefined
|
|
93
|
+
)
|
|
94
|
+
return true
|
|
95
|
+
|
|
96
|
+
let summary = coverageMap.getCoverageSummary()
|
|
97
|
+
let passed = true
|
|
98
|
+
|
|
99
|
+
if (statements !== undefined) {
|
|
100
|
+
let pct = summary.statements.pct
|
|
101
|
+
if (pct < statements) {
|
|
102
|
+
console.error(
|
|
103
|
+
colors.red(
|
|
104
|
+
`\nError: Coverage threshold not met (statements ${pct.toFixed(2)}% < ${statements}%)`,
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
passed = false
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (lines !== undefined) {
|
|
111
|
+
let pct = summary.lines.pct
|
|
112
|
+
if (pct < lines) {
|
|
113
|
+
console.error(
|
|
114
|
+
colors.red(`\nError: Coverage threshold not met (lines ${pct.toFixed(2)}% < ${lines}%)`),
|
|
115
|
+
)
|
|
116
|
+
passed = false
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (branches !== undefined) {
|
|
120
|
+
let pct = summary.branches.pct
|
|
121
|
+
if (pct < branches) {
|
|
122
|
+
console.error(
|
|
123
|
+
colors.red(
|
|
124
|
+
`\nError: Coverage threshold not met (branches ${pct.toFixed(2)}% < ${branches}%)`,
|
|
125
|
+
),
|
|
126
|
+
)
|
|
127
|
+
passed = false
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (functions !== undefined) {
|
|
131
|
+
let pct = summary.functions.pct
|
|
132
|
+
if (pct < functions) {
|
|
133
|
+
console.error(
|
|
134
|
+
colors.red(
|
|
135
|
+
`\nError: Coverage threshold not met (functions ${pct.toFixed(2)}% < ${functions}%)`,
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
passed = false
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return passed
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function writeIstanbulReports(coverageMap: CoverageMap, cwd: string, outDir: string) {
|
|
146
|
+
await fsp.mkdir(outDir, { recursive: true })
|
|
147
|
+
let { createContext, reports } = getIstanbul()
|
|
148
|
+
let ctx = createContext({ coverageMap, dir: outDir } as any)
|
|
149
|
+
console.log('\nCoverage report:')
|
|
150
|
+
reports.create('text').execute(ctx)
|
|
151
|
+
reports.create('lcovonly').execute(ctx)
|
|
152
|
+
console.log(`\nLCOV coverage written to ${path.relative(cwd, path.join(outDir, 'lcov.info'))}`)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Convert a single V8 coverage entry to Istanbul format and merge it into the
|
|
156
|
+
// coverage map.
|
|
157
|
+
//
|
|
158
|
+
// V8 reports byte offsets against the JS bytes it actually instrumented. When
|
|
159
|
+
// the entry already carries that source (Playwright's `coverage.stopJSCoverage`
|
|
160
|
+
// returns it on each entry, including the inline source map), we hand it
|
|
161
|
+
// straight to v8-to-istanbul so the offsets line up exactly. The server path
|
|
162
|
+
// uses Node's `NODE_V8_COVERAGE` JSON, which doesn't include source — there we
|
|
163
|
+
// re-derive by re-running our esbuild transform on the original TS file.
|
|
164
|
+
async function addV8EntryToCoverageMap(
|
|
165
|
+
coverageMap: CoverageMap,
|
|
166
|
+
filePath: string,
|
|
167
|
+
functions: V8CoverageEntry['functions'],
|
|
168
|
+
source: string,
|
|
169
|
+
): Promise<boolean> {
|
|
170
|
+
let { V8ToIstanbul } = getIstanbul()
|
|
171
|
+
let converter = new V8ToIstanbul(filePath, undefined, { source })
|
|
172
|
+
await converter.load()
|
|
173
|
+
converter.applyCoverage(functions)
|
|
174
|
+
coverageMap.merge(converter.toIstanbul())
|
|
175
|
+
return true
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function shouldExcludeFromCoverage(
|
|
179
|
+
filePath: string,
|
|
180
|
+
rootDir: string,
|
|
181
|
+
testFiles: Set<string>,
|
|
182
|
+
): boolean {
|
|
183
|
+
return (
|
|
184
|
+
!filePath.startsWith(rootDir + path.sep) ||
|
|
185
|
+
filePath.includes(`${path.sep}node_modules${path.sep}`) ||
|
|
186
|
+
testFiles.has(filePath)
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function collectServerCoverageMap(
|
|
191
|
+
coverageDataDir: string,
|
|
192
|
+
cwd: string,
|
|
193
|
+
testFiles: Set<string>,
|
|
194
|
+
): Promise<CoverageMap | null> {
|
|
195
|
+
let { createCoverageMap } = getIstanbul()
|
|
196
|
+
let coverageMap = createCoverageMap({})
|
|
197
|
+
let converted = 0
|
|
198
|
+
|
|
199
|
+
let files: string[]
|
|
200
|
+
try {
|
|
201
|
+
files = (await fsp.readdir(coverageDataDir)).filter(
|
|
202
|
+
(f) => f.startsWith('coverage-') && f.endsWith('.json'),
|
|
203
|
+
)
|
|
204
|
+
} catch {
|
|
205
|
+
return null
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for (let file of files) {
|
|
209
|
+
let data = JSON.parse(await fsp.readFile(path.join(coverageDataDir, file), 'utf-8'))
|
|
210
|
+
let scriptCoverages: Array<{ url: string; functions: any[] }> = data.result ?? []
|
|
211
|
+
|
|
212
|
+
for (let entry of scriptCoverages) {
|
|
213
|
+
if (!entry.url.startsWith('file://')) continue
|
|
214
|
+
|
|
215
|
+
let filePath: string
|
|
216
|
+
try {
|
|
217
|
+
filePath = fileURLToPath(entry.url)
|
|
218
|
+
} catch {
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (
|
|
223
|
+
!filePath ||
|
|
224
|
+
!['.ts', '.tsx'].includes(path.extname(filePath)) ||
|
|
225
|
+
shouldExcludeFromCoverage(filePath, cwd, testFiles)
|
|
226
|
+
) {
|
|
227
|
+
continue
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
// For server unit tests, we transform the TS with a module loader and V8 tracks
|
|
232
|
+
// coverage using byte offsets from the transformed JS. Re-transform with the
|
|
233
|
+
// same `esbuild` call here so offsets align, then pass the result with its
|
|
234
|
+
// inline source map to v8-to-istanbul.
|
|
235
|
+
let tsSource = await fsp.readFile(filePath, 'utf-8')
|
|
236
|
+
let { code } = await transformTypeScript(tsSource, filePath)
|
|
237
|
+
let success = await addV8EntryToCoverageMap(coverageMap, filePath, entry.functions, code)
|
|
238
|
+
if (success) converted++
|
|
239
|
+
} catch (e) {
|
|
240
|
+
// Skip files that can't be converted
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Clean up raw V8 coverage JSON files now that we've processed them
|
|
246
|
+
//await Promise.all(files.map((f) => fsp.rm(path.join(coverageDataDir, f), { force: true })))
|
|
247
|
+
|
|
248
|
+
return converted > 0 ? coverageMap : null
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export async function collectCoverageMapFromPlaywright(
|
|
252
|
+
entries: V8CoverageEntry[],
|
|
253
|
+
rootDir: string,
|
|
254
|
+
testFiles: Set<string>,
|
|
255
|
+
resolveRelativePath: (url: string) => Promise<string | null>,
|
|
256
|
+
): Promise<CoverageMap | null> {
|
|
257
|
+
let { createCoverageMap } = getIstanbul()
|
|
258
|
+
let coverageMap = createCoverageMap({})
|
|
259
|
+
let converted = 0
|
|
260
|
+
|
|
261
|
+
for (let entry of entries) {
|
|
262
|
+
let filePath: string
|
|
263
|
+
try {
|
|
264
|
+
let relativePath = await resolveRelativePath(new URL(entry.url).pathname)
|
|
265
|
+
if (!relativePath) continue
|
|
266
|
+
|
|
267
|
+
// Ignore entries outside the root dir, entries in node_modules, and test files
|
|
268
|
+
filePath = path.resolve(rootDir, relativePath)
|
|
269
|
+
if (shouldExcludeFromCoverage(filePath, rootDir, testFiles)) {
|
|
270
|
+
continue
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Ensure file exists
|
|
274
|
+
await fsp.access(filePath)
|
|
275
|
+
} catch {
|
|
276
|
+
continue
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (!entry.source) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
`Entry for ${entry.url} is missing source, cannot convert coverage. Ensure the browser launched with Playwright's JS coverage enabled.`,
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
let success = await addV8EntryToCoverageMap(
|
|
287
|
+
coverageMap,
|
|
288
|
+
filePath,
|
|
289
|
+
entry.functions,
|
|
290
|
+
entry.source,
|
|
291
|
+
)
|
|
292
|
+
if (success) converted++
|
|
293
|
+
} catch {
|
|
294
|
+
// Skip files that can't be converted
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return converted > 0 ? coverageMap : null
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export async function generateCombinedCoverageReport(
|
|
302
|
+
maps: (CoverageMap | null | undefined)[],
|
|
303
|
+
cwd: string,
|
|
304
|
+
config: CoverageConfig,
|
|
305
|
+
): Promise<boolean> {
|
|
306
|
+
let { createCoverageMap } = getIstanbul()
|
|
307
|
+
let combined = createCoverageMap({})
|
|
308
|
+
for (let map of maps) {
|
|
309
|
+
if (map) combined.merge(map)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (combined.files().length === 0) {
|
|
313
|
+
console.log('No coverage data collected.')
|
|
314
|
+
return true
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let filtered = filterCoverageMap(combined, cwd, config)
|
|
318
|
+
await writeIstanbulReports(filtered, cwd, config.dir)
|
|
319
|
+
return checkThresholds(filtered, config)
|
|
320
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { createTestContext, type CreateTestContextOptions } from './context.ts'
|
|
2
|
+
import type { V8CoverageEntry } from './coverage.ts'
|
|
3
|
+
import type { TestResult, TestResults } from './reporters/results.ts'
|
|
4
|
+
|
|
5
|
+
export async function runTests(
|
|
6
|
+
options?: Omit<CreateTestContextOptions, 'addE2ECoverageEntries'>,
|
|
7
|
+
): Promise<TestResults> {
|
|
8
|
+
let suites = (globalThis as any).__testSuites || []
|
|
9
|
+
let e2eCoverageEntries: Array<{ entries: V8CoverageEntry[]; baseUrl: string }> = []
|
|
10
|
+
let results: TestResults = {
|
|
11
|
+
passed: 0,
|
|
12
|
+
failed: 0,
|
|
13
|
+
skipped: 0,
|
|
14
|
+
todo: 0,
|
|
15
|
+
tests: [],
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let hasOnlySuites = suites.some((s: any) => s.only)
|
|
19
|
+
|
|
20
|
+
for (let suite of suites) {
|
|
21
|
+
// If any suite uses .only, skip all non-only suites
|
|
22
|
+
if (hasOnlySuites && !suite.only) {
|
|
23
|
+
for (let test of suite.tests) {
|
|
24
|
+
results.tests.push({
|
|
25
|
+
name: test.name,
|
|
26
|
+
suiteName: suite.name,
|
|
27
|
+
status: 'skipped',
|
|
28
|
+
duration: 0,
|
|
29
|
+
})
|
|
30
|
+
results.skipped++
|
|
31
|
+
}
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (suite.skip || suite.todo) {
|
|
36
|
+
let status: 'skipped' | 'todo' = suite.todo ? 'todo' : 'skipped'
|
|
37
|
+
for (let test of suite.tests) {
|
|
38
|
+
results.tests.push({ name: test.name, suiteName: suite.name, status, duration: 0 })
|
|
39
|
+
results[status]++
|
|
40
|
+
}
|
|
41
|
+
// describe.todo('name') with no tests — add placeholder so suite appears in output
|
|
42
|
+
if (suite.tests.length === 0) {
|
|
43
|
+
results.tests.push({ name: '', suiteName: suite.name, status, duration: 0 })
|
|
44
|
+
results[status]++
|
|
45
|
+
}
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (suite.beforeAll) {
|
|
50
|
+
try {
|
|
51
|
+
await suite.beforeAll()
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error(`beforeAll failed in suite "${suite.name}":`, error)
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let hasOnlyTests = suite.tests.some((t: any) => t.only)
|
|
59
|
+
|
|
60
|
+
for (let test of suite.tests) {
|
|
61
|
+
// If any test uses .only, skip all non-only tests in this suite
|
|
62
|
+
if (hasOnlyTests && !test.only) {
|
|
63
|
+
results.tests.push({
|
|
64
|
+
name: test.name,
|
|
65
|
+
suiteName: suite.name,
|
|
66
|
+
status: 'skipped',
|
|
67
|
+
duration: 0,
|
|
68
|
+
})
|
|
69
|
+
results.skipped++
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (test.skip || test.todo) {
|
|
74
|
+
let status: 'skipped' | 'todo' = test.todo ? 'todo' : 'skipped'
|
|
75
|
+
results.tests.push({ name: test.name, suiteName: suite.name, status, duration: 0 })
|
|
76
|
+
results[status]++
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let startTime = performance.now()
|
|
81
|
+
let result: TestResult = {
|
|
82
|
+
name: test.name,
|
|
83
|
+
suiteName: suite.name,
|
|
84
|
+
status: 'passed',
|
|
85
|
+
duration: 0,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let contextOpts: CreateTestContextOptions | undefined = options
|
|
89
|
+
? {
|
|
90
|
+
...options,
|
|
91
|
+
addE2ECoverageEntries: (e) => e2eCoverageEntries.push(e),
|
|
92
|
+
}
|
|
93
|
+
: undefined
|
|
94
|
+
let { testContext, cleanup } = createTestContext(contextOpts)
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
if (suite.beforeEach) {
|
|
98
|
+
await suite.beforeEach()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
await test.fn(testContext)
|
|
102
|
+
|
|
103
|
+
result.status = 'passed'
|
|
104
|
+
results.passed++
|
|
105
|
+
} catch (error: any) {
|
|
106
|
+
result.status = 'failed'
|
|
107
|
+
result.error = {
|
|
108
|
+
message: error.message || String(error),
|
|
109
|
+
stack: error.stack,
|
|
110
|
+
}
|
|
111
|
+
results.failed++
|
|
112
|
+
} finally {
|
|
113
|
+
await cleanup()
|
|
114
|
+
if (suite.afterEach) {
|
|
115
|
+
try {
|
|
116
|
+
await suite.afterEach()
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error('afterEach failed:', error)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
result.duration = performance.now() - startTime
|
|
123
|
+
results.tests.push(result)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (suite.afterAll) {
|
|
128
|
+
try {
|
|
129
|
+
await suite.afterAll()
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error(`afterAll failed in suite "${suite.name}":`, error)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Clear suites in-place so the shared framework module is reset
|
|
137
|
+
// for the next test file (which reuses the same cached module instance)
|
|
138
|
+
suites.length = 0
|
|
139
|
+
|
|
140
|
+
if (e2eCoverageEntries.length > 0) {
|
|
141
|
+
results.e2eBrowserCoverageEntries = e2eCoverageEntries
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return results
|
|
145
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { mock } from './mock.ts'
|
|
2
|
+
|
|
3
|
+
export interface FakeTimers {
|
|
4
|
+
advance(ms: number): void
|
|
5
|
+
restore(): void
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function createFakeTimers(): FakeTimers {
|
|
9
|
+
let currentTime = 0
|
|
10
|
+
let nextId = 1
|
|
11
|
+
let pending: Array<{ id: number; fn: () => void; time: number; repeatMs?: number }> = []
|
|
12
|
+
|
|
13
|
+
function schedule(fn: () => void, delay: number, repeatMs?: number): number {
|
|
14
|
+
let id = nextId++
|
|
15
|
+
pending.push({ id, fn, time: currentTime + Math.max(0, delay), repeatMs })
|
|
16
|
+
return id
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function cancel(id: number) {
|
|
20
|
+
pending = pending.filter((t) => t.id !== id)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let setTimeoutMock = mock.method(globalThis, 'setTimeout', ((fn: () => void, delay = 0) =>
|
|
24
|
+
schedule(fn, delay)) as unknown as typeof setTimeout)
|
|
25
|
+
let clearTimeoutMock = mock.method(
|
|
26
|
+
globalThis,
|
|
27
|
+
'clearTimeout',
|
|
28
|
+
cancel as unknown as typeof clearTimeout,
|
|
29
|
+
)
|
|
30
|
+
let setIntervalMock = mock.method(globalThis, 'setInterval', ((fn: () => void, delay = 0) =>
|
|
31
|
+
schedule(fn, delay, Math.max(0, delay))) as unknown as typeof setInterval)
|
|
32
|
+
let clearIntervalMock = mock.method(
|
|
33
|
+
globalThis,
|
|
34
|
+
'clearInterval',
|
|
35
|
+
cancel as unknown as typeof clearInterval,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
advance(ms: number) {
|
|
40
|
+
let targetTime = currentTime + ms
|
|
41
|
+
while (true) {
|
|
42
|
+
let next = pending.filter((t) => t.time <= targetTime).sort((a, b) => a.time - b.time)[0]
|
|
43
|
+
if (!next) break
|
|
44
|
+
currentTime = next.time
|
|
45
|
+
pending = pending.filter((t) => t.id !== next.id)
|
|
46
|
+
// Requeue intervals before running the callback so that calling
|
|
47
|
+
// clearInterval(id) from inside the callback can cancel the next firing.
|
|
48
|
+
if (next.repeatMs !== undefined) {
|
|
49
|
+
pending.push({ ...next, time: next.time + Math.max(1, next.repeatMs) })
|
|
50
|
+
}
|
|
51
|
+
next.fn()
|
|
52
|
+
}
|
|
53
|
+
currentTime = targetTime
|
|
54
|
+
},
|
|
55
|
+
restore() {
|
|
56
|
+
setTimeoutMock.mock.restore?.()
|
|
57
|
+
clearTimeoutMock.mock.restore?.()
|
|
58
|
+
setIntervalMock.mock.restore?.()
|
|
59
|
+
clearIntervalMock.mock.restore?.()
|
|
60
|
+
pending = []
|
|
61
|
+
currentTime = 0
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
}
|