Skip to content

Commit

Permalink
test: convert test webserver to module
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Mar 7, 2024
1 parent 3066804 commit ef6ed5d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 88 deletions.
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var umdWrapper = require('esbuild-plugin-umd-wrapper');
var banner = require('./src/fragments/license');
var process = require('process');
var stripLogsPlugin = require('./grunt/esbuild/strip-logs').default;
var kexec = require('kexec');
var MochaServer = require('./test/web_server');

module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-webpack');
Expand Down Expand Up @@ -85,7 +85,8 @@ module.exports = function (grunt) {
grunt.registerTask('all', ['build', 'requirejs']);

grunt.registerTask('mocha:webserver', 'Run the Mocha web server', function () {
kexec('test/web_server');
var done = this.async();
new MochaServer().listen().then(done);
});

grunt.registerTask('build:browser', function () {
Expand Down
34 changes: 0 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"hexy": "~0.2",
"jmespath": "^0.16.0",
"jsdom": "^20.0.0",
"kexec": "ably-forks/node-kexec#update-for-node-12",
"minimist": "^1.2.5",
"mocha": "^8.1.3",
"mocha-junit-reporter": "^2.2.1",
Expand Down
8 changes: 4 additions & 4 deletions test/support/runPlaywrightTests.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const playwright = require('playwright');
const path = require('path');
const mochaWebServerProcess = require('child_process').fork(path.resolve(__dirname, '..', 'web_server'), {
env: { PLAYWRIGHT_TEST: 1 },
});
const MochaServer = require('../web_server');
const fs = require('fs');
const jUnitDirectoryPath = require('./junit_directory_path');

const port = process.env.PORT || 3000;
const host = 'localhost';
const playwrightBrowsers = ['chromium', 'firefox', 'webkit'];
const mochaServer = new MochaServer(/* playwrightTest: */ true);

const runTests = async (browserType) => {
mochaServer.listen();
const browser = await browserType.launch();
const page = await browser.newPage();
await page.goto(`http://${host}:${port}`);
Expand Down Expand Up @@ -85,7 +85,7 @@ const runTests = async (browserType) => {
caughtError = error;
}

mochaWebServerProcess.kill();
mochaServer.close();

// now when mocha web server is terminated, if there was an error, we can log it and exit with a failure code
if (caughtError) {
Expand Down
47 changes: 0 additions & 47 deletions test/web_server

This file was deleted.

63 changes: 63 additions & 0 deletions test/web_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var express = require('express'),
cors = require('cors');

/**
* Runs a simple web server that runs the mocha.html tests
* This is useful if you need to run the mocha tests visually
* via a tunnel to your localhost server
*
* @param playwrightTest - used to let this script know that tests are
* running in a playwright context. When truthy, a different html document is
* returned on 'GET /' and console logging is skipped.
*/

class MochaServer {
constructor(playwrightTest) {
this.playwrightTest = playwrightTest;
}

async listen() {
const server = express();
this.server = server;
server.use(function (req, res, next) {
if (!this.playwrightTest) console.log('%s %s %s', req.method, req.url, req.path);
next();
});

server.use(cors());

server.get('/', function (req, res) {
if (this.playwrightTest) {
res.redirect('/playwright.html');
} else {
res.redirect('/mocha.html');
}
});

server.use('/node_modules', express.static(__dirname + '/../node_modules'));
server.use('/test', express.static(__dirname));
server.use('/browser', express.static(__dirname + '/../src/web'));
server.use('/build', express.static(__dirname + '/../build'));
server.use(express.static(__dirname));

var port = process.env.PORT || 3000;
server.listen(port);

console.log('Mocha test server listening on http://localhost:3000/');

await new Promise((resolve) => {
process.on('SIGTERM', () => {
resolve();
});
process.on('SIGINT', () => {
resolve();
});
});
}

close() {
this.server.close();
}
}

module.exports = MochaServer;

0 comments on commit ef6ed5d

Please sign in to comment.