This repository was archived by the owner on Sep 25, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
101 lines (90 loc) · 2.41 KB
/
Copy pathindex.ts
File metadata and controls
101 lines (90 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* @japa/run-failed-tests
*
* (c) Japa.dev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { join } from 'path'
import { PluginFn } from '@japa/runner'
import findCacheDir from 'find-cache-dir'
import { sticker } from '@poppinss/cliui'
import { outputJson, readJson } from 'fs-extra'
/**
* Returns the summary from the summary file
*/
async function getSummary(summaryFilePath: string): Promise<{ tests?: string[] }> {
try {
return await readJson(summaryFilePath)
} catch (error) {
if (error.code === 'ENOENT') {
return {}
}
throw new Error(`"@japa/run-failed-tests": ${error.message}`)
}
}
/**
* Perists summary with failing tests to the disk
*/
async function writeSummary(
summaryFilePath: string,
contents: { tests?: string[] }
): Promise<void> {
await outputJson(summaryFilePath, contents)
}
/**
* Plugin function to run failed tests only. Only for "@japa/runner"
*/
export function runFailedTests(options?: {
summaryFilePath?: string
ignoreFilesFilter?: boolean
}): PluginFn {
options = Object.assign({}, options)
return async function (config) {
/**
* Do not overwrite existing filters
*/
if (config.filters.tests?.length) {
return
}
/**
* Use the default path when no explicit path is provided
*/
if (!options!.summaryFilePath) {
options!.summaryFilePath = join(
findCacheDir({ name: '@japa/run-failed-tests' }, 'summary.json')
)
}
/**
* Get summary and look for failed tests
*/
const summary = await getSummary(options!.summaryFilePath)
/**
* Apply the filter when there are one or more failed tests
*/
if (summary.tests?.length) {
config.filters.tests = summary.tests
sticker()
.heading('"@japa/run-failed-tests"')
.add('')
.add(`${summary.tests.length} failed test(s) found`)
.add('Applying filter to run only failed tests')
.render()
/**
* Empty out files filter when "ignoreFilesFilter" is set to true
*/
if (options!.ignoreFilesFilter) {
config.filters.files = []
}
}
/**
* Persist file during teardown
*/
config.teardown.push(async (runner) => {
await writeSummary(options!.summaryFilePath!, {
tests: runner.getSummary().failedTestsTitles,
})
})
}
}