summaryrefslogtreecommitdiffstats
path: root/pkg/lib/qunit-tests.js
blob: 6f6e69d3c24ab71d1b66fcc7d013d87af07bf08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

"use strict";

import QUnit from "qunit/qunit/qunit.js";
import qunitTap from "qunit-tap/lib/qunit-tap.js";
import "qunit/qunit/qunit.css";

QUnit.mock_info = async key => {
    const response = await fetch(`http://${window.location.hostname}:${window.location.port}/mock/info`);
    return (await response.json())[key];
};

// Convenience for skipping tests that the python bridge can't yet
// handle.

let is_pybridge = null;

QUnit.test.skipWithPybridge = async (name, callback) => {
    if (is_pybridge === null)
        is_pybridge = await QUnit.mock_info("pybridge");

    if (is_pybridge)
        QUnit.test.skip(name, callback);
    else
        QUnit.test(name, callback);
};

/* Always use explicit start */
QUnit.config.autostart = false;

let qunit_started = false;

QUnit.moduleStart(() => {
    qunit_started = true;
});

window.setTimeout(() => {
    if (!qunit_started) {
        console.log("QUnit not started by test");
        console.log("cockpittest-tap-error");
    }
}, 20000);

/* QUnit-Tap writes the summary line right after this function returns.
* Delay printing the end marker until after that summary is out.
*/
QUnit.done(() => window.setTimeout(() => console.log("cockpittest-tap-done"), 0));

/* Now initialize qunit-tap
 *
 * When not running under a tap driver this stuff will just show up in
 * the console. We print out a special canary at the end of the tests
 * so that the tap driver can know when the testing is done.
 *
 * In addition double check for a test file that doesn't properly call
 * QUnit.start() after its done setting up its tests.
 *
 * We also want to insert the current test name into all tap lines.
 */
const tap_regex = /^((not )?ok [0-9]+ (- )?)(.*)$/;
qunitTap(QUnit, function() {
    if (arguments.length == 1 && QUnit.config.current) {
        const match = tap_regex.exec(arguments[0]);
        if (match) {
            console.log(match[1] + QUnit.config.current.testName + ": " + match[4]);
            return;
        }
    }
    console.log.apply(console, arguments);
});

export default QUnit;