Skip to content

Commit

Permalink
test: support typescript on qunit tests
Browse files Browse the repository at this point in the history
Rename qunit-tests.js to .ts and stop hanging our functions on the main
QUnit object (which isn't typesafe).  We can just export them from our
module.  Adjust the users accordingly.

Add explicit file extensions to the list of tests in files.js (removing
the hardcoded '.js' from build.js) and add our first .ts case.

This new case tests that `cockpit.assert()` works properly in the
positive and negative cases and also verifies that it can be used for
type narrowing (otherwise tsc would complain).
  • Loading branch information
allisonkarlitskaya committed Apr 16, 2024
1 parent bfc4186 commit 18ec3fa
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 107 deletions.
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function build() {

const { entryPoints, assetFiles, redhat_fonts } = getFiles(args.onlydir);
const tests = getTestFiles();
const testEntryPoints = tests.map(test => "pkg/" + test + ".js");
const testEntryPoints = tests.map(test => "pkg/" + test);

const pkgFirstPlugins = [
cleanPlugin({ subdir: args.onlydir }),
Expand Down
77 changes: 39 additions & 38 deletions files.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,45 @@ const info = {
],

tests: [
"base1/test-base64",
"base1/test-browser-storage",
"base1/test-cache",
"base1/test-chan",
"base1/test-dbus-address",
"base1/test-dbus-framed",
"base1/test-dbus",
"base1/test-echo",
"base1/test-events",
"base1/test-external",
"base1/test-file",
"base1/test-format",
"base1/test-framed-cache",
"base1/test-framed",
"base1/test-http",
"base1/test-journal-renderer",
"base1/test-locale",
"base1/test-location",
"base1/test-metrics",
"base1/test-no-jquery",
"base1/test-permissions",
"base1/test-promise",
"base1/test-protocol",
"base1/test-series",
"base1/test-spawn-proc",
"base1/test-spawn",
"base1/test-stream",
"base1/test-user",
"base1/test-utf8",
"base1/test-websocket",

"kdump/test-config-client",

"networkmanager/test-utils",

"shell/machines/test-machines",

"storaged/test-util",
"base1/test-base64.js",
"base1/test-browser-storage.js",
"base1/test-cache.js",
"base1/test-chan.js",
"base1/test-dbus-address.js",
"base1/test-dbus-framed.js",
"base1/test-dbus.js",
"base1/test-echo.js",
"base1/test-events.js",
"base1/test-external.js",
"base1/test-file.js",
"base1/test-format.js",
"base1/test-framed-cache.js",
"base1/test-framed.js",
"base1/test-http.js",
"base1/test-journal-renderer.js",
"base1/test-locale.js",
"base1/test-location.js",
"base1/test-metrics.js",
"base1/test-no-jquery.js",
"base1/test-permissions.js",
"base1/test-promise.js",
"base1/test-protocol.js",
"base1/test-series.js",
"base1/test-spawn-proc.js",
"base1/test-spawn.js",
"base1/test-stream.js",
"base1/test-types.ts",
"base1/test-user.js",
"base1/test-utf8.js",
"base1/test-websocket.js",

"kdump/test-config-client.js",

"networkmanager/test-utils.js",

"shell/machines/test-machines.js",

"storaged/test-util.js",
],

files: [
Expand Down
2 changes: 1 addition & 1 deletion node_modules
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"devDependencies": {
"@types/deep-equal": "1.0.4",
"@types/qunit": "^2.19.10",
"@types/react": "18.2.78",
"@types/react-dom": "18.2.25",
"@typescript-eslint/eslint-plugin": "7.7.0",
Expand Down
42 changes: 21 additions & 21 deletions pkg/base1/test-dbus-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import cockpit from "cockpit";
import QUnit from "qunit-tests";
import QUnit, { skipWithPybridge } from "qunit-tests";

function deep_update(target, data) {
for (const prop in data) {
Expand Down Expand Up @@ -104,7 +104,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
], "round trip");
});

QUnit.test.skipWithPybridge("integer bounds", async assert => {
skipWithPybridge("integer bounds", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);

async function testNumber(type, value, valid) {
Expand Down Expand Up @@ -163,7 +163,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
], "round trip");
});

QUnit.test.skipWithPybridge("variants", async assert => {
skipWithPybridge("variants", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
const reply = await dbus.call(
"/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
Expand All @@ -179,7 +179,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
], "round trip");
});

QUnit.test.skipWithPybridge("bad variants", async assert => {
skipWithPybridge("bad variants", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call(
Expand Down Expand Up @@ -236,7 +236,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("call bad base64", async assert => {
skipWithPybridge("call bad base64", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call(
Expand Down Expand Up @@ -322,7 +322,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
});
});

QUnit.test.skipWithPybridge("empty base64", assert => {
skipWithPybridge("empty base64", assert => {
const done = assert.async();
assert.expect(3);

Expand All @@ -340,7 +340,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
});
});

QUnit.test.skipWithPybridge("bad object path", async assert => {
skipWithPybridge("bad object path", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("invalid/path", "borkety.Bork", "Echo", [1]);
Expand All @@ -351,7 +351,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad interface name", async assert => {
skipWithPybridge("bad interface name", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/path", "!invalid!interface!", "Echo", [1]);
Expand All @@ -362,7 +362,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad method name", async assert => {
skipWithPybridge("bad method name", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/path", "borkety.Bork", "!Invalid!Method!", [1]);
Expand All @@ -373,7 +373,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad flags", async assert => {
skipWithPybridge("bad flags", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/path", "borkety.Bork", "Method", [1], { flags: 5 });
Expand All @@ -384,7 +384,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad types", async assert => {
skipWithPybridge("bad types", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/bork", "borkety.Bork", "Echo", [1], { type: "!!%%" });
Expand All @@ -395,7 +395,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad type invalid", async assert => {
skipWithPybridge("bad type invalid", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/bork", "borkety.Bork", "Echo", [1], { type: 5 }); // invalid
Expand All @@ -406,7 +406,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad dict type", async assert => {
skipWithPybridge("bad dict type", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody",
Expand All @@ -418,7 +418,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad object path", async assert => {
skipWithPybridge("bad object path", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody",
Expand All @@ -430,7 +430,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
}
});

QUnit.test.skipWithPybridge("bad signature", async assert => {
skipWithPybridge("bad signature", async assert => {
const dbus = cockpit.dbus(bus_name, channel_options);
try {
await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber", "Nobody", ["bad signature"], { type: "g" });
Expand Down Expand Up @@ -710,7 +710,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
dbus.removeEventListener("notify", onnotify);
});

QUnit.test.skipWithPybridge("path loop", async assert => {
skipWithPybridge("path loop", async assert => {
const name = "yo" + new Date().getTime();
const cache = { };

Expand All @@ -733,7 +733,7 @@ export function common_dbus_tests(channel_options, bus_name) { // eslint-disable
dbus.removeEventListener("notify", onnotify);
});

QUnit.test.skipWithPybridge("path signal", async assert => {
skipWithPybridge("path signal", async assert => {
const name = "yo" + new Date().getTime();
const cache = { };

Expand Down Expand Up @@ -940,7 +940,7 @@ export function dbus_track_tests(channel_options, bus_name) {
assert.equal(gone, false, "is not gone");
});

QUnit.test.skipWithPybridge("receive readable fd", async assert => {
skipWithPybridge("receive readable fd", async assert => {
const done = assert.async();
assert.expect(3);

Expand All @@ -959,7 +959,7 @@ export function dbus_track_tests(channel_options, bus_name) {
};
});

QUnit.test.skipWithPybridge("receive readable fd and ensure opening more than once fails", async assert => {
skipWithPybridge("receive readable fd and ensure opening more than once fails", async assert => {
const done = assert.async();
assert.expect(6);

Expand All @@ -981,7 +981,7 @@ export function dbus_track_tests(channel_options, bus_name) {
};
});

QUnit.test.skipWithPybridge("receive readable fd and ensure writing fails", async assert => {
skipWithPybridge("receive readable fd and ensure writing fails", async assert => {
const done = assert.async();
assert.expect(5);

Expand All @@ -1002,7 +1002,7 @@ export function dbus_track_tests(channel_options, bus_name) {
};
});

QUnit.test.skipWithPybridge("receive writable fd", async assert => {
skipWithPybridge("receive writable fd", async assert => {
const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", channel_options);
const [fd] = await dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
"MakeTestFd", ["writable"]);
Expand Down
Loading

0 comments on commit 18ec3fa

Please sign in to comment.