Skip to content

Commit

Permalink
Test: Add integration test with karma-qunit
Browse files Browse the repository at this point in the history
This avoids regressing any APIs that karma-qunit uses to detect
or report failure details, prior to merging or releasing any
changes.
  • Loading branch information
Krinkle committed Apr 18, 2022
1 parent 4f7738c commit ca1cd51
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/integration/karma-qunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const cp = require('child_process');
const path = require('path');
const DIR = path.join(__dirname, 'karma-qunit');

function normalize (str) {
return str
.replace(/^.*:INFO/gm, 'INFO')
.replace(/(Karma|Firefox) \S+/g, '$1')
.replace(/(Firefox) \([^)]*\)/g, '$1')
.replace(/(started at|on socket|SUCCESS) .*$/gm, '$1')
.replace(/^\s+/gm, ' ');
}

QUnit.module('karma-qunit', {
before: () => {
// Need --legacy-peer-deps under npm 7 for "file:" override in package.json.
// Once CI and dev environments are on npm 8, consider using native "overrides".
cp.execSync('npm install --prefer-offline --no-audit --no-dev --legacy-peer-deps', { cwd: DIR, encoding: 'utf8' });
}
});

QUnit.test('passing test', assert => {
const expected = `
INFO [karma-server]: Karma server started at
INFO [launcher]: Launching browsers FirefoxHeadless with concurrency unlimited
INFO [launcher]: Starting browser FirefoxHeadless
INFO [Firefox]: Connected on socket
...
Firefox: Executed 3 of 3 SUCCESS
`.trim();
const actual = normalize(
cp.execSync('npm test', { cwd: DIR, env: { PATH: process.env.PATH }, encoding: 'utf8' })
);
assert.pushResult({ result: actual.includes(expected), actual, expected });
});

QUnit.test.each('failing test', {
assert: ['fail-assert.js', `
Firefox example FAILED
some message
Expected: true
Actual: false
@fail-assert.js:2:10`
],
'global-error': ['fail-global-error.js', `
Firefox example FAILED
Died on test #1: boom is not defined
@fail-global-error.js:1:7`
]
}, (assert, [file, expected]) => {
try {
const ret = cp.execSync('npm test', {
cwd: DIR,
env: {
PATH: process.env.PATH,
KARMA_FILES: file
},
encoding: 'utf8'
});
assert.equal(ret, null);
} catch (e) {
const actual = normalize(e.stdout);
assert.pushResult({ result: actual.includes(expected), actual, expected });
assert.true(e.status > 0, 'non-zero exit code');
}
});
3 changes: 3 additions & 0 deletions test/integration/karma-qunit/fail-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
QUnit.test('example', function (assert) {
assert.true(false, 'some message');
});
5 changes: 5 additions & 0 deletions test/integration/karma-qunit/fail-global-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
QUnit.test('example', function (assert) {
// eslint-disable-next-line no-undef
boom();
assert.true(true);
});
16 changes: 16 additions & 0 deletions test/integration/karma-qunit/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env node */

module.exports = function (config) {
config.set({
browsers: [
'FirefoxHeadless'
],
frameworks: ['qunit'],
files: [
process.env.KARMA_FILES || 'pass-*.js'
],
autoWatch: false,
singleRun: true,
reporters: ['dots']
});
};
13 changes: 13 additions & 0 deletions test/integration/karma-qunit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"dependencies": {
"karma": "^6.3.18",
"karma-chrome-launcher": "^3.1.1",
"karma-firefox-launcher": "^2.1.2",
"karma-qunit": "^4.1.2",
"qunit": "file:../../.."
},
"scripts": {
"test": "karma start --no-colors"
}
}
9 changes: 9 additions & 0 deletions test/integration/karma-qunit/pass-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
QUnit.test('example 1', function (assert) {
assert.true(true, 'x');
});
QUnit.test('example 2', function (assert) {
assert.true(true, 'y');
});
QUnit.test('example 3', function (assert) {
assert.true(true, 'z');
});

0 comments on commit ca1cd51

Please sign in to comment.