Skip to content

Commit

Permalink
fix: enable running tests without building
Browse files Browse the repository at this point in the history
  • Loading branch information
rturnq committed Jan 15, 2025
1 parent 6feb129 commit 78cab88
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 137 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"scripts": {
"@ci:build": "npm run build",
"@ci:lint": "eslint --format unix . && prettier . --check --with-node-modules --log-level=warn",
"@ci:release": "npm run build && changeset publish && npm ci",
"@ci:release": "npm run build && node scripts/pkg-toggle && changeset publish && node scripts/pkg-toggle && npm ci",
"@ci:test": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" MARKO_DEBUG=1 c8 npm test",
"@ci:version": "npm run build && npm run format && changeset version && npm i --package-lock-only",
"build": "npm run build -w packages -w packages/adapters",
"build": "npm run build -w packages -w packages/adapters --if-present",
"build:examples": "npm run build -w examples",
"change": "changeset add",
"format": "eslint --format unix --fix .; prettier . --write --with-node-modules --log-level=warn",
Expand Down
4 changes: 4 additions & 0 deletions packages/explorer/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function start(port: number): Promise<{
port: number;
close(): Promise<void>;
}>;
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line - Errors during build due to missing types?
import { spawnServerWorker } from "@marko/run/vite";
import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const entry = path.join(__dirname, "server.js");

export async function start(port: number) {
export async function start(port) {
const worker = await spawnServerWorker(entry, [], port, undefined, false);
return {
port,
Expand Down
32 changes: 23 additions & 9 deletions packages/explorer/server.ts → packages/explorer/lib/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { exec } from "child_process";
import compression from "compression";
import { createServer } from "http";
import { IncomingMessage } from "http";
import { ServerResponse } from "http";
import { dirname } from "path";
import path from "path";
import createStaticServe from "serve-static";
Expand All @@ -15,9 +13,9 @@ const packageDir = path.join(__dirname, "..");
const appDir = path.join(packageDir, ".app");
const entryFile = path.join(packageDir, "src", "index.ts");

let buildPromise: Promise<void> | null = null;
let buildPromise = null;
async function build() {
return (buildPromise ??= new Promise<void>((resolve, reject) => {
return (buildPromise ??= new Promise((resolve, reject) => {
exec(
`marko-run build --output ${appDir} ${entryFile}`,
{ cwd: packageDir, env: { ...process.env, MR_EXPLORER: "false" } },
Expand All @@ -34,22 +32,38 @@ async function build() {
}));
}

let middleware = (req: IncomingMessage, res: ServerResponse) => {
let middleware = (req, res) => {
build().then(() => middleware(req, res));
};

const compress = compression({
flush: zlib.constants.Z_PARTIAL_FLUSH,
threshold: 500,
});
const staticServe = createStaticServe(appDir, {
const servePublic = createStaticServe(`${appDir}/public`, {
index: false,
redirect: false,
maxAge: "10 minutes",
});

const serveAssets = createStaticServe(`${appDir}/public/assets`, {
index: false,
redirect: false,
immutable: true,
fallthrough: false,
maxAge: "365 days",
});

createServer((req, res) =>
compress(req as any, res as any, () =>
staticServe(req, res, () => middleware(req, res)),
),
compress(req, res, () => {
if (req.url.startsWith("/assets/")) {
req.url = req.url.slice(7);
serveAssets(req, res, () => {
res.statusCode = 404;
res.end();
});
} else {
servePublic(req, res, () => middleware(req, res));
}
}),
).listen(PORT);
9 changes: 5 additions & 4 deletions packages/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"import": "./lib/index.js",
"types": "./lib/index.d.ts"
}
},
"main": "dist/index.cjs",
"files": [
"dist",
"src"
],
"scripts": {
"build": "tsc -b tsconfig.build.json && tsx scripts/build.ts",
"dev": "MR_EXPLORER=false marko-run",
"preview": "MR_EXPLORER=false marko-run preview"
},
Expand All @@ -42,6 +40,9 @@
"typescript": "^5.7.2",
"vite": "^6.0.0"
},
"peerDependencies": {
"@marko/run": "^0"
},
"logo": {
"url": "https://github.com/marko-js/run/raw/main/assets/marko-run.png"
}
Expand Down
50 changes: 0 additions & 50 deletions packages/explorer/scripts/build.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/explorer/scripts/importMetaURL.js

This file was deleted.

8 changes: 0 additions & 8 deletions packages/explorer/tsconfig.build.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/explorer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"strict": true,
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"lib": ["es2018", "dom"]
Expand Down
62 changes: 9 additions & 53 deletions packages/run/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,14 @@
"author": "Ryan Turnquist <[email protected]>",
"type": "module",
"exports": {
".": {
"types": "./dist/runtime/index.d.ts",
"import": "./dist/runtime/index.js",
"require": "./dist/runtime/index.cjs"
},
"./namespace": {
"types": "./dist/runtime/namespace.d.ts"
},
"./router": {
"types": "./dist/runtime/router.d.ts",
"import": "./dist/runtime/router.js",
"require": "./dist/runtime/router.cjs"
},
"./vite": {
"types": "./dist/vite/index.d.ts",
"import": "./dist/vite/index.js",
"require": "./dist/vite/index.cjs"
},
"./adapter/middleware": {
"types": "./dist/adapter/middleware.d.ts",
"import": "./dist/adapter/middleware.js",
"require": "./dist/adapter/middleware.cjs"
},
"./adapter": {
"types": "./dist/adapter/index.d.ts",
"import": "./dist/adapter/index.js",
"require": "./dist/adapter/index.cjs"
}
},
"main": "./src/runtime/index.ts",
"types": "./dist/runtime/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/runtime/index.d.ts"
],
"namespace": [
"./dist/runtime/namespace.d.ts"
],
"router": [
"./dist/runtime/router.d.ts"
],
"vite": [
"./dist/vite/index.d.ts"
],
"adapter/middleware": [
"./dist/adapter/middleware.d.ts"
],
"adapter": [
"./dist/adapter/index.d.ts"
]
}
".": "./src/runtime/index.ts",
"./namespace": "./src/runtime/namespace.ts",
"./router": "./src/runtime/router.ts",
"./vite": "./src/vite/index.ts",
"./adapter/middleware": "./src/adapter/middleware.ts",
"./adapter": "./src/adapter/index.ts"
},
"types": "./src/runtime/index.ts",
"bin": {
"marko-run": "./dist/cli/index.mjs"
},
Expand All @@ -75,7 +30,7 @@
],
"scripts": {
"build": "rm -rf ./dist && tsc -b && tsx scripts/build.ts",
"test": "cross-env NODE_ENV=test mocha \"./src/**/__tests__/*.test.?(c)ts\"",
"test": "cross-env NODE_ENV=test NODE_OPTIONS='$NODE_OPTIONS --import tsx' mocha \"./src/**/__tests__/*.test.?(c)ts\"",
"test:inspect": "npm test -- --inspect",
"test:update": "npm test -- --update",
"test:watch": "npm test -- --watch"
Expand Down Expand Up @@ -106,6 +61,7 @@
"@marko/fixture-snapshots": "^2.2.1",
"@marko/testing-library": "^6.2.0",
"@types/debug": "^4.1.12",
"@types/diff": "^7.0.0",
"@types/glob": "^8.1.0",
"@types/human-format": "^1.0.3",
"@types/jsdom": "^21.1.7",
Expand Down
43 changes: 43 additions & 0 deletions packages/run/package.toggle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"exports": {
".": {
"types": "./dist/runtime/index.d.ts",
"import": "./dist/runtime/index.js",
"require": "./dist/runtime/index.cjs"
},
"./namespace": {
"types": "./dist/runtime/namespace.d.ts"
},
"./router": {
"types": "./dist/runtime/router.d.ts",
"import": "./dist/runtime/router.js",
"require": "./dist/runtime/router.cjs"
},
"./vite": {
"types": "./dist/vite/index.d.ts",
"import": "./dist/vite/index.js",
"require": "./dist/vite/index.cjs"
},
"./adapter/middleware": {
"types": "./dist/adapter/middleware.d.ts",
"import": "./dist/adapter/middleware.js",
"require": "./dist/adapter/middleware.cjs"
},
"./adapter": {
"types": "./dist/adapter/index.d.ts",
"import": "./dist/adapter/index.js",
"require": "./dist/adapter/index.cjs"
}
},
"types": "./dist/runtime/index.d.ts",
"typesVersions": {
"*": {
"*": ["./dist/runtime/index.d.ts"],
"namespace": ["./dist/runtime/namespace.d.ts"],
"router": ["./dist/runtime/router.d.ts"],
"vite": ["./dist/vite/index.d.ts"],
"adapter/middleware": ["./dist/adapter/middleware.d.ts"],
"adapter": ["./dist/adapter/index.d.ts"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { defaultNormalizer, defaultSerializer } from "@marko/fixture-snapshots";
import { diffLines } from "diff";
import fs from "fs";
import { JSDOM } from "jsdom";
// import url from 'url'
import snap from "mocha-snap";
import mochaSnap from "mocha-snap";
import { createRequire } from "module";
import path from "path";
import * as playwright from "playwright";
import url from "url";

import * as cli from "../cli/commands";
import type { Options } from "../vite";
import { SpawnedServer, waitForServer } from "../vite/utils/server";

// const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const snap = (mochaSnap as any).default as typeof mochaSnap;

// https://github.com/esbuild-kit/tsx/issues/113
const { toString } = Function.prototype;
Expand Down
Loading

0 comments on commit 78cab88

Please sign in to comment.