From d2c218f72d1ad5bd65407433a51688b0a9bd0d89 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Thu, 2 May 2024 09:40:05 -0700
Subject: Use NODE_ENV to test for test environment; improve scripts
---
dev-server.cjs | 27 ---------------------------
jest.config.integ.cjs | 14 --------------
npm | 12 +++++++++++-
package.json | 2 +-
server.cjs | 30 ++++++++++++++++++++++++++++++
test/integration/setup.cjs | 2 +-
6 files changed, 43 insertions(+), 44 deletions(-)
delete mode 100644 dev-server.cjs
create mode 100644 server.cjs
diff --git a/dev-server.cjs b/dev-server.cjs
deleted file mode 100644
index 2e7c2ec..0000000
--- a/dev-server.cjs
+++ /dev/null
@@ -1,27 +0,0 @@
-const { createServer } = require('esbuild-server');
-const server = createServer(
- {
- bundle: true,
- entryPoints: ['src/index.js', 'src/map.js'],
- outdir: 'build'
- },
- {
- static: 'public',
- ...(process.argv.slice(2).includes('--test')) && {
- port: 3005,
- injectLiveReload: false,
- watch: false
- }
- }
-);
-
-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.integ.cjs b/jest.config.integ.cjs
index af5adf3..1b7b6a9 100644
--- a/jest.config.integ.cjs
+++ b/jest.config.integ.cjs
@@ -1,19 +1,5 @@
console.log("Jest config file read.");
-// const { spawn } = require("child_process");
-
-// const ls = spawn('ls', ['-lh', './test']);
-
-// ls.stdout.on('data', (data) => { console.log(`stdout: ${data}`); });
-
-// ls.stderr.on('data', (data) => {
-// console.error(`stderr: ${data}`);
-// });
-
-// ls.on('close', (code) => {
-// console.log(`child process exited with code ${code}`);
-// });
-
module.exports = {
globalSetup: "./test/integration/setup.cjs",
globalTeardown: "./test/integration/teardown.cjs",
diff --git a/npm b/npm
index 1d3b623..4ae615a 100755
--- a/npm
+++ b/npm
@@ -1,3 +1,13 @@
#!/usr/bin/env bash
-docker run --rm --init -it -v $PWD:/usr/src/app -u $(id -u):$(id -u) -p 8080:8080 btroops npm $@
+user_id=$(id -u)
+image=btroops
+
+if [[ $1 == run && $2 == test* ]]
+then
+ port=3005
+else
+ port=8080
+fi
+
+docker run --rm --init -it -v $PWD:/usr/src/app -u $user_id:$user_id -p $port:$port $image npm $@
diff --git a/package.json b/package.json
index 1e0f59f..97e55ee 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
"selenium-webdriver": "^4.19.0"
},
"scripts": {
- "start": "node dev-server.cjs",
+ "start": "node server.cjs",
"test:integ": "node --trace-warnings ./node_modules/.bin/jest --detectOpenHandles --config jest.config.integ.cjs",
"test:integ:debug": "NODE_INSPECT_RESUME_ON_START=1 node inspect ./node_modules/jest/bin/jest.js --config jest.config.integ.cjs --runInBand test/integration/page.test.js",
"test": "jest"
diff --git a/server.cjs b/server.cjs
new file mode 100644
index 0000000..b5bd00f
--- /dev/null
+++ b/server.cjs
@@ -0,0 +1,30 @@
+const { createServer } = require('esbuild-server');
+
+const server = createServer(
+ {
+ bundle: true,
+ entryPoints: ['src/index.js', 'src/map.js'],
+ ...(process.env.NODE_ENV !== 'test') && {
+ outdir: 'build'
+ }
+ },
+ {
+ static: 'public',
+ ...(process.env.NODE_ENV === 'test') && {
+ port: 3005,
+ injectLiveReload: false,
+ watch: false
+ }
+ }
+);
+
+const buildStart = Date.now();
+server
+ .start()
+ .then(() => {
+ console.log(`Build completed in ${Date.now() - buildStart}ms`);
+ })
+ .catch(() => {
+ console.error('Build failed');
+ });
+console.log(`${process.env.NODE_ENV === 'test' ? 'Test' : 'Development'} server running at ${server.url}`);
diff --git a/test/integration/setup.cjs b/test/integration/setup.cjs
index c476077..a03d7de 100644
--- a/test/integration/setup.cjs
+++ b/test/integration/setup.cjs
@@ -2,7 +2,7 @@ console.log("\nSpawning server process...");
const { spawn } = require("child_process");
module.exports = async function () {
- const child = spawn("node", ["dev-server.cjs", "--test"]);
+ const child = spawn("node", ["server.cjs"]);
child.stdout.on('data', (data) => {
console.log(`${data}`);
--
cgit v1.2.3