Skip to content

Commit f772574

Browse files
Copilothotlong
andcommitted
refactor: extract shared e2e helpers and vite crypto stub plugin
- Move waitForReactMount to e2e/helpers/index.ts - Move crypto stub plugin to scripts/vite-crypto-stub.ts - Update both vite configs and both e2e test files to use shared utilities Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 85c0d2c commit f772574

6 files changed

Lines changed: 46 additions & 56 deletions

File tree

apps/console/vite.config.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import path from 'path';
4+
import { viteCryptoStub } from '../../scripts/vite-crypto-stub';
45

56
// https://vitejs.dev/config/
67
export default defineConfig({
@@ -11,27 +12,8 @@ export default defineConfig({
1112
'process.version': '"0.0.0"',
1213
},
1314

14-
// @objectstack/core@2.0.4 statically imports Node.js crypto (for plugin hashing).
15-
// The code already has a browser fallback, so we provide an empty stub instead of
16-
// marking it as external (which emits a bare `import 'crypto'` that browsers reject).
17-
// enforce: 'pre' ensures this runs before Vite's built-in browser-external resolve.
1815
plugins: [
19-
{
20-
name: 'stub-crypto',
21-
enforce: 'pre',
22-
resolveId(id: string) {
23-
if (id === 'crypto') return '\0crypto-stub';
24-
},
25-
load(id: string) {
26-
if (id === '\0crypto-stub') {
27-
return [
28-
'export function createHash() { return { update() { return this; }, digest() { return ""; } }; }',
29-
'export function createVerify() { return { update() { return this; }, end() {}, verify() { return false; } }; }',
30-
'export default {};',
31-
].join('\n');
32-
}
33-
},
34-
},
16+
viteCryptoStub(),
3517
react(),
3618
],
3719
resolve: {

e2e/console-rendering.spec.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test, expect } from '@playwright/test';
2+
import { waitForReactMount } from './helpers';
23

34
/**
45
* Console rendering & navigation E2E tests.
@@ -8,14 +9,6 @@ import { test, expect } from '@playwright/test';
89
* the "blank page on Vercel" issue.
910
*/
1011

11-
/** Wait for React to mount (at least one child inside #root). */
12-
async function waitForReactMount(page: import('@playwright/test').Page) {
13-
await page.waitForFunction(
14-
() => (document.getElementById('root')?.children.length ?? 0) > 0,
15-
{ timeout: 30_000 },
16-
);
17-
}
18-
1912
test.describe('Console Rendering', () => {
2013
test('should not have critical console errors during bootstrap', async ({ page }) => {
2114
const criticalErrors: string[] = [];

e2e/helpers/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Page } from '@playwright/test';
2+
3+
/** Wait for React to mount (at least one child inside #root). */
4+
export async function waitForReactMount(page: Page) {
5+
await page.waitForFunction(
6+
() => (document.getElementById('root')?.children.length ?? 0) > 0,
7+
{ timeout: 30_000 },
8+
);
9+
}

e2e/smoke.spec.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test, expect } from '@playwright/test';
2+
import { waitForReactMount } from './helpers';
23

34
/**
45
* Smoke tests for the console production build.
@@ -12,14 +13,6 @@ import { test, expect } from '@playwright/test';
1213
* - React failing to mount into #root
1314
*/
1415

15-
/** Wait for React to mount (at least one child inside #root). */
16-
async function waitForReactMount(page: import('@playwright/test').Page) {
17-
await page.waitForFunction(
18-
() => (document.getElementById('root')?.children.length ?? 0) > 0,
19-
{ timeout: 30_000 },
20-
);
21-
}
22-
2316
test.describe('Console App – Smoke', () => {
2417
test('should load the page without JavaScript errors', async ({ page }) => {
2518
const errors: string[] = [];

examples/msw-todo/vite.config.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3+
import { viteCryptoStub } from '../../scripts/vite-crypto-stub';
34

45
// https://vitejs.dev/config/
56
export default defineConfig({
6-
// @objectstack/core@2.0.4 statically imports Node.js crypto (for plugin hashing).
7-
// The code already has a browser fallback, so we provide an empty stub instead of
8-
// marking it as external (which emits a bare `import 'crypto'` that browsers reject).
9-
// enforce: 'pre' ensures this runs before Vite's built-in browser-external resolve.
107
plugins: [
11-
{
12-
name: 'stub-crypto',
13-
enforce: 'pre',
14-
resolveId(id: string) {
15-
if (id === 'crypto') return '\0crypto-stub';
16-
},
17-
load(id: string) {
18-
if (id === '\0crypto-stub') {
19-
return [
20-
'export function createHash() { return { update() { return this; }, digest() { return ""; } }; }',
21-
'export function createVerify() { return { update() { return this; }, end() {}, verify() { return false; } }; }',
22-
'export default {};',
23-
].join('\n');
24-
}
25-
},
26-
},
8+
viteCryptoStub(),
279
react(),
2810
],
2911
server: {

scripts/vite-crypto-stub.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Plugin } from 'vite';
2+
3+
/**
4+
* Vite plugin that stubs the Node.js `crypto` module for browser builds.
5+
*
6+
* @objectstack/core statically imports `createHash` from `crypto` for plugin
7+
* hashing. The code already has a browser fallback, so we provide a no-op stub
8+
* instead of marking it as external (which emits a bare `import 'crypto'` that
9+
* browsers reject, causing a blank page).
10+
*
11+
* Must use `enforce: 'pre'` so it runs before Vite's built-in
12+
* browser-external resolve.
13+
*/
14+
export function viteCryptoStub(): Plugin {
15+
return {
16+
name: 'stub-crypto',
17+
enforce: 'pre',
18+
resolveId(id: string) {
19+
if (id === 'crypto') return '\0crypto-stub';
20+
},
21+
load(id: string) {
22+
if (id === '\0crypto-stub') {
23+
return [
24+
'export function createHash() { return { update() { return this; }, digest() { return ""; } }; }',
25+
'export function createVerify() { return { update() { return this; }, end() {}, verify() { return false; } }; }',
26+
'export default {};',
27+
].join('\n');
28+
}
29+
},
30+
};
31+
}

0 commit comments

Comments
 (0)