tests: Convert tests to TypeScript.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2025-08-06 13:16:46 -07:00
parent d9dbbf2359
commit 814de8ad6a
7 changed files with 58 additions and 24 deletions

1
.npmrc Normal file
View File

@@ -0,0 +1 @@
node-options=--experimental-strip-types

21
package-lock.json generated
View File

@@ -21,8 +21,10 @@
"@types/backoff": "^2.5.2", "@types/backoff": "^2.5.2",
"@types/i18n": "^0.13.1", "@types/i18n": "^0.13.1",
"@types/node": "^22.13.10", "@types/node": "^22.13.10",
"@types/p-fifo": "^1.0.2",
"@types/requestidlecallback": "^0.3.4", "@types/requestidlecallback": "^0.3.4",
"@types/semver": "^7.5.8", "@types/semver": "^7.5.8",
"@types/tape": "^5.8.1",
"@types/yaireo__tagify": "^4.3.2", "@types/yaireo__tagify": "^4.3.2",
"@yaireo/tagify": "^4.5.0", "@yaireo/tagify": "^4.5.0",
"adm-zip": "^0.5.5", "adm-zip": "^0.5.5",
@@ -3325,6 +3327,13 @@
"undici-types": "~6.21.0" "undici-types": "~6.21.0"
} }
}, },
"node_modules/@types/p-fifo": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@types/p-fifo/-/p-fifo-1.0.2.tgz",
"integrity": "sha512-Iz9wP2le1TQnvXNgPHwvr+16LcmT8T5DRijQCATOdVyL5xOMM/oBUIPFxRs4YJyqiKP5I1XC9vDI4zDxwGpDPA==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/pg": { "node_modules/@types/pg": {
"version": "8.15.5", "version": "8.15.5",
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.15.5.tgz", "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.15.5.tgz",
@@ -3414,6 +3423,18 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/tape": {
"version": "5.8.1",
"resolved": "https://registry.npmjs.org/@types/tape/-/tape-5.8.1.tgz",
"integrity": "sha512-vRjK+E1c+I4WRDSXcYfgepPjz2Knh+gulU3359LrR9H2KM8AyiMbNmX7W5aMlw7JFoXMpVOhq3bEIm78qakGbQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@ljharb/through": "*",
"@types/node": "*",
"mock-property": "*"
}
},
"node_modules/@types/tedious": { "node_modules/@types/tedious": {
"version": "4.0.14", "version": "4.0.14",
"resolved": "https://registry.npmjs.org/@types/tedious/-/tedious-4.0.14.tgz", "resolved": "https://registry.npmjs.org/@types/tedious/-/tedious-4.0.14.tgz",

View File

@@ -31,7 +31,7 @@
"lint-js": "xo", "lint-js": "xo",
"prettier-non-js": "prettier --check --log-level=warn . \"!**/*.{cjs,js,ts}\"", "prettier-non-js": "prettier --check --log-level=warn . \"!**/*.{cjs,js,ts}\"",
"test": "tsc && npm run lint-html && npm run lint-css && npm run lint-js && npm run prettier-non-js", "test": "tsc && npm run lint-html && npm run lint-css && npm run lint-js && npm run prettier-non-js",
"test-e2e": "vite build && tape \"tests/**/*.js\"", "test-e2e": "vite build && tape \"tests/**/*.ts\"",
"pack": "vite build && electron-builder --dir", "pack": "vite build && electron-builder --dir",
"dist": "vite build && electron-builder", "dist": "vite build && electron-builder",
"mas": "vite build && electron-builder --mac mas" "mas": "vite build && electron-builder --mac mas"
@@ -162,8 +162,10 @@
"@types/backoff": "^2.5.2", "@types/backoff": "^2.5.2",
"@types/i18n": "^0.13.1", "@types/i18n": "^0.13.1",
"@types/node": "^22.13.10", "@types/node": "^22.13.10",
"@types/p-fifo": "^1.0.2",
"@types/requestidlecallback": "^0.3.4", "@types/requestidlecallback": "^0.3.4",
"@types/semver": "^7.5.8", "@types/semver": "^7.5.8",
"@types/tape": "^5.8.1",
"@types/yaireo__tagify": "^4.3.2", "@types/yaireo__tagify": "^4.3.2",
"@yaireo/tagify": "^4.5.0", "@yaireo/tagify": "^4.5.0",
"adm-zip": "^0.5.5", "adm-zip": "^0.5.5",

View File

@@ -1,16 +1,17 @@
import Fifo from "p-fifo"; import Fifo from "p-fifo";
import type {Page} from "playwright-core";
import test from "tape"; import test from "tape";
import * as setup from "./setup.js"; import * as setup from "./setup.ts";
test("app runs", async (t) => { test("app runs", async (t) => {
t.timeoutAfter(10e3); t.timeoutAfter(10e3);
setup.resetTestDataDirectory(); setup.resetTestDataDirectory();
const app = await setup.createApp(); const app = await setup.createApp();
try { try {
const windows = new Fifo(); const windows = new Fifo<Page>();
for (const win of app.windows()) windows.push(win); for (const win of app.windows()) void windows.push(win);
app.on("window", (win) => windows.push(win)); app.on("window", async (win) => windows.push(win));
const mainWindow = await windows.shift(); const mainWindow = await windows.shift();
t.equal(await mainWindow.title(), "Zulip"); t.equal(await mainWindow.title(), "Zulip");

View File

@@ -1,43 +1,50 @@
import fs from "node:fs"; import fs from "node:fs";
import os from "node:os";
import path from "node:path"; import path from "node:path";
import process from "node:process"; import process from "node:process";
import {_electron} from "playwright-core"; import {type ElectronApplication, _electron} from "playwright-core";
import z from "zod";
const testsPackage = JSON.parse( const testsPackage = z
fs.readFileSync(new URL("package.json", import.meta.url), "utf8"), .object({productName: z.string()})
); .parse(
JSON.parse(
fs.readFileSync(new URL("package.json", import.meta.url), "utf8"),
),
);
// Runs Zulip Desktop. // Runs Zulip Desktop.
// Returns a promise that resolves to an Electron Application once the app has loaded. // Returns a promise that resolves to an Electron Application once the app has loaded.
export function createApp() { export async function createApp(): Promise<ElectronApplication> {
return _electron.launch({ return _electron.launch({
args: [import.meta.dirname], // Ensure this dir has a package.json file with a 'main' entry point args: [import.meta.dirname], // Ensure this dir has a package.json file with a 'main' entry point
}); });
} }
// Quit the app, end the test // Quit the app, end the test
export async function endTest(app) { export async function endTest(app: ElectronApplication): Promise<void> {
await app.close(); await app.close();
} }
function getAppDataDirectory() { function getAppDataDirectory(): string {
let base; let base;
switch (process.platform) { switch (process.platform) {
case "darwin": { case "darwin": {
base = path.join(process.env.HOME, "Library", "Application Support"); base = path.join(os.homedir(), "Library", "Application Support");
break; break;
} }
case "linux": { case "linux": {
base = base = process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config");
process.env.XDG_CONFIG_HOME ?? path.join(process.env.HOME, ".config");
break; break;
} }
case "win32": { case "win32": {
base = process.env.APPDATA; base = process.env.APPDATA;
if (base === undefined)
throw new Error("Missing APPDATA environment variable.");
break; break;
} }
@@ -51,7 +58,7 @@ function getAppDataDirectory() {
} }
// Resets the test directory, containing domain.json, window-state.json, etc // Resets the test directory, containing domain.json, window-state.json, etc
export function resetTestDataDirectory() { export function resetTestDataDirectory(): void {
const appDataDirectory = getAppDataDirectory(); const appDataDirectory = getAppDataDirectory();
fs.rmSync(appDataDirectory, {force: true, recursive: true}); fs.rmSync(appDataDirectory, {force: true, recursive: true});
} }

View File

@@ -1,16 +1,17 @@
import Fifo from "p-fifo"; import Fifo from "p-fifo";
import type {Page} from "playwright-core";
import test from "tape"; import test from "tape";
import * as setup from "./setup.js"; import * as setup from "./setup.ts";
test("add-organization", async (t) => { test("add-organization", async (t) => {
t.timeoutAfter(50e3); t.timeoutAfter(50e3);
setup.resetTestDataDirectory(); setup.resetTestDataDirectory();
const app = await setup.createApp(); const app = await setup.createApp();
try { try {
const windows = new Fifo(); const windows = new Fifo<Page>();
for (const win of app.windows()) windows.push(win); for (const win of app.windows()) void windows.push(win);
app.on("window", (win) => windows.push(win)); app.on("window", async (win) => windows.push(win));
const mainWindow = await windows.shift(); const mainWindow = await windows.shift();
t.equal(await mainWindow.title(), "Zulip"); t.equal(await mainWindow.title(), "Zulip");

View File

@@ -1,7 +1,8 @@
import Fifo from "p-fifo"; import Fifo from "p-fifo";
import type {Page} from "playwright-core";
import test from "tape"; import test from "tape";
import * as setup from "./setup.js"; import * as setup from "./setup.ts";
// Create new org link should open in the default browser [WIP] // Create new org link should open in the default browser [WIP]
@@ -10,9 +11,9 @@ test("new-org-link", async (t) => {
setup.resetTestDataDirectory(); setup.resetTestDataDirectory();
const app = await setup.createApp(); const app = await setup.createApp();
try { try {
const windows = new Fifo(); const windows = new Fifo<Page>();
for (const win of app.windows()) windows.push(win); for (const win of app.windows()) void windows.push(win);
app.on("window", (win) => windows.push(win)); app.on("window", async (win) => windows.push(win));
const mainWindow = await windows.shift(); const mainWindow = await windows.shift();
t.equal(await mainWindow.title(), "Zulip"); t.equal(await mainWindow.title(), "Zulip");