diff --git a/Dockerfile b/Dockerfile index 2630e00..b664290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM node:14.16.1-alpine LABEL maintainer="madnificent@gmail.com" -RUN apk update && apk upgrade && apk add --no-cache bash git openssh +RUN apk update && apk upgrade && apk add --no-cache bash git jq openssh ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql' ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application' diff --git a/README.md b/README.md index 9900bdc..2eb2ecb 100644 --- a/README.md +++ b/README.md @@ -177,3 +177,16 @@ When running inside a mu.semte.ch stack, you could mount your sources and connec - /absolute/path/to/your/sources/:/app/ ``` Now open Chromium, and visit [chrome://inspect/](chrome://inspect/). Once the service is launched, a remote target on localhost should pop up. + +## Running tests [BETA] +The javascript template provides [mocha](https://mochajs.org/#getting-started) as a testing framework. Create a `test` folder in your application root and nest any tests under that folder. +You can run tests by setting the `NODE_ENV` to "test": + +```bash +docker run + --rm + -v `pwd`:/app \ + -e NODE_ENV=test \ + --name my-js-test \ + semtech/mu-javascript-template +``` diff --git a/boot.sh b/boot.sh index 244263d..091f850 100755 --- a/boot.sh +++ b/boot.sh @@ -3,8 +3,11 @@ then # Run live-reload development exec /usr/src/app/node_modules/.bin/nodemon \ --watch /app \ - --ext js,mjs,cjs,json \ + --ext js,mjs,cjs,json,hbs \ --exec /usr/src/app/run-development.sh +elif [ "$NODE_ENV" == "test" ] +then + exec /usr/src/app/run-test.sh elif [ "$NODE_ENV" == "production" ] then diff -rq /app /app.original > /dev/null diff --git a/package.json b/package.json index d9ea937..dd76821 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "type": "git", "url": "git+https://github.com/mu-semtech/mu-javascript-template.git" }, + "scripts": { + "test": "mocha --require @babel/register --exit" + }, "keywords": [ "mu-semtech" ], @@ -16,11 +19,13 @@ "@babel/plugin-proposal-class-properties": "^7.13.0", "@babel/plugin-proposal-decorators": "^7.13.15", "@babel/preset-env": "^7.14.1", + "@babel/register": "^7.13.16", "babel-plugin-module-resolver": "4.1.0", "body-parser": "~1.19.0", "env-var": "^7.0.0", "express": "^4.17.1", "express-http-context": "~1.2.4", + "mocha": "^8.4.0", "nodemon": "^2.0.7", "sparql-client-2": "https://github.com/erikap/node-sparql-client.git", "uuid": "^8.3.2" diff --git a/run-test.sh b/run-test.sh new file mode 100755 index 0000000..a22276b --- /dev/null +++ b/run-test.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# create a copy of app, so we can modify as necessary +cp -a /app /usr/src/output + +# add some required files from the template +cp -a /usr/src/app/.babelrc.js /usr/src/app/helpers /usr/src/output/ +# merge package.json files, prefering whatever is specified in the app +jq -s '.[0] * .[1]' /usr/src/app/package.json /app/package.json > /usr/src/output/package.json + +# do things +cd /usr/src/output +npm install +exec npm test