From f731ca21ea4cfe38418880f15f6064e156a43a5e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 18 Nov 2024 21:00:04 +0900 Subject: [PATCH] test: simplify `playground/json/__tests__/ssr` (#18701) --- .../ssr/__tests__/fixtures/json/test.json | 3 + .../node/ssr/__tests__/ssrLoadModule.spec.ts | 25 +++++ .../json/__tests__/ssr/json-ssr.spec.ts | 23 ----- playground/json/__tests__/ssr/serve.ts | 35 ------- playground/json/index.ssr.html | 23 ----- playground/json/package.json | 5 +- playground/json/server.js | 92 ------------------- 7 files changed, 29 insertions(+), 177 deletions(-) create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/json/test.json delete mode 100644 playground/json/__tests__/ssr/json-ssr.spec.ts delete mode 100644 playground/json/__tests__/ssr/serve.ts delete mode 100644 playground/json/index.ssr.html delete mode 100644 playground/json/server.js diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/json/test.json b/packages/vite/src/node/ssr/__tests__/fixtures/json/test.json new file mode 100644 index 00000000000000..bacbe05617617e --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/json/test.json @@ -0,0 +1,3 @@ +{ + "hello": "this is json" +} diff --git a/packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts index 4479d20be233c5..1fbe62a9d04631 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts @@ -1,5 +1,6 @@ import { fileURLToPath } from 'node:url' import path from 'node:path' +import fs from 'node:fs' import { stripVTControlCharacters } from 'node:util' import { expect, test } from 'vitest' import { createServer } from '../../server' @@ -212,3 +213,27 @@ test('parse error', async () => { expect.unreachable() } }) + +test('json', async () => { + const server = await createDevServer() + const mod = await server.ssrLoadModule('/fixtures/json/test.json') + expect(mod).toMatchInlineSnapshot(` + { + "default": { + "hello": "this is json", + }, + "hello": "this is json", + } + `) + + const source = fs.readFileSync( + path.join(root, 'fixtures/json/test.json'), + 'utf-8', + ) + const json = await server.ssrTransform( + `export default ${source}`, + null, + '/test.json', + ) + expect(json?.code.length).toMatchInlineSnapshot(`61`) +}) diff --git a/playground/json/__tests__/ssr/json-ssr.spec.ts b/playground/json/__tests__/ssr/json-ssr.spec.ts deleted file mode 100644 index 374e7ca027c6fe..00000000000000 --- a/playground/json/__tests__/ssr/json-ssr.spec.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { beforeEach, test } from 'vitest' -import { port } from './serve' -import { page, untilUpdated } from '~utils' - -const url = `http://localhost:${port}` - -beforeEach(async () => { - await page.goto(url) -}) - -test('load json module', async () => { - await untilUpdated( - () => page.textContent('.fetch-json-module pre'), - 'export const hello = "hi";\n' + - 'export default {\n' + - ' hello,\n' + - '};\n', - ) -}) - -test('fs json', async () => { - await untilUpdated(() => page.textContent('.fetch-json-fs pre'), '61') -}) diff --git a/playground/json/__tests__/ssr/serve.ts b/playground/json/__tests__/ssr/serve.ts deleted file mode 100644 index 1e14e6e2b22e84..00000000000000 --- a/playground/json/__tests__/ssr/serve.ts +++ /dev/null @@ -1,35 +0,0 @@ -// this is automatically detected by playground/vitestSetup.ts and will replace -// the default e2e test serve behavior - -import path from 'node:path' -import kill from 'kill-port' -import { hmrPorts, ports, rootDir } from '~utils' - -export const port = ports.json - -export async function serve(): Promise<{ close(): Promise }> { - await kill(port) - - const { createServer } = await import(path.resolve(rootDir, 'server.js')) - const { app, vite } = await createServer(rootDir, hmrPorts.json) - - return new Promise((resolve, reject) => { - try { - const server = app.listen(port, () => { - resolve({ - // for test teardown - async close() { - await new Promise((resolve) => { - server.close(resolve) - }) - if (vite) { - await vite.close() - } - }, - }) - }) - } catch (e) { - reject(e) - } - }) -} diff --git a/playground/json/index.ssr.html b/playground/json/index.ssr.html deleted file mode 100644 index 8a4c4551a90817..00000000000000 --- a/playground/json/index.ssr.html +++ /dev/null @@ -1,23 +0,0 @@ -
- json-module: -

-  
-
-
- json-fs: -

-  
-
- - diff --git a/playground/json/package.json b/playground/json/package.json index 580e8ece86f80f..a481d8a994dc2f 100644 --- a/playground/json/package.json +++ b/playground/json/package.json @@ -7,10 +7,7 @@ "dev": "vite", "build": "vite build", "debug": "node --inspect-brk ../../packages/vite/bin/vite", - "preview": "vite preview", - "dev:ssr": "node server", - "serve:ssr": "NODE_ENV=production node server", - "debug:ssr": "node --inspect-brk server" + "preview": "vite preview" }, "devDependencies": { "@vitejs/test-json-module": "file:./json-module", diff --git a/playground/json/server.js b/playground/json/server.js deleted file mode 100644 index e36ced12f3643a..00000000000000 --- a/playground/json/server.js +++ /dev/null @@ -1,92 +0,0 @@ -// @ts-check -import fs from 'node:fs' -import path from 'node:path' -import { fileURLToPath } from 'node:url' -import express from 'express' - -const __dirname = path.dirname(fileURLToPath(import.meta.url)) -const isTest = process.env.VITEST - -export async function createServer(root = process.cwd(), hmrPort) { - const resolve = (p) => path.resolve(__dirname, p) - const app = express() - - /** - * @type {import('vite').ViteDevServer} - */ - const vite = await ( - await import('vite') - ).createServer({ - root, - logLevel: isTest ? 'error' : 'info', - server: { - middlewareMode: true, - watch: { - // During tests we edit the files too fast and sometimes chokidar - // misses change events, so enforce polling for consistency - usePolling: true, - interval: 100, - }, - hmr: { - port: hmrPort, - }, - }, - appType: 'custom', - json: { - stringify: true, - }, - }) - // use vite's connect instance as middleware - app.use(vite.middlewares) - - app.use('*all', async (req, res) => { - try { - let [url] = req.originalUrl.split('?') - if (url.endsWith('/')) url += 'index.ssr.html' - - if (url === '/json-module') { - console.time('load module') - const json = JSON.stringify(await vite.ssrLoadModule('/test.json')) - console.timeEnd('load module') - res.status(200).end('' + json.length) - return - } - - if (url === '/json-fs') { - // console.time('transform module') - const source = fs.readFileSync(path.resolve(__dirname, './test.json'), { - encoding: 'utf-8', - }) - const json = await vite.ssrTransform( - `export default ${source}`, - null, - './output.json', - ) - // console.timeEnd('transform module') - // @ts-expect-error ignore in test - res.status(200).end(String(json.code.length)) - return - } - - const htmlLoc = resolve(`.${url}`) - let html = fs.readFileSync(htmlLoc, 'utf-8') - html = await vite.transformIndexHtml(url, html) - - res.status(200).set({ 'Content-Type': 'text/html' }).end(html) - } catch (e) { - vite && vite.ssrFixStacktrace(e) - console.log(e.stack) - res.status(500).end(e.stack) - } - }) - - return { app, vite } -} - -if (!isTest) { - createServer().then(({ app }) => - app.listen(5173, () => { - console.log('http://localhost:5173') - }), - ) -}