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/i18n": "^0.13.1",
"@types/node": "^22.13.10",
"@types/p-fifo": "^1.0.2",
"@types/requestidlecallback": "^0.3.4",
"@types/semver": "^7.5.8",
"@types/tape": "^5.8.1",
"@types/yaireo__tagify": "^4.3.2",
"@yaireo/tagify": "^4.5.0",
"adm-zip": "^0.5.5",
@@ -3325,6 +3327,13 @@
"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": {
"version": "8.15.5",
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.15.5.tgz",
@@ -3414,6 +3423,18 @@
"dev": true,
"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": {
"version": "4.0.14",
"resolved": "https://registry.npmjs.org/@types/tedious/-/tedious-4.0.14.tgz",

View File

@@ -31,7 +31,7 @@
"lint-js": "xo",
"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-e2e": "vite build && tape \"tests/**/*.js\"",
"test-e2e": "vite build && tape \"tests/**/*.ts\"",
"pack": "vite build && electron-builder --dir",
"dist": "vite build && electron-builder",
"mas": "vite build && electron-builder --mac mas"
@@ -162,8 +162,10 @@
"@types/backoff": "^2.5.2",
"@types/i18n": "^0.13.1",
"@types/node": "^22.13.10",
"@types/p-fifo": "^1.0.2",
"@types/requestidlecallback": "^0.3.4",
"@types/semver": "^7.5.8",
"@types/tape": "^5.8.1",
"@types/yaireo__tagify": "^4.3.2",
"@yaireo/tagify": "^4.5.0",
"adm-zip": "^0.5.5",

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,8 @@
import Fifo from "p-fifo";
import type {Page} from "playwright-core";
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]
@@ -10,9 +11,9 @@ test("new-org-link", async (t) => {
setup.resetTestDataDirectory();
const app = await setup.createApp();
try {
const windows = new Fifo();
for (const win of app.windows()) windows.push(win);
app.on("window", (win) => windows.push(win));
const windows = new Fifo<Page>();
for (const win of app.windows()) void windows.push(win);
app.on("window", async (win) => windows.push(win));
const mainWindow = await windows.shift();
t.equal(await mainWindow.title(), "Zulip");