Skip to content

Commit 100b227

Browse files
authored
refactor(test): move execKarma into the World (#3500)
Also generalize a step to accept any CLI arguments instead of only log-level. This should make this step more flexible. The removed step was not very clear as to which command it applies to (foreground or background) and is fully covered with expanded "I {command} Karma ..." step.
1 parent f375884 commit 100b227

File tree

5 files changed

+20
-34
lines changed

5 files changed

+20
-34
lines changed

test/e2e/pass-opts.feature

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ Feature: Passing Options
1414
];
1515
singleRun = false;
1616
"""
17-
And command line arguments of: "-- arg1 arg2"
1817
When I start a server in background
1918
And I wait until server output contains:
2019
"""
2120
Executed 1 of 1 (1 FAILED)
2221
"""
23-
And I run Karma
22+
And I run Karma with additional arguments: "-- arg1 arg2"
2423
Then it passes with no debug:
2524
"""
2625
.

test/e2e/step_definitions/core_steps.js

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
const { defineParameterType, Given, Then, When } = require('cucumber')
22
const fs = require('fs')
33
const path = require('path')
4-
const { exec } = require('child_process')
54
const { waitForCondition } = require('./utils')
65
const stopper = require('../../../lib/stopper')
76

8-
let additionalArgs = []
9-
10-
function execKarma (command, level, callback) {
11-
level = level || 'warn'
12-
13-
const cmd = `${this.karmaExecutable} ${command} --log-level ${level} ${this.configFile} ${additionalArgs}`
14-
exec(cmd, { cwd: this.workDir }, (error, stdout, stderr) => {
15-
this.lastRun.error = error
16-
this.lastRun.stdout = stdout.toString()
17-
this.lastRun.stderr = stderr.toString()
18-
callback()
19-
})
20-
}
21-
227
Given('a default configuration', function () {
238
this.writeConfigFile()
249
})
@@ -28,11 +13,6 @@ Given('a configuration with:', function (fileContent) {
2813
this.writeConfigFile()
2914
})
3015

31-
Given('command line arguments of: {string}', function (args, callback) {
32-
additionalArgs = args
33-
return callback()
34-
})
35-
3616
Given('a proxy on port {int} that prepends {string} to the base path', async function (proxyPort, proxyPath) {
3717
return this.proxy.start(proxyPort, proxyPath)
3818
})
@@ -59,17 +39,12 @@ defineParameterType({
5939
regexp: /run|start|init|stop/
6040
})
6141

62-
defineParameterType({
63-
name: 'loglevel',
64-
regexp: /info|error|warn|debug/
65-
})
66-
67-
When('I {command} Karma', function (command, callback) {
68-
execKarma.apply(this, [command, undefined, callback])
42+
When('I {command} Karma', async function (command) {
43+
await this.runForegroundProcess(`${command} ${this.configFile}`)
6944
})
7045

71-
When('I {command} Karma with log-level {loglevel}', function (command, level, callback) {
72-
execKarma.apply(this, [command, level, callback])
46+
When('I {command} Karma with additional arguments: {string}', async function (command, args) {
47+
await this.runForegroundProcess(`${command} ${this.configFile} ${args}`)
7348
})
7449

7550
Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, function (mode, expectedOutput, callback) {

test/e2e/stop.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Feature: Stop karma
3838
singleRun = false;
3939
"""
4040
When I start a server in background
41-
And I stop Karma with log-level info
41+
And I stop Karma with additional arguments: "--log-level info"
4242
Then it passes with like:
4343
"""
4444
Server stopped.

test/e2e/support/world.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { spawn } = require('child_process')
1+
const { exec, spawn } = require('child_process')
22
const fs = require('fs')
33
const vm = require('vm')
44
const path = require('path')
@@ -42,6 +42,7 @@ class World {
4242
frameworks: ['jasmine'],
4343
basePath: this.workDir,
4444
colors: false,
45+
logLevel: 'warn',
4546
// Current approach uses vm.runInNewContext() method to apply
4647
// configuration overrides. With this approach config object is used as an
4748
// evaluation context and as result none of the regular node module
@@ -145,6 +146,17 @@ module.exports = (config) => {
145146
})
146147
}
147148
}
149+
150+
async runForegroundProcess (args) {
151+
return new Promise((resolve) => {
152+
exec(`${this.karmaExecutable} ${args}`, { cwd: this.workDir }, (error, stdout, stderr) => {
153+
this.lastRun.error = error
154+
this.lastRun.stdout = stdout.toString()
155+
this.lastRun.stderr = stderr.toString()
156+
resolve()
157+
})
158+
})
159+
}
148160
}
149161

150162
setWorldConstructor(World)

test/e2e/upstream-proxy.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Feature: UpstreamProxy
1818
};
1919
"""
2020
And a proxy on port 9875 that prepends '/__proxy__/' to the base path
21-
When I start Karma with log-level debug
21+
When I start Karma with additional arguments: "--log-level debug"
2222
Then it passes with regexp:
2323
"""
2424
Chrome Headless.*Executed.*SUCCESS

0 commit comments

Comments
 (0)