mocha : how to run multiple JS test files under separate process environment - environment-variables

I am new to Mocha so this might probably be a trivial question but couldn't yet find an answer:
I have a simple NodeJS project with the below package.json
{
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "davide talesco",
"license": "ISC",
"devDependencies": {
"chai": "^4.0.2",
"mocha": "^3.4.2"
}
}
and the following 2 tests files under test folder:
test1.js
process.env.NODE_ENV = 'test';
var chai = require('chai');
var should = chai.should();
describe('Test setProp', function(){
it('env variable should be test', function(done){
process.env.NODE_ENV.should.be.equal('test');
return done();
});
});
test2.js
process.env.NODE_ENV = 'prod';
var chai = require('chai');
var should = chai.should();
describe('Test setProp', function(){
it('env variable should be prod', function(done){
process.env.NODE_ENV.should.be.equal('prod');
return done();
});
});
when I run npm test the first test complete succesfully whilst the second fails as per below
ie-macp-davidt:crap davide_talesco$ npm test
> pc-lib#1.0.0 test /Users/davide_talesco/development/crap
> mocha
Test setProp
1) env variable should be test
Test setProp
✓ env variable should be prod
1 passing (16ms)
1 failing
1) Test setProp env variable should be test:
AssertionError: expected 'prod' to equal 'test'
+ expected - actual
-prod
+test
at Context.<anonymous> (test/test1.js:11:36)
npm ERR! Test failed. See above for more details.
its pretty clear that the tests are running under the same process...
my question is : how can I make them run under completely separate processes so each one can set its own environment?
Thanks,
Davide

One of the most simple ways is to use Unix find command:
find ./test -name '*.js' -exec mocha \{} \;
I'd recommend to use local mocha binaries to avoid troubles in case it isn't installed globally:
find ./test -name '*.js' -exec ./node_modules/.bin/mocha \{} \;
If you want to add that to package.json, please note that backslashes should be escaped:
...
"scripts": {
"test": "find ./test -name '*.js' -exec ./node_modules/.bin/mocha \\{} \\;"
},
...

If you want to abort testing as soon as a test file fails, you can do it like this:
find ./test -type f -name "*.js" -exec sh -c 'for n; do ./node_modules/.bin/mocha "$n" || exit 1; done' sh {} +

Alternatively you can use mocha-parallel-tests.
To install:
https://www.npmjs.com/package/mocha-parallel-tests
To use:
"scripts": {
"test": "mocha-parallel-tests"
},
Nice thing is that it is a proper mocha runner so you can configure reports and pass standard mocha configuration like bail.

Related

Playwright - Test against different environments and different variables

Im looking to use Playwright to test against a web page.
The system im working on has 4 different environments that we need to deploy against,
for example the test urls may be
www.test1.com
www.test2.com
www.test3.com
www.test4.com
The first question is how do I target the different Environment? In my playwright config I had a baseUrl but I need to override that.
In addition each environment has different login credentials, how can I create and override these as parameters per environment?
Since Playwright v1.13.0, there is a baseURL option available. You can utilise that in this way probably
In your config.js file, you can have this
import { PlaywrightTestConfig } from '#playwright/test';
const config: PlaywrightTestConfig = {
use: {
baseURL: process.env.URL,
},
};
export default config;
Now in the package.json file, you can have the environment variables set in the test commands for various env in the scripts , like this
...
"scripts": {
"start": "node app.js",
"test1": "URL=www.test1.com mocha --reporter spec",
"test2": "URL=www.test2.com mocha --reporter spec",
.
.
},
...
Similarly you can set the environment variables for the login credentials also and then pass them in the script in the same way the URL is passed.
Another approach to this is to use a Bash script. I use something like the following to run tests across environments, to ensure that my Playwright tests will work in all environments they're run in -
#!/bin/bash
echo "Running tests against env 1";
ENV_URL=https://www.env1.com SOMESERVICE_ENV_URL=http://www.env1.com/scholarship npx playwright test $1;
echo "Running tests against env 2"
ENV_URL=https://env2.com SOMESERVICE_ENV_URL=http://env2.com:4008 npx playwright test $1;
echo "Running tests against env 3";
ENV_URL=http://localhost:3000 SOMESERVICE_ENV_URL=http://localhost:4008 npx playwright test $1;
And then run with ./myScript.sh myTest.test.ts
(In a Bash script, the first argument passed in is available via $1.)

Next.js: how to use different env file for different environment?

