Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ssr): failed ssrLoadModule call throws same error (#7177)
Co-authored-by: Rich Harris <hello@rich-harris.dev>
- Loading branch information
1 parent
aebaf66
commit 891e7fc
Showing
4 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/vite/src/node/ssr/__tests__/fixtures/ssrModuleLoader-bad.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const bad = 1 | ||
throw new Error('it is an expected error') |
29 changes: 29 additions & 0 deletions
29
packages/vite/src/node/ssr/__tests__/ssrModuleLoader.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { resolve } from 'path' | ||
import { createServer } from '../../index' | ||
|
||
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) | ||
expect(expectedErrors[0]).toBe(expectedErrors[1]) | ||
expectedErrors.forEach((error) => { | ||
expect(error?.message).toContain(THROW_MESSAGE) | ||
}) | ||
expect(spy).toBeCalledTimes(1) | ||
const [firstParameter] = spy.mock.calls[0] | ||
expect(firstParameter).toContain('Error when evaluating SSR module') | ||
expect(firstParameter).toContain(THROW_MESSAGE) | ||
spy.mockClear() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters