Skip to content

Commit cf318e5

Browse files
devoto13Jonathan Ginsburg
authored and
Jonathan Ginsburg
committedNov 16, 2021
test: add test case for restarting test run on file change
1 parent 92ffe60 commit cf318e5

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
 

‎test/e2e/restart-on-change.feature

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Feature: Restart on file change
2+
In order to use Karma
3+
As a person who wants to write great tests
4+
I want Karma to re-run tests whenever file changes.
5+
6+
Scenario: Re-run tests when file changes
7+
Given a configuration with:
8+
"""
9+
files = ['basic/plus.js', 'basic/test.js'];
10+
browsers = ['ChromeHeadlessNoSandbox'];
11+
plugins = [
12+
'karma-jasmine',
13+
'karma-chrome-launcher'
14+
];
15+
restartOnFileChange = true;
16+
singleRun = false;
17+
"""
18+
When I start a server in background
19+
And I wait until server output contains:
20+
"""
21+
..
22+
Chrome Headless
23+
"""
24+
When I touch file: "basic/test.js"
25+
Then the background stdout matches RegExp:
26+
"""
27+
Executed 2 of 2 SUCCESS[\s\S]+Executed 2 of 2 SUCCESS
28+
"""

‎test/e2e/step_definitions/core_steps.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ When('I stop a server programmatically', function (callback) {
2727
})
2828

2929
When('I start a server in background', async function () {
30-
await this.runBackgroundProcess(['start', '--log-level', 'debug', this.configFile])
30+
await this.runBackgroundProcess(['start', this.configFile])
31+
})
32+
33+
When('I start a server in background with additional arguments: {string}', async function (args) {
34+
await this.runBackgroundProcess(['start', ...args.split(' '), this.configFile])
3135
})
3236

3337
When('I wait until server output contains:', async function (expectedOutput) {
@@ -50,6 +54,11 @@ When('I {command} Karma', async function (command) {
5054
await this.runForegroundProcess(`${command} ${this.configFile}`)
5155
})
5256

57+
When('I touch file: {string}', async function (file) {
58+
const now = new Date()
59+
await fs.promises.utimes(path.join(this.workDir, file), now, now)
60+
})
61+
5362
When('I {command} Karma with additional arguments: {string}', async function (command, args) {
5463
await this.runForegroundProcess(`${command} ${this.configFile} ${args}`)
5564
})
@@ -149,3 +158,27 @@ Then(/^the file at ([a-zA-Z0-9/\\_.]+) contains:$/, function (filePath, expected
149158
throw new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + data)
150159
}
151160
})
161+
162+
Then(/^the background (stdout|stderr) (is exactly|contains|matches RegExp):$/, async function (outputType, comparison, expectedOutput) {
163+
const message = comparison === 'is exactly' ? 'Expected output to be exactly as above, but got:'
164+
: comparison === 'contains' ? 'Expected output to contain the above text, but got:'
165+
: 'Expected output to match the above RegExp, but got:'
166+
167+
await waitForCondition(
168+
() => {
169+
const actualOutput = this.backgroundProcess[outputType].trim()
170+
expectedOutput = expectedOutput.trim()
171+
172+
switch (comparison) {
173+
case 'is exactly':
174+
return actualOutput === expectedOutput
175+
case 'contains':
176+
return actualOutput.includes(expectedOutput)
177+
case 'matches RegExp':
178+
return new RegExp(expectedOutput).test(actualOutput)
179+
}
180+
},
181+
5000,
182+
() => new Error(`${message}\n\n${this.backgroundProcess[outputType]}`)
183+
)
184+
})

0 commit comments

Comments
 (0)
Please sign in to comment.