Below is the code from package.json file
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
},
Below is my next.config.js file, here console.log always undefined
require("dotenv").config();
console.log(process.env.BASE_URL)
module.exports = {
env: {
API_BASE_URL: process.env.BASE_URL
},
reactStrictMode: true,
}
And this is in the .env.development
BASE_URL: 'http://localhost:3000'
When I ran the npm run dev command,
it prints on terminal "Loaded env from /var/www/html/next/next-SC/.env.development"
So, why the console prints undefined always.
I'm using next js version "10.0.4"
I assume you are using React with nextjs. If not, then please disregard this answer. I am doing the same thing. React has built in support for env vars. All you need to do is to prefix REACT_APP to your environment vars. So, in your .env.development or .env.staging, etc., you can have REACT_APP_BASE_URL=https://blah.com. You can then access them in your app using process.env.REACT_APP_BASE_URL. Then to build based on environment, I have (I am using craco, you would just use your normal build command)
"build:nightly": "env-cmd -f .env.nightly craco build",
"build:nightly": "env-cmd -f .env.staging craco build",
"build:nightly": "env-cmd -f .env.beta craco build",
...
For development environment, name the file .env.development, for production .env.production.
Next.js has built-in loader for environment variables. So dotenv or similar packages aren't needed. Just add the files. It will be loaded automatically (see documentation).

nyc and mocha code coverage failing when running in jenkins

I'm trying to generate the code coverage report for our builds in Jenkins -
Locally when I execute the command
npm run coverage
"scripts": {
"test": "cross-env mocha test -t 5000 --recursive --exit --reporter mochawesome",
"coverage":"nyc --reporter=html --reporter=text ./node_modules/.bin/mocha test -t 5000 --recursive --exit"
},
it will run the tests and then give me the code coverage results
Now when the same code runs in jenkins I get this error
/var/lib/jenkins/jobs/api-ci-develop/workspace/back-end/node_modules/path-to-regexp/index.js:63
path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
^
TypeError: Cannot read property 'length' of undefined
at pathtoRegexp (/var/lib/jenkins/jobs/api-ci-develop/workspace/back-end/node_modules/path-to-regexp/index.js:63:49)
at new Layer (/var/lib/jenkins/jobs/api-ci-develop/workspace/back-end/node_modules/express/lib/router/layer.js:45:17)
apparently is doing something with the node_module folder and I'm not sure how to avoid that. I exclude the folder and files in the nyc config
"nyc": {
"branches": 5,
"lines": 25,
"functions": 13,
"statements": 25,
"check-coverage": true,
"exclude": [
"**/services/plaidService.js",
"**/node_modules/**",
"**/node_modules/plaid/lib/PlaidClient.js"
]
},
Any ideas what should I do to avoid the node_modules folder to be checked?

Can I use npm custom flags to set the value of a configuration variable usable in webpack.config?

I'd like to achieve something like this:
npm run build --path custom
in order to set a different output.path that will be used in webpack.config.js
Does something like this exists?
I managed to achieve something similar to my goal like using a config field like this:
"name": "foo",
"config": {
"dist": "bar"
},
"scripts": {
"build": "webpack --watch",
"custom": "node test.js"
}
...
I managed to modify the value of dist ("bar") with:
npm config set foo:dist apple
And I can access the value of dist with:
process.env.npm_package_config_dist
Probably not ideal, but it works. I'm open to better suggestions.
The solution used in the end uses this command:
DIST=[YOUR-DIRECTORY] npm run build
The webpack.config handles the value of DIST like:
const target = process.env.DIST
? process.env.DIST
: 'dist';
and in the output we go with:
path: path.resolve(__dirname, target)
You may try this:
"name": "npm-help",
"scripts": {
"build": **"sass --watch"**,
}

nyc coveralls integration not working

I try to make nyc working with coveralls following the instruction:
https://github.com/istanbuljs/nyc#integrating-with-coveralls
But I can't get it to work. Here is an example repo:
https://github.com/unional/showdown-highlightjs-extension
Travis build is successful: https://travis-ci.org/unional/showdown-highlightjs-extension
And Coveralls notice the build, but does not seem to get any data:
https://coveralls.io/github/unional/showdown-highlightjs-extension
Here is my .travis.yml:
language: node_js
notifications:
email:
on_success: never
on_failure: change
node_js:
- "stable"
before_install:
- npm install -g npm
script:
- npm run verify
after_script:
- npm install coveralls && npm run coveralls
And here is my package.json:
{
...
"scripts": {
"coverage": "npm test && nyc check-coverage --branches 85 --functions 85 --lines 85",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "npm run clean && tsc && nyc ava"
...
},
"nyc": {
"exclude": [
"scripts",
"**/*.spec.*",
"**/fixtures/**/*"
]
},
...
}
Try adding your Coveralls repo API token (which can be found on the Coveralls page for your repo) to a new COVERALLS_REPO_TOKEN encrypted environment variable on Travis, as per the (somewhat sketchy) documentation on the Coveralls site.
I found out the issue is in my tsconfig.json:
{
"compilerOptions": {
"sourceRoot": "/showdown-highlight-extension"
...
}
}
This setting gives me the correct (I assume) source map in the browser. See What's the proper way to set sourceRoot in typescript?
But is not liked by the coverage tool.
Once I remove it, it starts working.
Need to find an answer to that question.

Resources