NodeJS Auto Testing in Container
Option #1: Run Headless Chrome
Since Chrome 59, it is possible to run it without the actual browser window. That feature is called Headless Chrome.
- install chrome Headless. https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/
#Prerequisites
sudo apt-get update
sudo apt-get install -y unzip xvfb libxi6 libgconf-2-4
sudo apt-get install default-jdk
#Install Google Chrome
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable
#Install ChromeDriver
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
#if you want to run Remote Selenium WebDrivers
wget https://selenium-release.storage.googleapis.com/3.13/selenium-server-standalone-3.13.0.jar
wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip
unzip testng-6.8.7.jar.zip
#Run Chrome via Selenium Server
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar
# or Start Headless ChromeDriver
chromedriver --url-base=/wd/hub
- add Package Reference
npm install karma-cli --save-dev
npm install mocha --save-dev
npm install chai --save-dev
npm install requirejs --save-dev
npm install karma --save-dev
npm install karma-mocha --save-dev
npm install karma-chai --save-dev
npm install karma-requirejs --save-dev
npm install karma-chrome-launcher --save-dev
npm install karma-coverage --save-dev
- Unit Test
add below config files in the root directory of source
Config Karma.conf.js, add below content in config.set
module.exports = function (config) {
config.set({
basePath: '',//the path for you project source. relative path should from path of karma.conf.js. e.g you karma.conf.js in root/abc/123, your source in root/xyz/src. the base path should be ../../ayz/src
frameworks: ['mocha', 'requirejs', 'chai'],
files: [
{ pattern: './public/execution-plan/**/*.js', included: false },//the files you want to test
{ pattern: './tests/main.js', included: true },//load anonymous js file
{ pattern: './tests/**/*.js', included: false }//test files
],
exclude: [],
//allow you to do some work with your files before they get served to the browser.http://karma-runner.github.io/4.0/config/preprocessors.html
preprocessors: {
'./public/**/*.js': ['coverage'],
'tests/**/*.js': ['coverage']
},
junitReporter: {
outputFile: 'TESTS-xunit.xml',
outputDir: 'reports/junit'
},
coverageReporter: {
type: 'lcov',
dir: 'reports',
subdir : 'coverage',
includeAllSources: true
},
reporters: ['progress', 'junit','coverage'],
browsers:['HeadlessChrome'],
customLaunchers:{
HeadlessChrome:{
base:"ChromeHeadless",
flags:[
"--no-sandbox",
// required to run without privileges in Docker
"--disable-web-security",
"--disable-gpu",
"--remote-debugging-port=9222"
]
}
},
port: 9876,
colors: false,
logLevel: config.LOG_INFO,
autoWatch: false,
singleRun: true,
concurrency: Infinity,
})
}
main.js
var tests = [];
for (var file in window.__karma__.files) {
if (/\.test\.js$/.test(file)) { tests.push(file); }
}
requirejs.config({
baseUrl: '/base/public/', //the path you want to test, base if define in karma.conf.js
deps: tests,//the tests folder
callback: mocha.run
});
Add RequireJs config file
requirejs.config({
baseUrl: 'public',
nodeRequire: require,
paths: {
ready: '../node_modules/domready/ready.min',
selfish: '../node_modules/selfish/selfish',
},
});
add Test command if pacakge.json
"scripts": {
"test": "karma --singleRun true --browsers HeadlessChrome start "
}
--watch=false
Specifies that we only want the tests to run once and then exit instead of watching for changes.
--browsers=HeadlessChrome
Specifies that we want to use Headless Chrome as the browser for the tests.
- create tests folder and add your Unit test
define(["../public/execution-plan/math-extensions.js"],function(math){
"use strict";
describe("", function(){
describe('math.divideByAvoidNaN() Test', () => {
it('should equal 0', () => {
const result = math.divideByAvoidNaN(1, 0);
expect(result).to.equal(0);
});
});
});
Do SonarQube Scan:
sonar-scanner -D sonar.host.url=[Sonarqube Host url] -D sonar.projectKey=[ProjectKey] -D sonar.login=[loginToken] -D sonar.language=js -D sonar.sourceEncoding=UTF-8 -D sonar.sources=[project src path] -D sonar.exclusions=/node_modules/ -D sonar.tests=[root path of UT] -D sonar.javascript.jstests.reportsPath=reports -D sonar.javascript.lcov.reportPaths=reports/coverage/lcov.info -D sonar.projectVersion=[project version]
Remark:
When I run the sonarqube scanning in different Linux kernel, got “Fail to extract report AXIBsbuecjS-BrcNhW3M from database, invalid entry size (expected 1024 but got 4 bytes)”
Kernel: Linux 3.10.0-1062.12.1.el7.x86_64 amd64, no error
kernel: Linux 4.15.0-1075-azure amd64, fail rate is higher than 90%.
based on the test result, I try to remove jstests report:
in the orginal sonarqube project
Kernel: Linux 3.10.0-1062.12.1.el7.x86_64 amd64, no error
kernel: Linux 4.15.0-1075-azure amd64, fail rate is higher than 90%.
create a new sonarqube project:
Kernel: Linux 3.10.0-1062.12.1.el7.x86_64 amd64, no error
kernel: Linux 4.15.0-1075-azure amd64, no sonarqube error
go back to the orginal sonarqube project:
turn off project option,【ignore header comments】
kernel: Linux 4.15.0-1075-azure amd64, no sonarqube error. try 6 time, all passed.
Summary:
currently , we just turn off the project option. for final solution ,no conclusion , find the root cause, still need more investigate, check what is the relation ship between the sonar qube project header comments and Linux kernel. for each case, if we use same kernel version without change the project option and change the project option with different kernel version in future
some reference links:
https://medium.com/serverlessguru/how-to-unit-test-with-nodejs-76967019ba56
https://blog.coding.net/blog/mocha-test-configuration
https://medium.com/@BoweiHan/setup-for-testing-amd-modules-with-karma-requirejs-and-mocha-2be3931c6a72
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/