Skip to content

Commit

Permalink
Snapshot test what compiler errors look like
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Oct 23, 2022
1 parent 736c2bf commit c0c3f09
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
},
"devDependencies": {
"@types/jest": "29.0.0",
"@types/node": "^14.0.0",
"eslint": "8.23.0",
"eslint-plugin-mmkal": "0.0.1-2",
"jest": "28.1.3",
"np": "7.6.2",
"ts-jest": "28.0.8",
"ts-morph": "16.0.0",
"typescript": "4.8.2"
}
}
68 changes: 60 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as tsmorph from 'ts-morph'

const tsErrors = (code: string) => {
const project = new tsmorph.Project()
project.addSourceFileAtPath('./src/index.ts')
project.createSourceFile('test.ts', `import {expectTypeOf} from './src'\n\n${code}`)
const diagnostics = project.getPreEmitDiagnostics()
return diagnostics.map(d => d.getMessageText())
}

test('toEqualTypeOf<...>() error message', async () => {
expect(tsErrors(`expectTypeOf({a: 1}).toEqualTypeOf<{a: string}>()`)).toMatchInlineSnapshot(`
Array [
"Expected 1 arguments, but got 0.",
]
`)
})

test('toEqualTypeOf(...) error message', async () => {
expect(tsErrors(`expectTypeOf({a: 1}).toEqualTypeOf({a: 'one'})`)).toMatchInlineSnapshot(`
Array [
"Argument of type '{ a: string; }' is not assignable to parameter of type 'never'.",
]
`)
})

test('toMatchTypeOf<...>() error message', async () => {
expect(tsErrors(`expectTypeOf({a: 1}).toMatchTypeOf<{a: string}>()`)).toMatchInlineSnapshot(`
Array [
"Expected 1 arguments, but got 0.",
]
`)
})

test('toMatchTypeOf(...) error message', async () => {
expect(tsErrors(`expectTypeOf({a: 1}).toMatchTypeOf({a: 'one'})`)).toMatchInlineSnapshot(`
Array [
"Expected 2 arguments, but got 1.",
]
`)
})

0 comments on commit c0c3f09

Please sign in to comment.