Debian Codegraph @

pkg/lib/qunit-tests.ts

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 "use strict";
7
8 import QUnit from "qunit";
9 import qunitTap from "qunit-tap";
10 import "qunit/qunit/qunit.css";
11
12 export const mock_info = async (key: string) => {
13 const response = await fetch(`http://${window.location.hostname}:${window.location.port}/mock/info`);
14 return (await response.json())[key];
15 };
16
17 // Convenience for skipping tests that our python bridge can't yet
18 // handle (the C bridge implemented these features)
19
20 export const skipWithPybridge = async (name: string, callback: (assert: unknown) => void | Promise<void>) => {
21 QUnit.skip(name, callback);
22 };
23
24 /* Always use explicit start */
25 QUnit.config.autostart = false;
26
27 let qunit_started = false;
28
29 QUnit.moduleStart(() => {
30 qunit_started = true;
31 });
32
33 window.setTimeout(() => {
34 if (!qunit_started) {
35 console.log("QUnit not started by test");
36 console.log("cockpittest-tap-error");
37 }
38 }, 20000);
39
40 /* QUnit-Tap writes the summary line right after this function returns.
41 * Delay printing the end marker until after that summary is out.
42 */
43 QUnit.done(() => { window.setTimeout(() => console.log("cockpittest-tap-done"), 0) });
44
45 /* Now initialize qunit-tap
46 *
47 * When not running under a tap driver this stuff will just show up in
48 * the console. We print out a special canary at the end of the tests
49 * so that the tap driver can know when the testing is done.
50 *
51 * In addition double check for a test file that doesn't properly call
52 * QUnit.start() after its done setting up its tests.
53 *
54 * We also want to insert the current test name into all tap lines.
55 */
56 const tap_regex = /^((not )?ok [0-9]+ (- )?)(.*)$/;
57 qunitTap(QUnit, function(message: string, ...args: unknown[]) {
58 if (args.length == 0 && QUnit.config.current) {
59 const match = tap_regex.exec(message);
60 if (match) {
61 console.log(match[1] + QUnit.config.current.testName + ": " + match[4]);
62 return;
63 }
64 }
65 console.log(message, args);
66 });
67
68 export function f(format: TemplateStringsArray, ...args: unknown[]) {
69 const strings = [...format].reverse();
70 args.reverse();
71
72 const parts = [strings.pop()];
73 if (strings.length !== args.length) {
74 throw new Error('unequal strings and args in f-string');
75 }
76
77 while (args.length !== 0) {
78 const arg = args.pop();
79 parts.push(JSON.stringify(arg) || String(arg));
80 parts.push(strings.pop());
81 }
82
83 return parts.join('');
84 }
85
86 export default QUnit;