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

feat: display error frame if present (like Vite) #2257

Merged
merged 6 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro

if (type)
printErrorType(type, ctx)

printErrorMessage(e, ctx.logger)
printStack(ctx, stacks, nearest, errorProperties, (s, pos) => {
if (showCodeFrame && s === nearest && nearest) {
const file = fileFromParsedStack(nearest)
// could point to non-existing original file
// for example, when there is a source map file, but no source in node_modules
if (existsSync(file)) {
const sourceCode = readFileSync(file, 'utf-8')
ctx.logger.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)))
if (e.frame) {
ctx.logger.log(c.yellow(e.frame))
}
else {
printStack(ctx, stacks, nearest, errorProperties, (s, pos) => {
if (showCodeFrame && s === nearest && nearest) {
const file = fileFromParsedStack(nearest)
// could point to non-existing original file
// for example, when there is a source map file, but no source in node_modules
if (existsSync(file)) {
const sourceCode = readFileSync(file, 'utf-8')
ctx.logger.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)))
}
}
}
})
})
}

if (e.cause && 'name' in e.cause) {
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ErrorWithDiff extends Error {
expected?: any
operator?: string
type?: string
frame?: string
}

export interface ModuleGraphData {
Expand Down
4 changes: 4 additions & 0 deletions test/stacktraces/fixtures/frame.spec.imba
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
describe "Test" do
test("1+1") do
expect(1+1).toBe 2
ciyet.
16 changes: 16 additions & 0 deletions test/stacktraces/fixtures/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ export default defineConfig({
plugins: [{
name: 'vite-plugin-imba',
transform(code, id) {
if (id.endsWith('frame.spec.imba')) {
// eslint-disable-next-line no-throw-literal
throw {
name: 'imba-parser error',
id,
message: 'Unexpected \'CALL_END\'',
code,
frame:
'4 | test("1+1") do\n5 | expect(1+1).toBe 2\n6 | frame.\n | ^\n7 |\n',
loc: {
line: 3,
column: 11,
file: id,
},
}
}
if (id.endsWith('.imba')) {
return {
code:
Expand Down
11 changes: 11 additions & 0 deletions test/stacktraces/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Vitest Snapshot v1

exports[`stacktraces should pick error frame if present > frame.spec.imba > frame.spec.imba 1`] = `
" ❯ frame.spec.imba (0 test)

4 | test(\\"1+1\\") do
5 | expect(1+1).toBe 2
6 | frame.
| ^
7 |
"
`;

exports[`stacktraces should respect sourcemaps > add.test.ts > add.test.ts 1`] = `
" ❯ add.test.ts:12:23
10|
Expand Down
32 changes: 32 additions & 0 deletions test/stacktraces/test/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,35 @@ describe('stacktraces should respect sourcemaps', async () => {
}, 30000)
}
})

describe('stacktraces should pick error frame if present', async () => {
const root = resolve(__dirname, '../fixtures')
const files = ['frame.spec.imba']

for (const file of files) {
it(file, async () => {
// in Windows child_process is very unstable, we skip testing it
if (process.platform === 'win32' && process.env.CI)
return

let error: any
await execa('npx', ['vitest', 'run', file], {
cwd: root,
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
})
.catch((e) => {
error = e
})

expect(error).toBeTruthy()
const lines = String(error).split(/\n/g)
const index = lines.findIndex(val => val.includes('(0 test)'))
const msg = lines.slice(index, index + 8).join('\n')
expect(msg).toMatchSnapshot(file)
}, 30000)
}
})