Skip to content

Commit

Permalink
test: use readDepOptimizationMetadata (#18900)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 6, 2024
1 parent e5f5301 commit 1379bdb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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`)
Expand Down Expand Up @@ -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()
})
})
Original file line number Diff line number Diff line change
@@ -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]')
Expand All @@ -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')
})
7 changes: 5 additions & 2 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
)
Expand Down

0 comments on commit 1379bdb

Please sign in to comment.