Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test): reduce execKarma to a reasonable size #3496

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/e2e/browser_console.feature
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ Feature: Browser Console Configuration
];
singleRun = false;
"""
When I run Karma
When I start a server in background
And I wait until server output contains:
"""
Executed 1 of 1 SUCCESS
"""
And I run Karma
Then it passes with like:
"""
LOG: 'foo'
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/error.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Feature: Error Display
"""
SyntaxError: Unexpected token '}'
"""

Scenario: Not single-run Syntax Error in a test file
Given a configuration with:
"""
Expand All @@ -29,7 +30,12 @@ Feature: Error Display
];
singleRun = false;
"""
When I run Karma
When I start a server in background
And I wait until server output contains:
"""
Executed 2 of 2 (1 FAILED)
"""
And I run Karma
Then it fails with like:
"""
SyntaxError: Unexpected token '}'
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/pass-opts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ Feature: Passing Options
singleRun = false;
"""
And command line arguments of: "-- arg1 arg2"
When I run Karma
When I start a server in background
And I wait until server output contains:
"""
Executed 1 of 1 (1 FAILED)
"""
And I run Karma
Then it passes with no debug:
"""
.
Expand Down
64 changes: 18 additions & 46 deletions test/e2e/step_definitions/core_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,21 @@ 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 configFile = this.configFile
const runtimePath = this.karmaExecutable
const baseDir = this.workDir

const executor = (done) => {
const cmd = runtimePath + ' ' + command + ' --log-level ' + level + ' ' + configFile + ' ' + additionalArgs

return exec(cmd, {
cwd: baseDir
}, done)
}

if (command === 'run') {
this.runBackgroundProcess(['start', '--log-level', 'warn', configFile])
.then(() => {
const cmd = runtimePath + ' run ' + configFile + ' ' + additionalArgs

setTimeout(() => {
exec(cmd, {
cwd: baseDir
}, (error, stdout, stderr) => {
if (error) {
this.lastRun.error = error
}
this.lastRun.stdout = stdout
this.lastRun.stderr = stderr
callback()
})
}, 1000)
})
.catch((error) => callback(error))
} else {
executor((error, stdout, stderr) => {
if (error) {
this.lastRun.error = error
}
this.lastRun.stdout = stdout
this.lastRun.stderr = stderr
callback()
})
}
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 () {
Expand Down Expand Up @@ -83,6 +50,10 @@ 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))
})

defineParameterType({
name: 'command',
regexp: /run|start|init|stop/
Expand All @@ -105,7 +76,7 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
const noDebug = mode === 'no debug'
const like = mode === 'like'
const regexp = mode === 'regexp'
let actualOutput = this.lastRun.stdout.toString()
let actualOutput = this.lastRun.stdout
let lines

if (noDebug) {
Expand All @@ -132,9 +103,9 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
})

Then('it fails with:', function (expectedOutput, callback) {
const actualOutput = this.lastRun.stdout.toString()
const actualOutput = this.lastRun.stdout
const actualError = this.lastRun.error
const actualStderr = this.lastRun.stderr.toString()
const actualStderr = this.lastRun.stderr

if (actualOutput.match(expectedOutput)) {
return callback()
Expand All @@ -146,9 +117,10 @@ Then('it fails with:', function (expectedOutput, callback) {
})

Then('it fails with like:', function (expectedOutput, callback) {
const actualOutput = this.lastRun.stdout.toString()
const actualOutput = this.lastRun.stdout
const actualError = this.lastRun.error
const actualStderr = this.lastRun.stderr.toString()
const actualStderr = this.lastRun.stderr

if (actualOutput.match(new RegExp(expectedOutput))) {
return callback()
}
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/step_definitions/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { promisify } = require('util')

const sleep = promisify(setTimeout)

module.exports.waitForCondition = async (evaluateCondition, timeout = 1000) => {
let remainingTime = timeout
while (!evaluateCondition()) {
if (remainingTime > 0) {
await sleep(50)
remainingTime -= 50
} else {
throw new Error(`Condition not fulfilled, waited ${timeout}ms`)
}
}
}