From 32670d9eb4136daa48d73ec7cfe222977bb26060 Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Mon, 16 Dec 2024 19:07:05 +0800 Subject: [PATCH] chore: add test case --- .../ssr/tests/base-async-entry.test.ts | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/integration/ssr/tests/base-async-entry.test.ts diff --git a/tests/integration/ssr/tests/base-async-entry.test.ts b/tests/integration/ssr/tests/base-async-entry.test.ts new file mode 100644 index 000000000000..3eac06fce0f3 --- /dev/null +++ b/tests/integration/ssr/tests/base-async-entry.test.ts @@ -0,0 +1,82 @@ +import dns from 'node:dns'; +import path, { join } from 'path'; +import { fs } from '@modern-js/utils'; +import puppeteer, { type Browser, type Page } from 'puppeteer'; +import { + getPort, + killApp, + launchApp, + launchOptions, +} from '../../../utils/modernTestUtils'; + +const fixtureDir = path.resolve(__dirname, '../fixtures'); + +dns.setDefaultResultOrder('ipv4first'); + +jest.setTimeout(1000 * 20); + +describe('init with SSR', () => { + let app: any; + let appPort: number; + let page: Page; + let browser: Browser; + let appDir: string; + + beforeAll(async () => { + appDir = join(fixtureDir, 'base-async-entry'); + appPort = await getPort(); + app = await launchApp(appDir, appPort); + + browser = await puppeteer.launch(launchOptions as any); + page = await browser.newPage(); + }); + + afterAll(async () => { + if (browser) { + browser.close(); + } + if (app) { + await killApp(app); + } + }); + + test('should get assets correctly', async () => { + page.setJavaScriptEnabled(false); + await page.goto(`http://localhost:${appPort}`, { + waitUntil: 'load', + }); + const scriptAry = await page.$$eval('body > script', scripts => + scripts + .filter(script => script.src && script.type !== 'application/json') + .map(script => { + return new URL(script.src).pathname; + }), + ); + const cssAry = await page.$$eval('head > link', links => + links + .filter(link => link.rel === 'stylesheet') + .map(link => { + return new URL(link.href).pathname; + }), + ); + + const loadableStats = fs.readJSONSync( + path.join(appDir, 'dist/loadable-stats.json'), + ); + const chunks = loadableStats.namedChunkGroups['async-main'].assets; + const urls: string[] = chunks.map((chunk: { name: string }) => { + return `/${chunk.name}`; + }); + const existAssets = loadableStats.entrypoints.main.assets.map( + (asset: { name: string }) => `/${asset.name}`, + ); + + const allInHTML = urls.every( + url => + existAssets.includes(url) || + scriptAry.includes(url) || + cssAry.includes(url), + ); + expect(allInHTML).toBeTruthy(); + }); +});