Skip to content

Commit

Permalink
chore(test): scenario for checking socket message order
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Ginsburg committed Jun 21, 2021
1 parent 2b71a3c commit 4d46e84
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/e2e/communications.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: Client-Server Communications
In order to use Karma
As a person who wants to write great tests
I want to be able to run tests from the command line.

Scenario: Messages arrive in order
Given a configuration with:
"""
files = ['communications/test.js'];
browsers = ['ChromeHeadlessNoSandbox'];
plugins = [
'karma-jasmine',
'karma-chrome-launcher'
];
"""
And the environment:
| DEBUG | * |
When I start Karma
Then the stderr contains in order socket.io logs
26 changes: 26 additions & 0 deletions test/e2e/step_definitions/core_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Given('a proxy on port {int} that prepends {string} to the base path', async fun
return this.proxy.start(proxyPort, proxyPath)
})

Given('the environment:', function (env) {
this.env = {}
for (const [key, value] of env.rawTable) {
this.env[key] = value
}
})

When('I stop a server programmatically', function (callback) {
setTimeout(() => {
stopper.stop(this.config, (exitCode) => {
Expand Down Expand Up @@ -149,3 +156,22 @@ Then(/^the file at ([a-zA-Z0-9/\\_.]+) contains:$/, function (filePath, expected
throw new Error('Expected output to match the following:\n ' + expectedOutput + '\nGot:\n ' + data)
}
})

Then('the stderr contains in order socket.io logs', function (callback) {
const regex = /emitting event (.*)/g
const found = Array.from(this.lastRun.stderr.matchAll(regex))
const events = []
// eslint-disable-next-line no-unused-vars
for (const [_, group] of found) {
events.push(JSON.parse(group))
}
const eventSequence = ['register', 'start', 'result', 'complete']
for (let i = 0; i < eventSequence.length; i++) {
const expectedEvent = eventSequence[i]
const actualEvent = events[i][0]
if (actualEvent !== expectedEvent) {
throw new Error(`Expected event '${expectedEvent}' at position ${i}, but got '${actualEvent}' instead`)
}
}
callback()
})
5 changes: 5 additions & 0 deletions test/e2e/support/communications/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('a suite', function () {
it('a spec', function () {
expect().nothing()
})
})
2 changes: 1 addition & 1 deletion test/e2e/support/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports = (config) => {

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

0 comments on commit 4d46e84

Please sign in to comment.