Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): failed ssrLoadModule call throws same error #7177

Merged
merged 3 commits into from Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
export const bad = 1
throw new Error('it is an expected error')
29 changes: 29 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrModuleLoader.spec.ts
@@ -0,0 +1,29 @@
import { createServer } from '../../index'
import { resolve } from 'path'

const badjs = resolve(__dirname, './fixtures/ssrModuleLoader-bad.js')
const THROW_MESSAGE = 'it is an expected error'

test('always throw error when evaluating an wrong SSR module', async () => {
const viteServer = await createServer()
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const expectedErrors = []
for (const i of [0, 1]) {
try {
await viteServer.ssrLoadModule(badjs)
} catch (e) {
expectedErrors.push(e)
}
}
await viteServer.close()
expect(expectedErrors).toHaveLength(2)
expectedErrors.forEach((error) => {
expect(error?.message).toContain(THROW_MESSAGE)
})
expect(spy).toBeCalledTimes(2)
spy.mock.calls.forEach(([info]) => {
expect(info).toContain('Error when evaluating SSR module')
expect(info).toContain(THROW_MESSAGE)
})
spy.mockClear()
})
1 change: 1 addition & 0 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -202,6 +202,7 @@ async function instantiateModule(
ssrExportAll
)
} catch (e) {
mod.ssrModule = null
if (e.stack && fixStacktrace !== false) {
const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph)
rebindErrorStacktrace(e, stacktrace)
Expand Down