Skip to content

Commit b6dec91

Browse files
committed
e2e: create basic background & cleanup steps
Add 'basic.feature' to test common workflows using the bundle server. Initially, add a common "Background" step starting the bundle web server at the specified port, and a common "After" step run when a test ends (successful or not) stopping that web server process. Signed-off-by: Victoria Dye <[email protected]>
1 parent bc8df92 commit b6dec91

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

test/e2e/cucumber.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ module.exports = {
77
formatOptions: {
88
snippetInterface: 'async-await'
99
},
10+
worldParameters: {
11+
bundleWebServerCommand: '../../bin/git-bundle-web-server',
12+
}
1013
}
1114
}

test/e2e/features/basic.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: Basic bundle server usage
2+
3+
Background: The bundle web server is running
4+
Given the bundle web server was started at port 8080
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as child_process from 'child_process'
2+
3+
export class BundleServer {
4+
private bundleWebServerCmd: string
5+
6+
// Web server
7+
private webServerProcess: child_process.ChildProcess | undefined
8+
9+
constructor(bundleWebServerCmd: string) {
10+
this.bundleWebServerCmd = bundleWebServerCmd
11+
}
12+
13+
startWebServer(port: number): void {
14+
if (this.webServerProcess) {
15+
throw new Error("Tried to start web server, but web server is already running")
16+
}
17+
this.webServerProcess = child_process.spawn(this.bundleWebServerCmd, ["--port", String(port)])
18+
}
19+
20+
cleanup(): void {
21+
if (this.webServerProcess) {
22+
const killed = this.webServerProcess.kill('SIGINT')
23+
if (!killed) {
24+
console.warn("Web server process was not successfully stopped")
25+
}
26+
}
27+
}
28+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { BundleServerWorld, } from '../support/world'
2+
import { Given } from '@cucumber/cucumber'
3+
4+
Given('the bundle web server was started at port {int}', async function (this: BundleServerWorld, port: number) {
5+
this.bundleServer.startWebServer(port)
6+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BundleServerWorld } from '../support/world'
2+
import { After } from '@cucumber/cucumber'
3+
4+
/**
5+
* Steps handling operations that are common across tests.
6+
*/
7+
8+
After(function (this: BundleServerWorld) {
9+
this.cleanup()
10+
});

test/e2e/features/support/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as path from 'path'
2+
3+
export function absPath(pathParam: string): string {
4+
// Convert a given path (either relative to 'test/e2e/' or absolute) to an
5+
// absolute path
6+
if (!path.isAbsolute(pathParam)) {
7+
return path.resolve(__dirname, "../..", pathParam)
8+
} else {
9+
return pathParam
10+
}
11+
}

test/e2e/features/support/world.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { setWorldConstructor, World, IWorldOptions } from '@cucumber/cucumber'
2+
import * as utils from './utils'
3+
import { BundleServer } from '../classes/bundleServer'
4+
5+
interface BundleServerParameters {
6+
bundleWebServerCommand: string
7+
}
8+
9+
export class BundleServerWorld extends World<BundleServerParameters> {
10+
// Bundle server
11+
bundleServer: BundleServer
12+
13+
constructor(options: IWorldOptions<BundleServerParameters>) {
14+
super(options)
15+
16+
// Set up the bundle server
17+
this.bundleServer = new BundleServer(utils.absPath(this.parameters.bundleWebServerCommand))
18+
}
19+
20+
cleanup(): void {
21+
this.bundleServer.cleanup()
22+
}
23+
}
24+
25+
setWorldConstructor(BundleServerWorld)

0 commit comments

Comments
 (0)