This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
/
RunTests.js
207 lines (186 loc) · 4.9 KB
/
RunTests.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict'
/*
* adonis-vow
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const path = require('path')
const _ = require('lodash')
const { Command } = require('@adonisjs/ace')
const debug = require('debug')('adonis:vow:command')
class RunTests extends Command {
constructor (runner, cli, env) {
super()
this.runner = runner
this.cli = cli
this.env = env
}
/**
* The command signature
*
* @method signature
*
* @return {String}
*/
static get signature () {
return `test
{ group?=all: Define the test groups to be executed. Multiple groups are seperated by comma }
{ -b, --bail: Stop running tests on first failure }
{ -t, --timeout=@value: Define a global timeout for all the tests }
{ -f, --files=@value: Pick only specific files. File names are seperated by comma }
{ -g, --grep=@value: Grep on tests title to run only selected tests }
{ --glob=@value: Define a custom glob to pick test files }`
}
/**
* The command description
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Run application tests'
}
/**
* Injecting dependencies via IoC container
*
* @method inject
*
* @return {Array}
*/
static get inject () {
return ['Test/Runner', 'Test/Cli', 'Adonis/Src/Env']
}
/**
* Require the vow file from the project root
* if there
*
* @method _requireVowFile
*
* @param {String} projectRoot
*
* @return {void}
*
* @private
*/
_requireVowFile (projectRoot) {
try {
require(path.join(projectRoot, 'vowfile'))(this.cli, this.runner)
debug('loaded vowfile.js')
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') {
throw error
}
}
}
/**
* Handle method called by ace when test command
* is executed
*
* @method handle
*
* @param {String} options.group
* @param {Boolean} options.bail
* @param {Number} options.timeout
* @param {String} options.files
* @param {Boolean} options.grep
* @param {String} options.glob
*
* @return {void}
*/
async handle ({ group }, { bail, timeout, files, grep, glob }) {
const projectRoot = this.cli.projectRoot
this._requireVowFile(projectRoot)
this.runner.bail(bail || false)
/**
* If there is a global timeout set it on
* runner
*/
const numberedTimeout = Number(timeout)
if (timeout && !isNaN(numberedTimeout)) {
debug('global timeout %d', numberedTimeout)
this.runner.timeout(numberedTimeout)
}
/**
* If grep statement is defined, use it
*/
if (grep) {
debug('grep term %s', grep)
this.runner.grep(grep)
}
/**
* Override all existing groups with a custom
* glob group.
*/
if (typeof (glob) === 'string' && glob) {
this.cli.setGroups({ 'glob': glob })
}
/**
* Only run groups defined by the end user
*/
if (typeof (group) === 'string' && group !== 'all') {
const groups = group.split(',').map((group) => group.trim())
const selectedGroups = _.transform(this.cli.getGroups(), (result, glob, name) => {
if (_.includes(groups, name)) {
result[name] = glob
}
return result
}, {})
debug('executing %j groups', selectedGroups)
this.cli.setGroups(selectedGroups)
}
/**
* Only run tests for following files ( only if defined )
*
* @type {Array}
*/
const filesToPick = typeof (files) === 'string' ? files.split(',') : []
/**
* Getting all test files from the cli
*/
let testFiles = await this.cli.getTestFiles()
/**
* If there are specific files defined, then grep on
* them to pick only those files
*/
if (_.size(filesToPick)) {
testFiles = _.filter(testFiles, (file) => {
return !!_.find(filesToPick, (selectedFile) => {
return file.endsWith(selectedFile.trim())
})
})
debug('post --files filter %j', testFiles)
}
try {
/**
* Setting test url when its not defined
*/
if (!process.env.TEST_SERVER_URL) {
process.env.TEST_SERVER_URL = `http://${process.env.HOST}:${process.env.PORT}`
}
debug('running test server on %s', process.env.TEST_SERVER_URL)
/**
* Requiring all test files.
*/
_.each(testFiles, (file) => require(file))
/**
* Running the test runner
*/
await this.runner.run()
process.exit(0)
} catch (error) {
/**
* If log the error when test suite was not executed.
* Otherwise test reporter will report the error
*/
if (!this.runner.executedStack) {
console.error(error)
}
process.exit(1)
}
}
}
module.exports = RunTests