Skip to content

Commit

Permalink
chore: add tests for tsc error parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
joris-gallot committed May 8, 2024
1 parent 206f0ae commit 0575401
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import consola from 'consola'
import type { TscError, TypestepConfig } from './types.js'
import { tscDiagnosticToTscError, uniqArray } from './utils.js'

function parseTsError(tscError: string): TscError {
export function parseTsError(tscError: string): TscError {
const regex = /([^()\n]+)\((\d+),(\d+)\):\s+error\s+(TS\d+):\s+(.*)/
const [_, file, line, column, tsCode, error] = regex.exec(tscError) || []

Expand Down
74 changes: 74 additions & 0 deletions tests/parse-ts-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { expect, test } from 'vitest'
import { parseTsError } from '../src/index.js'

test('valid tsc error', async () => {
let tscError = parseTsError('src/App.vue(153,11): error TS2322: Type \'Timeout\' is not assignable to type \'null\'.')

expect(tscError).toStrictEqual({
path: 'src/App.vue',
cursor: {
line: 153,
column: 11,
},
error: 'Type \'Timeout\' is not assignable to type \'null\'.',
tsCode: 'TS2322',
})

tscError = parseTsError('src/App.vue(203,17): error TS7006: Parameter \'e\' implicitly has an \'any\' type.')

expect(tscError).toStrictEqual({
path: 'src/App.vue',
cursor: {
line: 203,
column: 17,
},
error: 'Parameter \'e\' implicitly has an \'any\' type.',
tsCode: 'TS7006',
})

tscError = parseTsError('src/components/__tests__/SortingButton.spec.ts(25,24): error TS2571: Object is of type \'unknown\'.')

expect(tscError).toStrictEqual({
path: 'src/components/__tests__/SortingButton.spec.ts',
cursor: {
line: 25,
column: 24,
},
error: 'Object is of type \'unknown\'.',
tsCode: 'TS2571',
})

tscError = parseTsError('src/App.vue(283,7): error TS2554: Expected 7 arguments, but got 4.')

expect(tscError).toStrictEqual({
path: 'src/App.vue',
cursor: {
line: 283,
column: 7,
},
error: 'Expected 7 arguments, but got 4.',
tsCode: 'TS2554',
})
})

test('invalid tsc error', async () => {
expect(
() => parseTsError(' Property \'filter\' does not exist on type \'string\'.'),
).toThrowError('Invalid error format')

expect(
() => parseTsError(' Argument of type \'number\' is not assignable to parameter of type \'Blob\'.'),
).toThrowError('Invalid error format')

expect(
() => parseTsError(' Type \'{ id: number }\' is missing the following properties from type \'MyParam\': name, description, and 55 more.'),
).toThrowError('Invalid error format')

expect(
() => parseTsError(' Type \'{ file: string; name: string; extension: string; }\' is not assignable to type \'never\'.'),
).toThrowError('Invalid error format')

expect(
() => parseTsError(' Property \'context\' does not exist on type \'{ userId: string | string[]; translate: string | string[]; }\'.'),
).toThrowError('Invalid error format')
})

0 comments on commit 0575401

Please sign in to comment.