v1.1.0 and the JUnit Reporter
You may have noticed that a few days ago, MiniSpec v1.1.0 has been released.
The main addition of that version is related to the JUnit Reporter: it adds a time
attribute to testcase
nodes which is filled with the execution duration of the test.
That is a good opportunity to introduce the JUnit Reporter!
Introduction to the JUnit Reporter
JUnit is a well-knwon testing framework for Java. Its XML reporting format is very popular and implemented in a lot of tools. It is a perfect candidate for a MiniSpec reporter which could be used to share reports with other tools.
For example, MiniSpec itself is using the JUnit Reporter with Gitlab CI. To do
so, in spec/minispec_entrypoint.ts
,
the reporter is instanciated with a stream to a file:
// minispec_entrypoint.ts
import MiniSpec from 'minispec'
import { SummaryReporter, ConsoleReporter, JunitReporter } from 'minispec/reporters'
import fs from 'fs'
// snip... other imports here
// Several reporters will be required: lets use an array
let reporters: BaseReporter[] = []
// Development environment do not need a junit report
// It is generated only when the execution is part of a CI
if (process.env['CI']) {
// Creation of a write stream using node-js built-in `fs` API
const junitReport = fs.createWriteStream('./junitReport.xml')
// Instanciation of a new JUnit Reporter using the previously created stream
reporters.push(new JunitReporter(junitReport))
// A summary of the execution will still be reported to the logs of the CI
reporters.push(new SummaryReporter())
} else {
// On other environment, like development, other reporters can be used instead
reporters.push(new ConsoleReporter())
}
MiniSpec
.getInstance()
.setReporters(reporters) // This is where the magic happens :D
.execute()
.gitlab-ci.yml
needs a small addition too:
test:
# snip...
script:
- npm install-test
artifacts:
when: always
paths:
- junitReport.xml
# other artifacts
reports:
junit:
- junitReport.xml
# other reports
And voilĂ ! Gitlab CI is now aware of our tests and can display a report in the UI. And thanks to the addition in v1.1.0, it shows the duration of each test, and even sorts them by duration in reverse order!
If you look at the MiniSpec repository on Gitlab, you will notice that there are actually two dinstinct JUnit reports: one for the unit tests, and another one for the acceptance tests. Gitlab automatically merges the reports in one single view \o/