-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest.js
103 lines (92 loc) · 2.78 KB
/
test.js
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
102
103
const { reporter } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const { runCLI } = require('jest-cli')
const exitOnCatch = require('../lib/exitOnCatch')
const loadEnvFiles = require('../lib/loadEnvFiles')
const makePaths = require('../lib/paths')
const handler = async ({
verbose,
cwd,
testRegex,
updateSnapshot,
coverage,
watch,
watchAll,
}) => {
const paths = makePaths(cwd)
const mode = (process.env.NODE_ENV = process.env.BABEL_ENV = 'test')
loadEnvFiles(paths, mode)
reporter.info('Running tests...')
await exitOnCatch(
async () => {
const defaultJestConfig = require(paths.jestConfigDefaults)
const appJestConfig = fs.existsSync(paths.jestConfig)
? require(paths.jestConfig)
: {}
const pkgJestConfig = require(paths.package).jest
const jestConfig = {
roots: ['./src'],
...defaultJestConfig,
...appJestConfig,
...pkgJestConfig,
}
reporter.debug('Resolved jest config', jestConfig)
const ci = process.env.CI
const result = await runCLI(
{
testPathPattern: testRegex,
config: JSON.stringify(jestConfig),
updateSnapshot: !ci && updateSnapshot,
collectCoverage: coverage,
watch: (!ci && watch) || undefined,
watchAll: (!ci && watchAll) || undefined,
ci,
verbose: verbose,
},
[paths.base]
)
if (result.results.success) {
reporter.info(`Tests completed`)
} else {
reporter.error(`Tests failed`)
process.exit(1)
}
},
{
name: 'test',
onError: () =>
reporter.error('Test script exited with non-zero exit code'),
}
)
}
const command = {
command: 'test [testRegex]',
aliases: 't',
desc: 'Run application unit tests',
builder: {
updateSnapshot: {
type: 'boolean',
desc: 'Update jest snapshots',
aliases: 'u',
default: false,
},
coverage: {
type: 'boolean',
desc: 'Collect test coverage',
default: false,
},
watch: {
type: 'boolean',
desc: 'Watch modified source files for changes',
alias: 'w',
default: false,
},
watchAll: {
type: 'boolean',
desc: 'Watch all source files for changes',
default: false,
},
},
handler,
}
module.exports = command