From 817eb368b9b8c73f7781bcadd131f27c6067bb5b Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 22 May 2024 14:40:44 +0200 Subject: [PATCH] fix tests --- .../scenarios/app-config-environment-test.ts | 2 +- .../compat-addon-classic-features-test.ts | 2 +- tests/scenarios/package.json | 2 +- tests/scenarios/stage1-test.ts | 4 +- tests/scenarios/vite-app-dev-test.ts | 287 ------------------ tests/scenarios/vite-dep-optimizer-test.ts | 14 +- 6 files changed, 12 insertions(+), 299 deletions(-) delete mode 100644 tests/scenarios/vite-app-dev-test.ts diff --git a/tests/scenarios/app-config-environment-test.ts b/tests/scenarios/app-config-environment-test.ts index fa4cec64b..73a7a733c 100644 --- a/tests/scenarios/app-config-environment-test.ts +++ b/tests/scenarios/app-config-environment-test.ts @@ -82,7 +82,7 @@ appScenarios // and is equivalent to visiting the app's /tests page let devBuildResult = await app.execute(`pnpm build`); assert.equal(devBuildResult.exitCode, 0, devBuildResult.output); - let testRunResult = await app.execute(`pnpm test:dist --path dist`); + let testRunResult = await app.execute(`pnpm test:ember --path dist`); assert.equal(testRunResult.exitCode, 0, testRunResult.output); }); }); diff --git a/tests/scenarios/compat-addon-classic-features-test.ts b/tests/scenarios/compat-addon-classic-features-test.ts index e719dcff0..819029205 100644 --- a/tests/scenarios/compat-addon-classic-features-test.ts +++ b/tests/scenarios/compat-addon-classic-features-test.ts @@ -217,7 +217,7 @@ appScenarios }); test('virtual styles are included in the CSS of the test build', async function (assert) { - let result = await app.execute('pnpm test:dist'); + let result = await app.execute('pnpm test'); assert.equal(result.exitCode, 0, result.output); // TODO: replace with an Audit when it's ready to take any given dist diff --git a/tests/scenarios/package.json b/tests/scenarios/package.json index 2aa348d0e..5fe97e90c 100644 --- a/tests/scenarios/package.json +++ b/tests/scenarios/package.json @@ -25,7 +25,7 @@ "ts-node": "^10.9.1" }, "scripts": { - "test": "qunit --require ts-node/register *-test.ts --filter=vite-dep-optimizer", + "test": "qunit --require ts-node/register *-test.ts", "test:list": "scenario-tester list --require ts-node/register --files=*-test.ts", "test:output": "scenario-tester output --require ts-node/register --files=*-test.ts" }, diff --git a/tests/scenarios/stage1-test.ts b/tests/scenarios/stage1-test.ts index 1c5bc4b81..62a319d61 100644 --- a/tests/scenarios/stage1-test.ts +++ b/tests/scenarios/stage1-test.ts @@ -144,7 +144,7 @@ appScenarios ); fileContents.matches( - /precompileTemplate\(["']
Extra<\/div>["']\)/, + /hbs\(["']
Extra<\/div>["']\)/, 'called template is still hbs and custom transforms have run' ); @@ -225,7 +225,7 @@ appScenarios ); file.matches( - /precompileTemplate\(["']
Extra<\/div>["']\)/, + /hbs\(["']
Extra<\/div>["']\)/, 'called template is still hbs and custom transforms have run' ); diff --git a/tests/scenarios/vite-app-dev-test.ts b/tests/scenarios/vite-app-dev-test.ts deleted file mode 100644 index fa8123fb9..000000000 --- a/tests/scenarios/vite-app-dev-test.ts +++ /dev/null @@ -1,287 +0,0 @@ -import { appScenarios } from './scenarios'; -import type { PreparedApp } from 'scenario-tester'; -import QUnit from 'qunit'; - -const { module: Qmodule, test } = QUnit; - -appScenarios - .map('vite-app-development', project => { - project.mergeFiles({ - scripts: { - 'run-tests.mjs': ` - import child from "child_process"; - import { resolve } from "path"; - import { readdirSync, writeFileSync, rmSync } from "fs"; - import PCR from "puppeteer-chromium-resolver"; - - const __root = process.cwd(); - - async function run() { - // eslint-disable-next-line new-cap - const { puppeteer, executablePath } = await PCR({}); - console.log("[ci] starting"); - let runvite; - - async function startVite() { - await /** @type {Promise} */ ( - new Promise((fulfill) => { - runvite = child.fork( - resolve(__root, "node_modules", "vite", "bin", "vite.js"), - ["--port", "60173", "--no-open", "--force"], - { - stdio: "pipe", - } - ); - - process.on("exit", () => runvite.kill()); - - runvite.stderr.on("data", (data) => { - console.log("stderr", String(data)); - }); - - runvite.stdout.on("data", (data) => { - const chunk = String(data); - console.log("stdout", chunk); - if (chunk.includes("Local") && chunk.includes("60173")) { - fulfill(1); - } - }); - - console.log("[ci] spawning"); - }) - ); - } - - async function runTests() { - console.log("[ci] spawned"); - - const browser = await puppeteer.launch({ - headless: "new", - executablePath, - args: ["--no-sandbox", "--disable-setuid-sandbox"], - }); - - console.log("[ci] puppeteer launched"); - let unOptimizedDeps = []; - let allUrls = new Set(); - let madeRequests = {}; - let result = await /** @type {Promise} */ ( - // eslint-disable-next-line no-async-promise-executor - new Promise(async (fulfill) => { - const page = await browser.newPage(); - - page.on("pageerror", (msg) => { - console.error(msg); - fulfill(1); - }); - - function logRequest(interceptedRequest) { - const url = interceptedRequest.url(); - const initiator = interceptedRequest.initiator().url; - madeRequests[initiator] = madeRequests[initiator] || []; - - madeRequests[initiator].push(url); - allUrls.add(url); - const allow = [ - "vite/dist/client/env.mjs", - "@babel+runtime", - ".css", - "@embroider/macros", - "ember-source/ember/index.js", - ]; - - function importerAllowedUnoptimized(importer) { - // virtual modules can contain the rewritten-app location - if (allow.some((a) => url.includes(a))) { - return true; - } - return !!( - importer.includes("node_modules") && - !importer.includes("rewritten-app") - ); - } - - if ( - url.includes("node_modules") && - !url.includes("rewritten-app") && - !url.includes(".vite/deps") && - !url.includes("embroider_virtual") && - !importerAllowedUnoptimized(initiator) - ) { - console.error("url does not use optimized dep", url, initiator); - unOptimizedDeps.push(url); - } - } - page.on("request", logRequest); - - page.on("console", (msg) => { - const text = msg.text(); - const location = msg.location(); - if (text.includes("HARNESS")) { - try { - const parsed = JSON.parse(text); - if (parsed.type === "[HARNESS] done") { - return fulfill(parsed.failed > 0 ? 1 : 0); - } - } catch (e) {} - } - if (location.url?.includes(\`/qunit.js\`)) { - console.log(text); - } else { - console.debug(text); - } - }); - - await page.goto("http://localhost:60173/tests/?hidepassed&ci"); - }) - ); - - await browser.close(); - - const optmizedDeps = readdirSync("./node_modules/.vite/deps") - .filter((f) => !f.endsWith(".map")) - .filter((f) => f !== "_metadata.json") - .filter((f) => f !== "package.json"); - const allUrlsList = [...allUrls]; - const unusedOptimzedDep = optmizedDeps.filter((o) => { - return !allUrlsList.some((url) => url.includes(o)); - }); - - if (unusedOptimzedDep.length) { - console.error("has unused optmized deps", unusedOptimzedDep); - result = 1; - } - - if (unOptimizedDeps.length) { - console.error("unoptimized deps detected", unOptimizedDeps); - result = 1; - } - - return { - result, - madeRequests, - }; - } - - async function test1() { - console.log("test1"); - await startVite(); - await runTests(); - // need also test compat for other types - const applicationHbs = \` - {{page-title "ViteApp"}} - {{outlet}} - \`; - const importsTest = \` - import { test } from 'qunit'; - import * as title from 'app-template/helpers/page-title'; - test("should work", async function (assert) { - assert.ok(title.default); - }) - \`.trim(); - writeFileSync("./app/templates/application.hbs", applicationHbs); - writeFileSync("./tests/unit/imports-test.js", importsTest); - await runTests(); - await runvite.kill(); - } - - async function test2() { - console.log("test2"); - await startVite(); - await runTests(); - const importsTest = \` - import { test } from 'qunit'; - import * as title2 from '../../helpers/page-title'; - test("should work", async function (assert) { - assert.ok(title2.default); - }) - \`.trim(); - writeFileSync("./tests/unit/imports-test.js", importsTest); - await runTests(); - await runvite.kill(); - } - - async function test3() { - console.log("test3"); - await startVite(); - await runTests(); - const importsTest = \` - import { test } from 'qunit'; - import * as title3 from 'ember-page-title/helpers/page-title'; - test("should work", async function (assert) { - assert.ok(title3.default); - }) - \`.trim(); - writeFileSync("./tests/unit/imports-test.js", importsTest); - await runTests(); - await runvite.kill(); - } - - async function test4() { - console.log("test4"); - await startVite(); - await runTests(); - const importsTest = \` - import { test } from 'qunit'; - import * as title4 from '#embroider_compat/helpers/page-title'; - test("should work", async function (assert) { - assert.ok(title4.default); - }) - \`.trim(); - writeFileSync("./tests/unit/imports-test.js", importsTest); - await runTests(); - await runvite.kill(); - } - - async function test5() { - console.log("test5"); - await startVite(); - await runTests(); - const importsTest = \` - import { test } from 'qunit'; - import * as title from 'app-template/helpers/page-title'; - import * as title2 from '../../helpers/page-title'; - import * as title3 from 'ember-page-title/helpers/page-title'; - import * as title4 from '#embroider_compat/helpers/page-title'; - - test("should work", async function (assert) { - assert.ok(title.default); - assert.ok(title2.default); - assert.ok(title3.default); - assert.ok(title.default === title2.default); - assert.ok(title.default === title3.default); - assert.ok(title.default === title4.default); - }) - \`.trim(); - writeFileSync("./tests/unit/imports-test.js", importsTest); - await runTests(); - await runvite.kill(); - } - - await test1(); - await test2(); - await test3(); - await test4(); - await test5(); - } - - await run(); - - `, - }, - }); - }) - .forEachScenario(scenario => { - Qmodule(scenario.name, function (hooks) { - let app: PreparedApp; - - hooks.before(async () => { - app = await scenario.prepare(); - }); - - test(`pnpm test:ember`, async function (assert) { - let result = await app.execute('pnpm test:ember'); - assert.equal(result.exitCode, 0, result.output); - }); - }); - }); diff --git a/tests/scenarios/vite-dep-optimizer-test.ts b/tests/scenarios/vite-dep-optimizer-test.ts index d23a6c7b8..9bf2a948b 100644 --- a/tests/scenarios/vite-dep-optimizer-test.ts +++ b/tests/scenarios/vite-dep-optimizer-test.ts @@ -56,7 +56,7 @@ app.forEachScenario(scenario => { } catch (e) { await new Promise(resolve => setTimeout(resolve, 1000)); retries += 1; - if (retries > 10) { + if (retries > 30) { throw new Error(`unable to visit all urls ${e}`); } continue; @@ -111,7 +111,7 @@ app.forEachScenario(scenario => { test('created initial optimized deps', async function (assert) { await waitUntilOptimizedReady(expectAudit); optimizedFiles = readdirSync(join(app.dir, 'node_modules', '.vite', 'deps')).filter(f => f.endsWith('.js')); - assert.ok(optimizedFiles.length === 136, `should have created optimized deps: ${optimizedFiles.length}`); + assert.ok(optimizedFiles.length === 144, `should have created optimized deps: ${optimizedFiles.length}`); }); test('should use all optimized deps', function (assert) { @@ -170,7 +170,6 @@ app.forEachScenario(scenario => { await server.shutdown(); server = CommandWatcher.launch('vite', ['--clearScreen', 'false', '--force'], { cwd: app.dir }); [, appURL] = await server.waitFor(/Local:\s*(.*)/); - await expectAudit.rerun(); }); hooks.afterEach(async () => { await server.shutdown(); @@ -204,7 +203,8 @@ app.forEachScenario(scenario => { }); }); - test('all optimized deps are used', function (assert) { + test('all optimized deps are used', async function (assert) { + await waitUntilOptimizedReady(expectAudit); const optimizedFiles = readdirSync(join(app.dir, 'node_modules', '.vite', 'deps')).filter(f => f.endsWith('.js') ); @@ -228,8 +228,8 @@ app.forEachScenario(scenario => { .resolves(/dep-tests\.js/) .toModule() .withContents((_src, imports) => { - let pageTitleImports = imports.filter(imp => /my-services-addon/.test(imp.source)); - assert.strictEqual(pageTitleImports.length, 1, 'found two uses of page-title addon'); + let pageTitleImports = imports.filter(imp => /page-title/.test(imp.source)); + assert.strictEqual(pageTitleImports.length, 1, `found one use of page-title addon: ${imports}`); assert.ok( pageTitleImports.every(isOptimizedImport), `every page-title module is optimized but we saw ${pageTitleImports.map(i => i.source).join(', ')}` @@ -317,6 +317,7 @@ app.forEachScenario(scenario => { }); test('ember index is not optimized', async function (assert) { + await waitUntilOptimizedReady(expectAudit); expectAudit .module('./index.html') .resolves(/\/@embroider\/core\/entrypoint/) @@ -346,7 +347,6 @@ app.forEachScenario(scenario => { await server.shutdown(); server = CommandWatcher.launch('vite', ['--clearScreen', 'false', '--force'], { cwd: app.dir }); [, appURL] = await server.waitFor(/Local:\s*(.*)/); - await expectAudit.rerun(); }); hooks.afterEach(async () => { await server.shutdown();