Skip to content

Commit 969d53f

Browse files
Add e2e integration test for data-eval=async
1 parent f61c620 commit 969d53f

File tree

7 files changed

+89
-23
lines changed

7 files changed

+89
-23
lines changed

packages/npm-packages/ruby-wasm-wasi/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
"test": "npm run test:head",
2929
"test:jest": "NODE_OPTIONS=\"--experimental-wasi-unstable-preview1\" jest --coverage",
3030
"test:unit": "./tools/run-test-unit.mjs",
31-
"test:e2e": "npm run test:e2e:example",
32-
"test:e2e:example": "playwright install && playwright test -c test-e2e/playwright.examples.config.ts",
31+
"test:e2e": "playwright install && npm run test:e2e:examples && npm run test:e2e:integrations",
32+
"test:e2e:examples": "playwright test -c test-e2e/playwright.examples.config.ts",
33+
"test:e2e:integrations": "playwright test -c test-e2e/playwright.integrations.config.ts",
3334
"serve:example": "bundle exec ruby -run -e httpd ./example -p 8085",
3435
"format": "prettier --write .",
3536
"build:static": "./tools/pack-bindgen-src.sh ./dist",

packages/npm-packages/ruby-wasm-wasi/test-e2e/examples/examples.spec.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
import { test, expect, Page } from '@playwright/test';
2-
import fs from "fs"
32
import path from "path"
3+
import { waitForRubyVM, setupDebugLog, setupProxy } from "../support"
44

55
test.beforeEach(async ({ context }) => {
6-
if (process.env.DEBUG) {
7-
context.on('request', request => console.log('>>', request.method(), request.url()));
8-
context.on('response', response => console.log('<<', response.status(), response.url()));
9-
context.on("console", msg => console.log("LOG:", msg.text()))
10-
}
6+
setupDebugLog(context);
117
if (process.env.RUBY_NPM_PACKAGE_ROOT) {
12-
const cdnPattern = /cdn.jsdelivr.net\/npm\/ruby-head-wasm-wasi@.+\/dist\/(.+)/
13-
context.route(cdnPattern, route => {
14-
const request = route.request()
15-
console.log(">> [MOCK]", request.method(), request.url())
16-
const relativePath = request.url().match(cdnPattern)[1]
17-
route.fulfill({ path: path.join(process.env.RUBY_NPM_PACKAGE_ROOT, "dist", relativePath) })
18-
})
8+
setupProxy(context);
199
} else {
2010
console.info("Testing against CDN deployed files")
2111
}
2212
})
2313

24-
const waitForRubyVM = async (page: Page) => {
25-
await page.waitForFunction(() => window["rubyVM"])
26-
}
27-
2814
test('hello.html is healthy', async ({ page }) => {
2915
const messages = []
3016
page.on("console", msg => messages.push(msg.text()))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { test, expect, Page } from '@playwright/test';
2+
3+
import { setupDebugLog, setupProxy, waitForRubyVM } from "../support"
4+
5+
if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
6+
test.skip('skip', () => {})
7+
} else {
8+
test.beforeEach(async ({ context }) => {
9+
setupDebugLog(context);
10+
setupProxy(context);
11+
})
12+
13+
test.describe('data-eval="async"', () => {
14+
test("JS::Object#await returns value", async ({ page, context }) => {
15+
let checkResolved;
16+
const resolvedValue = new Promise((resolve) => {
17+
checkResolved = resolve;
18+
})
19+
await page.exposeBinding('checkResolved', async (source, v) => {
20+
checkResolved(v);
21+
});
22+
await page.setContent(`
23+
<script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.script.iife.js"></script>
24+
<script type="text/ruby" data-eval="async">
25+
require "js"
26+
JS.global.checkResolved JS.global[:Promise].resolve(42).await
27+
</script>
28+
`)
29+
expect(await resolvedValue).toBe(42);
30+
})
31+
32+
test("JS::Object#await throws error on default attr", async ({ page, context }) => {
33+
await page.setContent(`
34+
<script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.script.iife.js"></script>
35+
<script type="text/ruby">
36+
require "js"
37+
JS.global[:Promise].resolve(42).await
38+
</script>
39+
`)
40+
const error = await page.waitForEvent("pageerror")
41+
expect(error.message).toMatch(/please ensure that you specify `data-eval="async"`/)
42+
})
43+
})
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
fullyParallel: true,
3+
retries: process.env.CI ? 2 : 0,
4+
workers: process.env.CI ? 1 : undefined,
5+
};

packages/npm-packages/ruby-wasm-wasi/test-e2e/playwright.examples.config.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { defineConfig, devices } from '@playwright/test';
1+
import { defineConfig } from '@playwright/test';
2+
import base from "./playwright.base.config"
23

34
export default defineConfig({
5+
...base,
46
testDir: 'examples',
5-
fullyParallel: true,
6-
retries: process.env.CI ? 2 : 0,
7-
workers: process.env.CI ? 1 : undefined,
87
use: {
98
baseURL: 'http://127.0.0.1:8085',
109
},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from '@playwright/test';
2+
import base from "./playwright.base.config"
3+
4+
export default defineConfig({
5+
...base,
6+
testDir: 'integrations',
7+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { BrowserContext, Page } from '@playwright/test';
2+
import path from "path"
3+
4+
export const waitForRubyVM = async (page: Page) => {
5+
await page.waitForFunction(() => window["rubyVM"])
6+
}
7+
8+
export const setupDebugLog = (context: BrowserContext) => {
9+
if (process.env.DEBUG) {
10+
context.on('request', request => console.log('>>', request.method(), request.url()));
11+
context.on('response', response => console.log('<<', response.status(), response.url()));
12+
context.on("console", msg => console.log("LOG:", msg.text()))
13+
}
14+
}
15+
16+
export const setupProxy = (context: BrowserContext) => {
17+
const cdnPattern = /cdn.jsdelivr.net\/npm\/ruby-head-wasm-wasi@.+\/dist\/(.+)/
18+
context.route(cdnPattern, route => {
19+
const request = route.request()
20+
console.log(">> [MOCK]", request.method(), request.url())
21+
const relativePath = request.url().match(cdnPattern)[1]
22+
route.fulfill({ path: path.join(process.env.RUBY_NPM_PACKAGE_ROOT, "dist", relativePath) })
23+
})
24+
}

0 commit comments

Comments
 (0)