@@ -2,54 +2,21 @@ const { defineParameterType, Given, Then, When } = require('cucumber')
22const fs = require ( 'fs' )
33const path = require ( 'path' )
44const { exec } = require ( 'child_process' )
5+ const { waitForCondition } = require ( './utils' )
56const stopper = require ( '../../../lib/stopper' )
67
78let additionalArgs = [ ]
89
910function execKarma ( command , level , callback ) {
1011 level = level || 'warn'
1112
12- const configFile = this . configFile
13- const runtimePath = this . karmaExecutable
14- const baseDir = this . workDir
15-
16- const executor = ( done ) => {
17- const cmd = runtimePath + ' ' + command + ' --log-level ' + level + ' ' + configFile + ' ' + additionalArgs
18-
19- return exec ( cmd , {
20- cwd : baseDir
21- } , done )
22- }
23-
24- if ( command === 'run' ) {
25- this . runBackgroundProcess ( [ 'start' , '--log-level' , 'warn' , configFile ] )
26- . then ( ( ) => {
27- const cmd = runtimePath + ' run ' + configFile + ' ' + additionalArgs
28-
29- setTimeout ( ( ) => {
30- exec ( cmd , {
31- cwd : baseDir
32- } , ( error , stdout , stderr ) => {
33- if ( error ) {
34- this . lastRun . error = error
35- }
36- this . lastRun . stdout = stdout
37- this . lastRun . stderr = stderr
38- callback ( )
39- } )
40- } , 1000 )
41- } )
42- . catch ( ( error ) => callback ( error ) )
43- } else {
44- executor ( ( error , stdout , stderr ) => {
45- if ( error ) {
46- this . lastRun . error = error
47- }
48- this . lastRun . stdout = stdout
49- this . lastRun . stderr = stderr
50- callback ( )
51- } )
52- }
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+ } )
5320}
5421
5522Given ( 'a default configuration' , function ( ) {
@@ -83,6 +50,10 @@ When('I start a server in background', async function () {
8350 await this . runBackgroundProcess ( [ 'start' , '--log-level' , 'debug' , this . configFile ] )
8451} )
8552
53+ When ( 'I wait until server output contains:' , async function ( expectedOutput ) {
54+ await waitForCondition ( ( ) => this . backgroundProcess . stdout . includes ( expectedOutput ) )
55+ } )
56+
8657defineParameterType ( {
8758 name : 'command' ,
8859 regexp : / r u n | s t a r t | i n i t | s t o p /
@@ -105,7 +76,7 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
10576 const noDebug = mode === 'no debug'
10677 const like = mode === 'like'
10778 const regexp = mode === 'regexp'
108- let actualOutput = this . lastRun . stdout . toString ( )
79+ let actualOutput = this . lastRun . stdout
10980 let lines
11081
11182 if ( noDebug ) {
@@ -132,9 +103,9 @@ Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, f
132103} )
133104
134105Then ( 'it fails with:' , function ( expectedOutput , callback ) {
135- const actualOutput = this . lastRun . stdout . toString ( )
106+ const actualOutput = this . lastRun . stdout
136107 const actualError = this . lastRun . error
137- const actualStderr = this . lastRun . stderr . toString ( )
108+ const actualStderr = this . lastRun . stderr
138109
139110 if ( actualOutput . match ( expectedOutput ) ) {
140111 return callback ( )
@@ -146,9 +117,10 @@ Then('it fails with:', function (expectedOutput, callback) {
146117} )
147118
148119Then ( 'it fails with like:' , function ( expectedOutput , callback ) {
149- const actualOutput = this . lastRun . stdout . toString ( )
120+ const actualOutput = this . lastRun . stdout
150121 const actualError = this . lastRun . error
151- const actualStderr = this . lastRun . stderr . toString ( )
122+ const actualStderr = this . lastRun . stderr
123+
152124 if ( actualOutput . match ( new RegExp ( expectedOutput ) ) ) {
153125 return callback ( )
154126 }
0 commit comments