Skip to content

Commit

Permalink
use vite dev
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Apr 15, 2024
1 parent 51cc907 commit 2ca472e
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 325 deletions.
471 changes: 155 additions & 316 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions tests/addon-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"start": "ember serve",
"test": "vite build --mode test && ember test --path dist",
"test:ember": "vite build --mode test && ember test --path dist",
"test": "node ./scripts/run-tests.mjs",
"test:dist": "vite build --mode test && ember test --path dist",
"test:ember": "node ./scripts/run-tests.mjs",
"test:ember-compatibility": "ember try:each"
},
"dependencies": {
"ember-cli-babel": "^7.26.11",
"ember-cli-htmlbars": "^6.1.0"
},
"devDependencies": {
"puppeteer-chromium-resolver": "^21.0.0",
"js-reporters": "^2.1.0",
"@babel/core": "^7.19.3",
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
Expand Down
133 changes: 133 additions & 0 deletions tests/addon-template/scripts/run-tests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import child from "child_process";
import { resolve } from "path";
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");

await /** @type {Promise<void>} */ (
new Promise((fulfill) => {
const 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");
})
);

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 = [];

const result = await /** @type {Promise<void>} */ (
// 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 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(interceptedRequest.initiator().url)
) {
console.error(
"url does not use optimized dep",
url,
interceptedRequest.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();

if (unOptimizedDeps.length) {
console.error("unoptimized deps detected");
process.exit(1);
return;
}

process.exit(result);
}

run();
34 changes: 34 additions & 0 deletions tests/addon-template/tests/setup-harness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { autoRegister } from 'js-reporters';
import QUnit from 'qunit';

export function setupQunit() {
if (hasFlag('ci')) {
const runner = autoRegister();
const tap = QUnit.reporters.tap;
tap.init(runner, { log: console.info });

QUnit.config.urlConfig.push({
id: 'smoke_tests',
label: 'Enable Smoke Tests',
tooltip: 'Enable Smoke Tests',
});

QUnit.config.urlConfig.push({
id: 'ci',
label: 'Enable CI Mode',
tooltip:
'CI mode makes tests run faster by sacrificing UI responsiveness',
});

console.log(`[HARNESS] ci=${hasFlag('ci')}`);
}

QUnit.done((details) => {
console.log(JSON.stringify({ ...details, type: '[HARNESS] done' }));
});
}

function hasFlag(flag) {
let location = typeof window !== 'undefined' && window.location;
return location && new RegExp(`[?&]${flag}`).test(location.search);
}
3 changes: 3 additions & 0 deletions tests/addon-template/tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import * as QUnit from 'qunit';
import { setApplication } from '@ember/test-helpers';
import { setup } from 'qunit-dom';
import { start } from 'ember-qunit';
import { setupQunit } from './setup-harness';

setApplication(Application.create(config.APP));

setup(QUnit.assert);

setupQunit();

start();
4 changes: 3 additions & 1 deletion tests/addon-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export default defineConfig(({ mode }) => {
extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"],
}),
],
optimizeDeps: optimizeDeps(),
optimizeDeps: optimizeDeps(null, {
excludeLegacyAddons: ["ember-source/ember/index.js"],
}),
server: {
port: 4200,
watch: {
Expand Down
2 changes: 1 addition & 1 deletion tests/app-template/scripts/run-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function run() {
new Promise((fulfill) => {
const runvite = child.fork(
resolve(__root, "node_modules", "vite", "bin", "vite.js"),
["--port", "60173", "--no-open"],
["--port", "60173", "--no-open", "--force"],
{
stdio: "pipe",
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ts-app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"lint:hbs:fix": "ember-template-lint . --fix",
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"start": "ember serve",
"test": "concurrently \"npm:lint\" \"npm:test:*\" --names \"lint,test:\"",
"test:ember": "ember test"
"test": "node ./scripts/run-tests.mjs",
"test:dist": "vite build --mode test && ember test --path dist",
"test:ember": "node ./scripts/run-tests.mjs"
},
"devDependencies": {
"puppeteer-chromium-resolver": "^21.0.0",
"js-reporters": "^2.1.0",
"@babel/core": "^7.22.20",
"@babel/eslint-parser": "^7.21.3",
"@babel/plugin-proposal-decorators": "^7.21.0",
Expand Down
126 changes: 126 additions & 0 deletions tests/ts-app-template/scripts/run-tests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import child from 'child_process';
import { resolve } from 'path';
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');

await /** @type {Promise<void>} */ (
new Promise(fulfill => {
const 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');
})
);

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 = [];

const result = await /** @type {Promise<void>} */ (
// 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 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(interceptedRequest.initiator().url)
) {
console.error('url does not use optimized dep', url, interceptedRequest.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();

if (unOptimizedDeps.length) {
console.error('unoptimized deps detected');
process.exit(1);
return;
}

process.exit(result);
}

run();
Loading

0 comments on commit 2ca472e

Please sign in to comment.