Skip to content

Commit

Permalink
test: ssr external / resolveId test (vitejs#18327)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 11, 2024
1 parent 18f1dad commit 4c5cf91
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/vite/src/node/__tests__/environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import path from 'node:path'
import { describe, expect, onTestFinished, test } from 'vitest'
import type { RollupOutput } from 'rollup'
import { createServer } from '../server'
import { defineConfig } from '../config'
import type { InlineConfig } from '../config'
import { createBuilder } from '../build'
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner'

describe('custom environment conditions', () => {
function getConfig() {
return defineConfig({
function getConfig(): InlineConfig {
return {
configFile: false,
root: import.meta.dirname,
logLevel: 'error',
server: {
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('custom environment conditions', () => {
},
},
},
})
}
}

test('dev', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import dep from '@vitejs/test-dep-conditions/with-module'
export default dep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'dir/index.default.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'dir/index.module.js'
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"worker": "./index.worker.js",
"browser": "./index.browser.js",
"default": "./index.default.js"
},
"./with-module": {
"module": "./dir/index.module.js",
"default": "./dir/index.default.js"
}
}
}
52 changes: 52 additions & 0 deletions packages/vite/src/node/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, onTestFinished, test } from 'vitest'
import { createServer } from '../server'
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner'

describe('import and resolveId', () => {
async function createTestServer() {
const server = await createServer({
configFile: false,
root: import.meta.dirname,
logLevel: 'error',
server: {
middlewareMode: true,
},
})
onTestFinished(() => server.close())
const runner = createServerModuleRunner(server.environments.ssr, {
hmr: {
logger: false,
},
sourcemapInterceptor: false,
})
return { server, runner }
}

test('import first', async () => {
const { server, runner } = await createTestServer()
const mod = await runner.import(
'/fixtures/test-dep-conditions-app/entry-with-module',
)
const resolved = await server.environments.ssr.pluginContainer.resolveId(
'@vitejs/test-dep-conditions/with-module',
)
expect([mod.default, resolved?.id]).toEqual([
'dir/index.default.js',
expect.stringContaining('dir/index.module.js'),
])
})

test('resolveId first', async () => {
const { server, runner } = await createTestServer()
const resolved = await server.environments.ssr.pluginContainer.resolveId(
'@vitejs/test-dep-conditions/with-module',
)
const mod = await runner.import(
'/fixtures/test-dep-conditions-app/entry-with-module',
)
expect([mod.default, resolved?.id]).toEqual([
'dir/index.default.js',
expect.stringContaining('dir/index.module.js'),
])
})
})

0 comments on commit 4c5cf91

Please sign in to comment.