From 8e74cef4a5b63ab14e47d8d698f804745c0c4ea6 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Thu, 18 Apr 2024 10:01:26 -0700
Subject: Run server on different port when running tests
---
README.md | 4 ++--
dev-server.cjs | 33 +++++++++++++++++++++------------
jest.config.cjs | 3 +++
jest.config.integ.cjs | 6 +++---
package.json | 1 +
test/google.test.js | 18 ------------------
test/integration/page.test.js | 18 ++++++++++++++++++
test/integration/setup.cjs | 20 ++++++++++++++++++++
test/integration/teardown.cjs | 5 +++++
test/setup.cjs | 28 ----------------------------
test/teardown.cjs | 5 -----
11 files changed, 73 insertions(+), 68 deletions(-)
create mode 100644 jest.config.cjs
delete mode 100644 test/google.test.js
create mode 100644 test/integration/page.test.js
create mode 100644 test/integration/setup.cjs
create mode 100644 test/integration/teardown.cjs
delete mode 100644 test/setup.cjs
delete mode 100644 test/teardown.cjs
diff --git a/README.md b/README.md
index 1437308..ad1221a 100644
--- a/README.md
+++ b/README.md
@@ -4,13 +4,13 @@
## Start the dev server
- docker run --rm --init -it -w /app -v $PWD:/app -p 8080:8080 node node dev-server.js
+ docker run --rm --init -it -v $PWD:/usr/src/app -p 8080:8080 btroops
Visit `localhost:8080` to view.
## Run a test
-You need chrome and chromedriver installed. The Dockerfile builds an image that does that. Then run the test with that image.
+You need chrome and chromedriver installed to run the integration tests. The Dockerfile builds an image that does that. Then run the test with that image.
`--network host` gives the container internet access. Necessary if tests make outside requests.
diff --git a/dev-server.cjs b/dev-server.cjs
index fc4da91..13b517a 100644
--- a/dev-server.cjs
+++ b/dev-server.cjs
@@ -1,13 +1,22 @@
-console.log('Starting server.');
+const { createServer } = require('esbuild-server');
+const server = createServer(
+ {
+ bundle: true,
+ entryPoints: ['src/index.js'],
+ },
+ {
+ static: 'public',
+ ...(process.argv.slice(2).includes('--test')) && { port: 3005 }
+ }
+);
-require('esbuild-server')
- .createServer(
- {
- bundle: true,
- entryPoints: ['src/index.js'],
- },
- {
- static: 'public'
- }
- )
- .start();
+const buildStart = Date.now();
+server
+ .start()
+ .then(() => {
+ console.log(`Build completed in ${Date.now() - buildStart}ms`);
+ })
+ .catch(() => {
+ console.error('Build failed');
+ });
+console.log(`Development server running at ${server.url}`);
diff --git a/jest.config.cjs b/jest.config.cjs
new file mode 100644
index 0000000..0a2d032
--- /dev/null
+++ b/jest.config.cjs
@@ -0,0 +1,3 @@
+module.exports = {
+ testPathIgnorePatterns: ["/node_modules/", "test/integration"]
+};
diff --git a/jest.config.integ.cjs b/jest.config.integ.cjs
index 8336242..af5adf3 100644
--- a/jest.config.integ.cjs
+++ b/jest.config.integ.cjs
@@ -15,8 +15,8 @@ console.log("Jest config file read.");
// });
module.exports = {
- globalSetup: "./test/setup.cjs",
- globalTeardown: "./test/teardown.cjs",
- testPathIgnorePatterns: ["/node_modules/"],
+ globalSetup: "./test/integration/setup.cjs",
+ globalTeardown: "./test/integration/teardown.cjs",
+ testPathIgnorePatterns: ["/node_modules/", "test/unit"],
testTimeout: 5000
};
diff --git a/package.json b/package.json
index 84c1534..c76cae8 100644
--- a/package.json
+++ b/package.json
@@ -10,6 +10,7 @@
"svg-pan-zoom": "github:webdevcat-me/svg-pan-zoom"
},
"scripts": {
+ "start": "node dev-server.cjs",
"test:integ": "jest --config jest.config.integ.cjs",
"test": "jest"
}
diff --git a/test/google.test.js b/test/google.test.js
deleted file mode 100644
index 56729bf..0000000
--- a/test/google.test.js
+++ /dev/null
@@ -1,18 +0,0 @@
-const { Builder } = require('selenium-webdriver'),
- chrome = require('selenium-webdriver/chrome.js'),
- { expect, it } = require('@jest/globals'),
- chromeOptions = new chrome.Options();
-
-let driver;
-
-chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
-
-beforeAll(async () => {
- driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
-});
-
-it('loads the page', async () => {
- await driver.get("http://localhost:8080");
-
- expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');
-});
diff --git a/test/integration/page.test.js b/test/integration/page.test.js
new file mode 100644
index 0000000..7157bff
--- /dev/null
+++ b/test/integration/page.test.js
@@ -0,0 +1,18 @@
+const { Builder } = require('selenium-webdriver'),
+ chrome = require('selenium-webdriver/chrome.js'),
+ { expect, it } = require('@jest/globals'),
+ chromeOptions = new chrome.Options();
+
+let driver;
+
+chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
+
+beforeAll(async () => {
+ driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
+});
+
+it('loads the page', async () => {
+ await driver.get("http://localhost:3005");
+
+ expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');
+});
diff --git a/test/integration/setup.cjs b/test/integration/setup.cjs
new file mode 100644
index 0000000..c476077
--- /dev/null
+++ b/test/integration/setup.cjs
@@ -0,0 +1,20 @@
+console.log("\nSpawning server process...");
+const { spawn } = require("child_process");
+
+module.exports = async function () {
+ const child = spawn("node", ["dev-server.cjs", "--test"]);
+
+ child.stdout.on('data', (data) => {
+ console.log(`${data}`);
+ });
+
+ globalThis.__INTEG_TEST_SERVER_PID__ = child.pid;
+
+ child.stderr.on("data", (data) => {
+ const str = data.toString();
+ console.log("[server]", str);
+ if (str.includes("localhost:3005")) {
+ setTimeout(resolve, 200);
+ }
+ });
+};
diff --git a/test/integration/teardown.cjs b/test/integration/teardown.cjs
new file mode 100644
index 0000000..1872061
--- /dev/null
+++ b/test/integration/teardown.cjs
@@ -0,0 +1,5 @@
+console.log('Stopping server.')
+
+module.exports = function teardown() {
+ process.kill(globalThis.__INTEG_TEST_SERVER_PID__);
+};
diff --git a/test/setup.cjs b/test/setup.cjs
deleted file mode 100644
index 01551a7..0000000
--- a/test/setup.cjs
+++ /dev/null
@@ -1,28 +0,0 @@
-console.log("\nSpawning server process...");
-const { spawn } = require("child_process");
-
-module.exports = async function () {
- const child = spawn("node", ["dev-server.cjs"]);
-
- child.stdout.on('data', (data) => {
- console.log(`${data}`);
- });
-
- // child.stderr.on('data', (data) => {
- // console.error(`stderr: ${data}`);
- // });
-
- // child.on('close', (code) => {
- // console.log(`child process exited with code ${code}`);
- // });
-
- globalThis.__INTEG_TEST_SERVER_PID__ = child.pid;
-
- child.stderr.on("data", (data) => {
- const str = data.toString();
- console.log("[server]", str);
- if (str.includes("localhost:3005")) {
- setTimeout(resolve, 200);
- }
- });
-};
diff --git a/test/teardown.cjs b/test/teardown.cjs
deleted file mode 100644
index 1872061..0000000
--- a/test/teardown.cjs
+++ /dev/null
@@ -1,5 +0,0 @@
-console.log('Stopping server.')
-
-module.exports = function teardown() {
- process.kill(globalThis.__INTEG_TEST_SERVER_PID__);
-};
--
cgit v1.2.3