| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ import { createIndexLocationsMap } from '../utils/base' | |||
| 12 | 12 | import { createDebugger } from '../utils/debugger' | |
| 13 | 13 | import { calculateSuiteHash, createFileTask as createFileTaskOriginal, createTaskName } from '../utils/tasks' | |
| 14 | 14 | import { detectCodeBlock } from '../utils/test-helpers' | |
| 15 | + import { toRollupError } from './environments/fetchModule' | ||
| 15 | 16 | ||
| 16 | 17 | interface ParsedFile extends File { | |
| 17 | 18 | start: number | |
@@ -323,28 +324,16 @@ export function createFailedFileTask(project: TestProject, filepath: string, err | |||
| 323 | 324 | end: 0, | |
| 324 | 325 | result: { | |
| 325 | 326 | state: 'fail', | |
| 326 | - errors: serializeError(project, error), | ||
| 327 | + errors: serializeError(error), | ||
| 327 | 328 | }, | |
| 328 | 329 | } | |
| 329 | 330 | file.file = file | |
| 330 | 331 | return file | |
| 331 | 332 | } | |
| 332 | 333 | ||
| 333 | - function serializeError(ctx: TestProject, error: any): TestError[] { | ||
| 334 | - if ('errors' in error && 'pluginCode' in error) { | ||
| 335 | - const errors = error.errors.map((e: any) => { | ||
| 336 | - return { | ||
| 337 | - name: error.name, | ||
| 338 | - message: e.text, | ||
| 339 | - stack: e.location | ||
| 340 | - ? `${error.name}: ${e.text}\n at ${relative(ctx.config.root, e.location.file)}:${e.location.line}:${e.location.column}` | ||
| 341 | - : '', | ||
| 342 | - } | ||
| 343 | - }) | ||
| 344 | - return errors | ||
| 345 | - } | ||
| 334 | + function serializeError(error: any): TestError[] { | ||
| 346 | 335 | return [ | |
| 347 | - { | ||
| 336 | + toRollupError(error) ?? { | ||
| 348 | 337 | name: error.name, | |
| 349 | 338 | stack: error.stack, | |
| 350 | 339 | message: error.message, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ import type { Span } from '@opentelemetry/api' | |||
| 2 | 2 | import type { StaticMockCall } from '@vitest/mocker/node' | |
| 3 | 3 | import type { DevEnvironment, EnvironmentModuleNode, Rollup, TransformResult } from 'vite' | |
| 4 | 4 | import type { FetchFunctionOptions, FetchResult } from 'vite/module-runner' | |
| 5 | - import type { FetchCachedFileSystemResult, ModuleType, VitestFetchResult } from '../../types/general' | ||
| 5 | + import type { FetchCachedFileSystemResult, ModuleType, TestError, VitestFetchResult } from '../../types/general' | ||
| 6 | 6 | import type { OTELCarrier, Traces } from '../../utils/traces' | |
| 7 | 7 | import type { FileSystemModuleCache } from '../cache/fsModuleCache' | |
| 8 | 8 | import type { VitestResolver } from '../resolver' | |
@@ -575,17 +575,16 @@ function extractSourceMap(code: string): null | Rollup.SourceMap { | |||
| 575 | 575 | } | |
| 576 | 576 | ||
| 577 | 577 | // serialize rollup error on server to preserve details as a test error | |
| 578 | - export function handleRollupError(e: unknown): never { | ||
| 578 | + export function toRollupError(e: unknown): TestError | undefined { | ||
| 579 | 579 | if ( | |
| 580 | 580 | e instanceof Error | |
| 581 | 581 | && ('plugin' in e || 'frame' in e || 'id' in e) | |
| 582 | 582 | ) { | |
| 583 | - // eslint-disable-next-line no-throw-literal | ||
| 584 | - throw { | ||
| 583 | + return { | ||
| 585 | 584 | name: e.name, | |
| 586 | 585 | message: e.message, | |
| 587 | 586 | stack: e.stack, | |
| 588 | - cause: e.cause, | ||
| 587 | + cause: e.cause as TestError | undefined, | ||
| 589 | 588 | __vitest_rollup_error__: { | |
| 590 | 589 | plugin: (e as any).plugin, | |
| 591 | 590 | id: (e as any).id, | |
@@ -594,7 +593,10 @@ export function handleRollupError(e: unknown): never { | |||
| 594 | 593 | }, | |
| 595 | 594 | } | |
| 596 | 595 | } | |
| 597 | - throw e | ||
| 596 | + } | ||
| 597 | + | ||
| 598 | + export function handleRollupError(e: unknown): never { | ||
| 599 | + throw toRollupError(e) ?? e | ||
| 598 | 600 | } | |
| 599 | 601 | ||
| 600 | 602 | declare module 'vite' { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + import { expect, it } from 'vitest' | ||
| 2 | + | ||
| 3 | + it('unterminated', () => { | ||
| 4 | + expect(1).toBe(1) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + import { expect, it } from 'vitest' | ||
| 2 | + | ||
| 3 | + it('works', () => { | ||
| 4 | + expect(1).toBe(1) | ||
| 5 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + import { defineConfig } from 'vitest/config' | ||
| 2 | + | ||
| 3 | + export default defineConfig({ | ||
| 4 | + test: { | ||
| 5 | + include: ['broken.test.ts', 'ok.test.ts'], | ||
| 6 | + }, | ||
| 7 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,15 @@ test.each([ | |||
| 38 | 38 | expect(exitCode).toBe(1) | |
| 39 | 39 | }) | |
| 40 | 40 | ||
| 41 | + test('output shows the details of a transform error', async () => { | ||
| 42 | + const { stderr, stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list-parse-error') | ||
| 43 | + expect(stdout).toBe('') | ||
| 44 | + expect(stderr).toContain('Error: Transform failed with 1 error:') | ||
| 45 | + expect(stderr).toContain('Plugin: vite:') | ||
| 46 | + expect(stderr).toContain('broken.test.ts') | ||
| 47 | + expect(exitCode).toBe(1) | ||
| 48 | + }) | ||
| 49 | + | ||
| 41 | 50 | test('correctly outputs json', async () => { | |
| 42 | 51 | const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', '--json') | |
| 43 | 52 | expect(relative(stdout)).toMatchInlineSnapshot(` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1543,6 +1543,15 @@ test('invalid @module-tag throws and error', async () => { | |||
| 1543 | 1543 | `) | |
| 1544 | 1544 | }) | |
| 1545 | 1545 | ||
| 1546 | + test('reports the details of a transform error', async () => { | ||
| 1547 | + const testModule = await collectTestModule(`it('unterminated', () => {`) | ||
| 1548 | + | ||
| 1549 | + const [error] = testModule.errors() | ||
| 1550 | + expect(error.message.split('\n')[0]).toBe('Transform failed with 1 error:') | ||
| 1551 | + expect(error.__vitest_rollup_error__).toMatchObject({ id: 'simple.test.ts' }) | ||
| 1552 | + expect((error.__vitest_rollup_error__ as any).plugin).toBeTypeOf('string') | ||
| 1553 | + }) | ||
| 1554 | + | ||
| 1546 | 1555 | test('collects tests with runIf modifier', async () => { | |
| 1547 | 1556 | const testModule = await collectTests(` | |
| 1548 | 1557 | import { test } from 'vitest' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments