2.0.0 • Published 12 months ago

@oat-sa/tao-qunit-testrunner v2.0.0

Weekly downloads
29
License
GPL-2.0
Repository
github
Last release
12 months ago

tao-qunit-testrunner-fe

TAO Frontend Unit & Integration Test Runner based on QUnit and Puppeteer

How to use

Run all tests:

npx qunit-testrunner

Run specific test:

npx qunit-testrunner <testname>

Enable coverage:

npx qunit-testrunner --cov

Display coverage report

Requirement: npm install nyc

npx nyc report

Generate html coverage report

Requirement: npm install nyc

npx nyc report --reporter=lcov

Environment variables

Option nameDescriptionTypeDefault
PARALLEL_TESTSDefine the amount of parallel testsnumber5
REDIRECT_CONSOLERedirects browser console to terminalbooleanfalse

Command line arguments

Option nameDescriptionTypeDefault
--config, -cDefine config filestring.qunit-testrunner.config.json
--test-dir, -dTest directory name in rootstringtest
--root, -rRoot directory namestringprocess.cwd()
--specTest files patternglob**/test.html
--mock-apiEnables API Mock middlewarebooleanfalse
--without-serverStart testrunner without serverbooleanfalse
--reporterTest result reporter (json, basic, stats
or custom path)stringbasic
--verbose, -v, -vv ...Verbose levelcount0
--help -hDisplay helpbooleanfalse
Webserver options
--keepaliveDo not run tests just start webserverbooleanfalse
--hostWebserver hoststring127.0.0.1
--port, -pWebserver portnumberrandom free port
Coverage options
--coverage, --covEnable coverage measurementbooleanfalse
--coverage-instrument-specCoverage instrument pattern from rootglobsrc/**/*.js
--coverage-output-dirCoverage output directory in rootstring.nyc_output
--clear-coverage-dirClear the coverage output directory before running
test, when coverage option is enabledbooltrue

Config file

In the config file, all command line argument can be defined. If an argument is defined in config and in command line as well, the command line value overrides config value.

.qunit-testrunner.config.json:

{
  "verbose": 1,
  "testDir": "exampleTest",
  "coverage": true,
  "coverage-instrument-spec": "exampleSrc/**/*.js",
  "port": 8082
}

Recommended package.json scripts:

package.json:

"scripts": {
  "test": "npx qunit-testrunner",
  "test:keepAlive": "npx qunit-testrunner --keepalive",
  "test:cov": "npx qunit-testrunner --cov",
  "coverage": "nyc report",
  "coverage:html": "nyc report --reporter=lcov && open-cli coverage/lcov-report/index.html"
}

Coverage measurement description

  1. Source files are instrumented when they are requested. --coverage-instrument-spec defines the pattern.
  2. QUnit.done event listener is injected into test .html files. Injector injects into every files based on --spec pattern and puts event handler after <head>
  3. Handler post __coverage__ result back to /__coverage__/[original path], like /test/foo/bar.html -> /__coverage__/foo/bar.html.
  4. istanbulCoverage middleware collects coverage results and saves them into the directory provided by --coverage-output-dir. The file name is an md5 hash generated from coverage url.
  5. nyc can generate coverage report based on coverage measurement. nyc report or nyc report --reporter=lcov

Small hack that generates empty coverage objects, so report will show totally uncovered files as well: npx nyc --instrument --include <src glob> --all --silent echo