From 5e1f33ea6e0c8d1d84dd24dbd6eb385c07c3cb73 Mon Sep 17 00:00:00 2001 From: dmail Date: Mon, 6 Nov 2023 16:26:15 +0100 Subject: [PATCH] add test for inline importmap autoreload --- .../autoreload_importmap_inline.test.mjs | 89 +++++++++++++++++++ .../autoreload_importmap_inline/client/a.js | 1 + .../autoreload_importmap_inline/client/b.js | 1 + .../client/main.html | 25 ++++++ 4 files changed, 116 insertions(+) create mode 100644 tests/dev_server/autoreload_importmap_inline/autoreload_importmap_inline.test.mjs create mode 100644 tests/dev_server/autoreload_importmap_inline/client/a.js create mode 100644 tests/dev_server/autoreload_importmap_inline/client/b.js create mode 100644 tests/dev_server/autoreload_importmap_inline/client/main.html diff --git a/tests/dev_server/autoreload_importmap_inline/autoreload_importmap_inline.test.mjs b/tests/dev_server/autoreload_importmap_inline/autoreload_importmap_inline.test.mjs new file mode 100644 index 0000000000..68ce488a1a --- /dev/null +++ b/tests/dev_server/autoreload_importmap_inline/autoreload_importmap_inline.test.mjs @@ -0,0 +1,89 @@ +/* + * Test the following: + * - importmap resolution applies correctly when inline + * - updating inline importmap trigger autoreload + correctly update resolution + */ + +import { readFileSync, writeFileSync } from "node:fs"; +import { chromium } from "playwright"; +import { assert } from "@jsenv/assert"; + +import { startDevServer } from "@jsenv/core"; + +const htmlFileUrl = new URL("./client/main.html", import.meta.url); +const htmlFileContent = { + beforeTest: readFileSync(htmlFileUrl), + update: (content) => writeFileSync(htmlFileUrl, content), + restore: () => writeFileSync(htmlFileUrl, htmlFileContent.beforeTest), +}; +const devServer = await startDevServer({ + logLevel: "warn", + serverLogLevel: "warn", + sourceDirectoryUrl: new URL("./client/", import.meta.url), + outDirectoryUrl: new URL("./.jsenv/", import.meta.url), + keepProcessAlive: false, +}); + +const browser = await chromium.launch({ headless: true }); +const page = await browser.newPage({ ignoreHTTPSErrors: true }); + +try { + await page.goto(`${devServer.origin}/main.html`); + const getWindowAnswer = () => { + return page.evaluate( + /* eslint-disable no-undef */ + () => window.answer, + /* eslint-enable no-undef */ + ); + }; + + { + const actual = await getWindowAnswer(); + const expected = 42; + assert({ actual, expected }); + } + + const pageReloadPromise = page.waitForNavigation(); + htmlFileContent.update(` + + + Title + + + + + + + + + `); + await pageReloadPromise; + { + const actual = await getWindowAnswer(); + const expected = "b"; + assert({ actual, expected }); + } + htmlFileContent.restore(); + await new Promise((resolve) => setTimeout(resolve, 500)); + { + const actual = await getWindowAnswer(); + const expected = 42; + assert({ actual, expected }); + } +} finally { + htmlFileContent.restore(); + browser.close(); + devServer.stop(); // required because for some reason the rooms are kept alive +} diff --git a/tests/dev_server/autoreload_importmap_inline/client/a.js b/tests/dev_server/autoreload_importmap_inline/client/a.js new file mode 100644 index 0000000000..64a32fd291 --- /dev/null +++ b/tests/dev_server/autoreload_importmap_inline/client/a.js @@ -0,0 +1 @@ +export const answer = 42; diff --git a/tests/dev_server/autoreload_importmap_inline/client/b.js b/tests/dev_server/autoreload_importmap_inline/client/b.js new file mode 100644 index 0000000000..95cc880473 --- /dev/null +++ b/tests/dev_server/autoreload_importmap_inline/client/b.js @@ -0,0 +1 @@ +export const answer = "b"; diff --git a/tests/dev_server/autoreload_importmap_inline/client/main.html b/tests/dev_server/autoreload_importmap_inline/client/main.html new file mode 100644 index 0000000000..40e233699a --- /dev/null +++ b/tests/dev_server/autoreload_importmap_inline/client/main.html @@ -0,0 +1,25 @@ + + + + Title + + + + + + + + +