forked from karma-runner/karma
/
core_steps.js
151 lines (127 loc) · 4.97 KB
/
core_steps.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
const { defineParameterType, Given, Then, When } = require('cucumber')
const fs = require('fs')
const path = require('path')
const { waitForCondition } = require('./utils')
const stopper = require('../../../lib/stopper')
Given('a default configuration', function () {
this.writeConfigFile()
})
Given('a configuration with:', function (fileContent) {
this.updateConfig(fileContent)
this.writeConfigFile()
})
Given('a proxy on port {int} that prepends {string} to the base path', async function (proxyPort, proxyPath) {
return this.proxy.start(proxyPort, proxyPath)
})
When('I stop a server programmatically', function (callback) {
setTimeout(() => {
stopper.stop(this.config, (exitCode) => {
this.stopperExitCode = exitCode
callback()
})
}, 1000)
})
When('I start a server in background', async function () {
await this.runBackgroundProcess(['start', '--log-level', 'debug', this.configFile])
})
When('I wait until server output contains:', async function (expectedOutput) {
await waitForCondition(
() => this.backgroundProcess.stdout.includes(expectedOutput),
5000,
() => new Error(
'Expected server output to contain the above text within 5000ms, but got:\n\n' +
this.backgroundProcess.stdout
)
)
})
defineParameterType({
name: 'command',
regexp: /run|start|init|stop/
})
When('I {command} Karma', async function (command) {
await this.runForegroundProcess(`${command} ${this.configFile}`)
})
When('I {command} Karma with additional arguments: {string}', async function (command, args) {
await this.runForegroundProcess(`${command} ${this.configFile} ${args}`)
})
When('I execute Karma with arguments: {string}', async function (args) {
await this.runForegroundProcess(args)
})
Then(/^it passes with(:? (like|regexp))?:$/, { timeout: 10 * 1000 }, function (mode, expectedOutput, callback) {
const like = mode === 'like'
const regexp = mode === 'regexp'
let actualOutput = this.lastRun.stdout
if (like && actualOutput.indexOf(expectedOutput) >= 0) {
return callback()
}
if (regexp && actualOutput.match(expectedOutput)) {
return callback()
}
if (actualOutput.indexOf(expectedOutput) === 0) {
return callback()
}
if (actualOutput) {
return callback(new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + actualOutput))
}
callback(new Error('Failed all comparisons'))
})
Then('it fails with:', function (expectedOutput, callback) {
const actualOutput = this.lastRun.stdout
const actualError = this.lastRun.error
const actualStderr = this.lastRun.stderr
if (actualOutput.match(expectedOutput)) {
return callback()
}
if (actualError || actualStderr) {
return callback(new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + actualOutput))
}
})
Then('it fails with like:', function (expectedOutput, callback) {
const actualOutput = this.lastRun.stdout
const actualError = this.lastRun.error
const actualStderr = this.lastRun.stderr
if (actualOutput.match(new RegExp(expectedOutput))) {
return callback()
}
if (actualError || actualStderr) {
callback(new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + actualOutput))
}
})
Then(/^the (stdout|stderr) (is exactly|contains|matches RegExp):$/, function (outputType, comparison, expectedOutput) {
const actualOutput = (outputType === 'stdout' ? this.lastRun.stdout : this.lastRun.stderr).trim()
expectedOutput = expectedOutput.trim()
switch (comparison) {
case 'is exactly':
if (actualOutput !== expectedOutput) {
throw new Error(`Expected output to be exactly as above, but got:\n\n${actualOutput}`)
}
break
case 'contains':
if (!actualOutput.includes(expectedOutput)) {
throw new Error(`Expected output to contain the above text, but got:\n\n${actualOutput}`)
}
break
case 'matches RegExp':
if (!(new RegExp(expectedOutput).test(actualOutput))) {
throw new Error(`Expected output to match the above RegExp, but got:\n\n${actualOutput}`)
}
break
default:
throw new Error(`Unknown comparison type: ${comparison}`)
}
})
Then(/^The (server|stopper) is dead(:? with exit code (\d+))?$/, function (stopperOrServer, code, callback) {
const server = stopperOrServer === 'server'
setTimeout(() => {
const actualExitCode = server ? this.backgroundProcess.handle.exitCode : this.stopperExitCode
if (actualExitCode === undefined) return callback(new Error('Server has not exited.'))
if (code === undefined || parseInt(code, 10) === actualExitCode) return callback()
callback(new Error('Exit-code mismatch'))
}, 4000)
})
Then(/^the file at ([a-zA-Z0-9/\\_.]+) contains:$/, function (filePath, expectedOutput) {
const data = fs.readFileSync(path.join(this.workDir, filePath), 'utf8')
if (!data.match(expectedOutput)) {
throw new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + data)
}
})