In the package's package.json
, I have two scripts. One starts the server for testing, and one starts the tests:
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --no-inline",
"test": "jest"
}
Note that start
is a background script that starts a server then stays running in the terminal until it is manually interrupted (Ctrl + C). So the npm run test
script must be executed in a separate terminal.
However, the server can also be started from another server package (using lerna in the context of a monorepo), as offered by that package's package.json
:
"scripts": {
"debug": "DEBUG=* ts-node src/"
}
Likewise, this is also background script that starts a server then stays running in the terminal until it is manually interrupted (Ctrl + C).
I want my test files to know how the server was started when the script npm run test
is executed. This could be done by setting or altering an env variable in the start script, that is later inspected in the test files. If that env variable is set then the test will know that its server was started by a user running npm run start
in that package before hand. If not, then the tests will know that the server was started by a user doing npm run debug
in the other package. It would then be able to run some conditional code, such as:
if (process.env.server_from_start) {
...
} else {
...
}
I have gotten as far as sharing an env variable from within a script, by doing:
"scripts": {
"script1": "export FOO=bar && bash script_that_echos_FOO.sh",
"script2": "bash script_that_echos_FOO.sh"
}
FOO is echoed correctly when I run npm run script1
, but nothing is echoed when I run npm run script2
.