forked from ruby/ruby.wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.spec.ts
87 lines (77 loc) · 2.74 KB
/
examples.spec.ts
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
import { test, expect, Page } from "@playwright/test";
import path from "path";
import {
waitForRubyVM,
setupDebugLog,
setupProxy,
setupUncaughtExceptionRejection,
} from "../support";
import { readFileSync } from "fs";
import http from "http";
import https from "https";
test.beforeEach(async ({ context, page }) => {
setupDebugLog(context);
setupUncaughtExceptionRejection(page);
if (process.env.RUBY_NPM_PACKAGE_ROOT) {
setupProxy(context);
} else {
console.info("Testing against CDN deployed files");
const packagePath = path.join(__dirname, "..", "..", "package.json");
const packageInfo = JSON.parse(readFileSync(packagePath, "utf-8"));
const version = packageInfo.version;
const url = `https://registry.npmjs.org/@ruby/head-wasm-wasi/${version}`;
const response = await new Promise<http.IncomingMessage>(
(resolve, reject) => {
https.get(url, resolve).on("error", reject);
},
);
if (response.statusCode == 404) {
console.log(
`@ruby/head-wasm-wasi@${version} is not published yet, so skipping CDN tests`,
);
test.skip();
}
}
});
test("hello.html is healthy", async ({ page }) => {
const messages = [];
page.on("console", (msg) => messages.push(msg.text()));
await page.goto("/hello.html");
await waitForRubyVM(page);
expect(messages[messages.length - 1]).toEqual("Hello, world!\n");
});
test("lucky.html is healthy", async ({ page }) => {
await page.goto("/lucky.html");
await waitForRubyVM(page);
await page.getByRole("button", { name: "Draw Omikuji" }).click();
const result = await page.locator("#result").textContent();
expect(result).toMatch(/(Lucky|Unlucky)/);
});
test("script-src/index.html is healthy", async ({ page }) => {
const messages = [];
page.on("console", (msg) => messages.push(msg.text()));
await page.goto("/script-src/index.html");
await waitForRubyVM(page);
const expected = "Hello, world!\n";
while (messages[messages.length - 1] != expected) {
await page.waitForEvent("console");
}
});
// The browser.script.iife.js obtained from CDN does not include the patch to require_relative.
// Skip when testing against the CDN.
if (process.env.RUBY_NPM_PACKAGE_ROOT) {
test("require_relative/index.html is healthy", async ({ page }) => {
// Add a listener to detect errors in the page
page.on("pageerror", (error) => {
console.log(`page error occurs: ${error.message}`);
});
const messages: string[] = [];
page.on("console", (msg) => messages.push(msg.text()));
await page.goto("/require_relative/index.html");
await waitForRubyVM(page);
const expected = "Hello, world!\n";
while (messages[messages.length - 1] != expected) {
await page.waitForEvent("console");
}
});
}