From 1379bdbc82f2317c1e0c2797a4dd5c17ff597068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 6 Dec 2024 13:11:29 +0900 Subject: [PATCH] test: use `readDepOptimizationMetadata` (#18900) --- .../__tests__/environment-react-ssr.spec.ts | 35 ++++++++----------- .../optimize-deps-no-discovery.spec.ts | 10 +++--- playground/test-utils.ts | 7 ++-- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/playground/environment-react-ssr/__tests__/environment-react-ssr.spec.ts b/playground/environment-react-ssr/__tests__/environment-react-ssr.spec.ts index 3bf14f6e4f5eca..2f4da3b570395d 100644 --- a/playground/environment-react-ssr/__tests__/environment-react-ssr.spec.ts +++ b/playground/environment-react-ssr/__tests__/environment-react-ssr.spec.ts @@ -2,10 +2,10 @@ import fs from 'node:fs' import path from 'node:path' import { stripVTControlCharacters } from 'node:util' import { describe, expect, onTestFinished, test } from 'vitest' -import type { DepOptimizationMetadata } from 'vite' import { isBuild, page, + readDepOptimizationMetadata, readFile, serverLogs, testDir, @@ -21,8 +21,7 @@ test('basic', async () => { describe.runIf(!isBuild)('pre-bundling', () => { test('client', async () => { - const meta = await readFile('node_modules/.vite/deps/_metadata.json') - const metaJson: DepOptimizationMetadata = JSON.parse(meta) + const metaJson = readDepOptimizationMetadata() expect(metaJson.optimized['react']).toBeTruthy() expect(metaJson.optimized['react-dom/client']).toBeTruthy() @@ -32,8 +31,7 @@ describe.runIf(!isBuild)('pre-bundling', () => { }) test('ssr', async () => { - const meta = await readFile('node_modules/.vite/deps_ssr/_metadata.json') - const metaJson: DepOptimizationMetadata = JSON.parse(meta) + const metaJson = readDepOptimizationMetadata('ssr') expect(metaJson.optimized['react']).toBeTruthy() expect(metaJson.optimized['react-dom/server']).toBeTruthy() @@ -45,17 +43,12 @@ describe.runIf(!isBuild)('pre-bundling', () => { test('deps reload', async () => { const envs = ['client', 'server'] as const - const getMeta = (env: (typeof envs)[number]): DepOptimizationMetadata => { - const meta = readFile( - `node_modules/.vite/deps${env === 'client' ? '' : '_ssr'}/_metadata.json`, - ) - return JSON.parse(meta) - } - - expect(getMeta('client').optimized['react-fake-client']).toBeFalsy() - expect(getMeta('client').optimized['react-fake-server']).toBeFalsy() - expect(getMeta('server').optimized['react-fake-server']).toBeFalsy() - expect(getMeta('server').optimized['react-fake-client']).toBeFalsy() + const clientMeta = readDepOptimizationMetadata('client') + const ssrMeta = readDepOptimizationMetadata('ssr') + expect(clientMeta.optimized['react-fake-client']).toBeFalsy() + expect(clientMeta.optimized['react-fake-server']).toBeFalsy() + expect(ssrMeta.optimized['react-fake-server']).toBeFalsy() + expect(ssrMeta.optimized['react-fake-client']).toBeFalsy() envs.forEach((env) => { const filePath = path.resolve(testDir, `src/entry-${env}.tsx`) @@ -84,9 +77,11 @@ describe.runIf(!isBuild)('pre-bundling', () => { 'react-fake-server, react-fake-client', ) - expect(getMeta('client').optimized['react-fake-client']).toBeTruthy() - expect(getMeta('client').optimized['react-fake-server']).toBeFalsy() - expect(getMeta('server').optimized['react-fake-server']).toBeTruthy() - expect(getMeta('server').optimized['react-fake-client']).toBeFalsy() + const clientMetaNew = readDepOptimizationMetadata('client') + const ssrMetaNew = readDepOptimizationMetadata('ssr') + expect(clientMetaNew.optimized['react-fake-client']).toBeTruthy() + expect(clientMetaNew.optimized['react-fake-server']).toBeFalsy() + expect(ssrMetaNew.optimized['react-fake-server']).toBeTruthy() + expect(ssrMetaNew.optimized['react-fake-client']).toBeFalsy() }) }) diff --git a/playground/optimize-deps-no-discovery/__tests__/optimize-deps-no-discovery.spec.ts b/playground/optimize-deps-no-discovery/__tests__/optimize-deps-no-discovery.spec.ts index 12bf4e5b57efaa..19826f82d59a9f 100644 --- a/playground/optimize-deps-no-discovery/__tests__/optimize-deps-no-discovery.spec.ts +++ b/playground/optimize-deps-no-discovery/__tests__/optimize-deps-no-discovery.spec.ts @@ -1,5 +1,5 @@ import { expect, test } from 'vitest' -import { isBuild, page, readFile } from '~utils' +import { isBuild, page, readDepOptimizationMetadata } from '~utils' test('optimized dep', async () => { expect(await page.textContent('.optimized-dep')).toBe('[success]') @@ -10,8 +10,8 @@ test('vue + vuex', async () => { }) test.runIf(!isBuild)('metadata', async () => { - const meta = await readFile('node_modules/.vite/deps/_metadata.json') - expect(meta).toMatch(`"@vitejs/test-dep-no-discovery"`) - expect(meta).not.toMatch(`"vue"`) - expect(meta).not.toMatch(`"vuex"`) + const meta = readDepOptimizationMetadata() + expect(Object.keys(meta.optimized)).toContain('@vitejs/test-dep-no-discovery') + expect(Object.keys(meta.optimized)).not.toContain('vue') + expect(Object.keys(meta.optimized)).not.toContain('vuex') }) diff --git a/playground/test-utils.ts b/playground/test-utils.ts index 5607aec8e6f8ba..8d58160b53c33e 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -201,10 +201,13 @@ export function readManifest(base = ''): Manifest { ) } -export function readDepOptimizationMetadata(): DepOptimizationMetadata { +export function readDepOptimizationMetadata( + environmentName = 'client', +): DepOptimizationMetadata { + const suffix = environmentName === 'client' ? '' : `_${environmentName}` return JSON.parse( fs.readFileSync( - path.join(testDir, 'node_modules/.vite/deps/_metadata.json'), + path.join(testDir, `node_modules/.vite/deps${suffix}/_metadata.json`), 'utf-8', ), )