Running Tests using Karma test framework
Gathering coverage and test information using the SeaLights Node.js Test Listener is done in a few steps and commands, detailed below in a table comparing the relevancy of each depending on the type of tests you’re running
Step | Command | Unit Tests | Functional Tests |
---|---|---|---|
1 | Starting the Test Stage |
|
|
2 | Executing your application instrumented by Sealights |
|
|
3 | Run your Karma tests |
|
|
4.a | Upload Coverage Report |
|
|
4.b | Upload JUnit Report |
|
|
5 | End the Test Stage |
|
|
See 'Generating an Agent token' for instructions on how to generate a token
Starting the Test Stage
First, the SeaLights server needs to be notified that a test stage is starting.
npx slnodejs start --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId --teststage "Unit Tests"ests"
See 'Node.js Command Reference - Starting a test stage' for full parameter details
Running your tests
Functional Tests
Before running your functional tests, you need to set up your backend or frontend applications to send the coverage to Sealights (test footprints):
Using Node.js Agents - Running backend server using SeaLights agent
SeaLights Node.js agent - Scanning and Instrumentation of a Front-End app
Once set up, you now run your tests normally while generating one or more JUnit XML result files to be reported to the SeaLights server.
Unit Tests
In order to report the test and coverage information to SeaLights, you need to run the karma tests while producing coverage information and a JUnit results file.
For the JUnit results XML file, you can use the karma-junit-reporter reporter.
Seenpm: karma-junit-reporter for more details.
For the coverage, you need to use the karma-coverage configured to generate a
coverage-final.json
file by using the coverageReporter typejson
.Seenpm: karma-coverage for more details.
For generating these report files, update your karma.conf.js
and add or update the reporters
, coverageReporter
, and junitReporter
sections:
module.exports = function (config) {
config.set({
// your existing config...
reporters: ['progress', 'junit', 'coverage'],
coverageReporter: {
type: 'json',
dir: 'karma-reports',
subdir: '.',
file: 'coverage-final.json',
},
junitReporter: {
outputDir: 'karma-reports',
outputFile: 'junit.xml',
useBrowserName: false,
},
});
};
Uploading Test Metadata
Upload coverage report files from Unit Tests
Once done running the Unit Tests, you upload the coverage report files to the SeaLights server using the nycReport option
npx slnodejs nycReport --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId [--report ./coverage/coverage-final.json]
Upload test results report files
Once done running the tests, you upload the test results file to the SeaLights server using the uploadReports option
npx slnodejs uploadReports --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId --reportFile junit.xml
Ending the Test Stage
Finally, the server needs to be notified that a test stage has ended.
npx slnodejs end --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId
Sample - Capturing Unit Tests with Karma
Below, a sample script running the Unit Tests while instrumenting for client-side (front-end) coverage
#Install the project dependencies
npm install
# transpile the code with source maps to the dist folder
npm run build
# Install the SeaLights agents
npm i slnodejs
# Create a SeaLights ignore file to only include the src folder
echo '**/vendor*.js' > .slignore
echo '**/polyfills*.js' >> .slignore
# Scan the source files to provide SeaLights the structure of the project
npx slnodejs scan --tokenfile sltoken.txt --buildsessionidfile buildSessionId --workspacepath dist --scm git --projectRoot $(pwd)
npx slnodejs instrument --tokenfile sltoken.txt --buildsessionidfile buildSessionId --outputpath sl_dist --workspacepath dist
# Notify SeaLights the Unit Tests are starting
npx slnodejs start --tokenfile sltoken.txt --buildsessionidfile buildSessionId.txt --testStage "Unit Tests"
# Run the unit tests while ignoring the error code returned from Karma if the tests fail
# This should be with coverage and Junit reports enabled - we assume they're placed in the 'karma-reports' folder
set +e
npm run test
set -e
# Upload the coverage and tests results to SeaLights
npx slnodejs nycReport --tokenfile sltoken.txt --buildsessionidfile buildSessionId.txt --report ./karma-reports/coverage-final.json
npx slnodejs uploadReports --tokenfile sltoken.txt --buildsessionidfile buildSessionId.txt --reportFile ./karma-reports/junit.xml
# Notify SeaLights the Unit Tests have finished
npx slnodejs end --tokenfile sltoken.txt --buildsessionidfile buildSessionId.txt