Skip to content

Commit

Permalink
refactor(test): move execKarma into the World (#3500)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devoto13 committed May 7, 2020
1 parent f375884 commit 100b227
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
3 changes: 1 addition & 2 deletions test/e2e/pass-opts.feature
Expand Up @@ -14,13 +14,12 @@ Feature: Passing Options
];
singleRun = false;
"""
And command line arguments of: "-- arg1 arg2"
When I start a server in background
And I wait until server output contains:
"""
Executed 1 of 1 (1 FAILED)
"""
And I run Karma
And I run Karma with additional arguments: "-- arg1 arg2"
Then it passes with no debug:
"""
.
Expand Down
33 changes: 4 additions & 29 deletions test/e2e/step_definitions/core_steps.js
@@ -1,24 +1,9 @@
const { defineParameterType, Given, Then, When } = require('cucumber')
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const { waitForCondition } = require('./utils')
const stopper = require('../../../lib/stopper')

let additionalArgs = []

function execKarma (command, level, callback) {
level = level || 'warn'

const cmd = `${this.karmaExecutable} ${command} --log-level ${level} ${this.configFile} ${additionalArgs}`
exec(cmd, { cwd: this.workDir }, (error, stdout, stderr) => {
this.lastRun.error = error
this.lastRun.stdout = stdout.toString()
this.lastRun.stderr = stderr.toString()
callback()
})
}

Given('a default configuration', function () {
this.writeConfigFile()
})
Expand All @@ -28,11 +13,6 @@ Given('a configuration with:', function (fileContent) {
this.writeConfigFile()
})

Given('command line arguments of: {string}', function (args, callback) {
additionalArgs = args
return callback()
})

Given('a proxy on port {int} that prepends {string} to the base path', async function (proxyPort, proxyPath) {
return this.proxy.start(proxyPort, proxyPath)
})
Expand All @@ -59,17 +39,12 @@ defineParameterType({
regexp: /run|start|init|stop/
})

defineParameterType({
name: 'loglevel',
regexp: /info|error|warn|debug/
})

When('I {command} Karma', function (command, callback) {
execKarma.apply(this, [command, undefined, callback])
When('I {command} Karma', async function (command) {
await this.runForegroundProcess(`${command} ${this.configFile}`)
})

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

Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, function (mode, expectedOutput, callback) {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/stop.feature
Expand Up @@ -38,7 +38,7 @@ Feature: Stop karma
singleRun = false;
"""
When I start a server in background
And I stop Karma with log-level info
And I stop Karma with additional arguments: "--log-level info"
Then it passes with like:
"""
Server stopped.
Expand Down
14 changes: 13 additions & 1 deletion test/e2e/support/world.js
@@ -1,4 +1,4 @@
const { spawn } = require('child_process')
const { exec, spawn } = require('child_process')
const fs = require('fs')
const vm = require('vm')
const path = require('path')
Expand Down Expand Up @@ -42,6 +42,7 @@ class World {
frameworks: ['jasmine'],
basePath: this.workDir,
colors: false,
logLevel: 'warn',
// Current approach uses vm.runInNewContext() method to apply
// configuration overrides. With this approach config object is used as an
// evaluation context and as result none of the regular node module
Expand Down Expand Up @@ -145,6 +146,17 @@ module.exports = (config) => {
})
}
}

async runForegroundProcess (args) {
return new Promise((resolve) => {
exec(`${this.karmaExecutable} ${args}`, { cwd: this.workDir }, (error, stdout, stderr) => {
this.lastRun.error = error
this.lastRun.stdout = stdout.toString()
this.lastRun.stderr = stderr.toString()
resolve()
})
})
}
}

setWorldConstructor(World)
2 changes: 1 addition & 1 deletion test/e2e/upstream-proxy.feature
Expand Up @@ -18,7 +18,7 @@ Feature: UpstreamProxy
};
"""
And a proxy on port 9875 that prepends '/__proxy__/' to the base path
When I start Karma with log-level debug
When I start Karma with additional arguments: "--log-level debug"
Then it passes with regexp:
"""
Chrome Headless.*Executed.*SUCCESS
Expand Down

0 comments on commit 100b227

Please sign in to comment.