From c0c3f0985dde591491901c9a8872cf230bc8760b Mon Sep 17 00:00:00 2001 From: Misha Kaletsky <15040698+mmkal@users.noreply.github.com> Date: Sun, 23 Oct 2022 19:40:59 +0000 Subject: [PATCH] Snapshot test what compiler errors look like --- package.json | 2 ++ pnpm-lock.yaml | 68 +++++++++++++++++++++++++++++++++++++++------ test/errors.test.ts | 41 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 test/errors.test.ts diff --git a/package.json b/package.json index aeac648..01acada 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c1e4393..994d54e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,20 +2,24 @@ lockfileVersion: 5.4 specifiers: '@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 devDependencies: '@types/jest': 29.0.0 + '@types/node': 14.18.32 eslint: 8.23.0 eslint-plugin-mmkal: 0.0.1-2_njvpd7oexm4jrpqkdx4xx3mxbm - jest: 28.1.3 + jest: 28.1.3_@types+node@14.18.32 np: 7.6.2 ts-jest: 28.0.8_556mfp7b5dutuj2jcrj5i7zc5q + ts-morph: 16.0.0 typescript: 4.8.2 packages: @@ -924,6 +928,15 @@ packages: defer-to-connect: 2.0.1 dev: true + /@ts-morph/common/0.17.0: + resolution: {integrity: sha512-RMSSvSfs9kb0VzkvQ2NWobwnj7TxCA9vI/IjR9bDHqgAyVbu2T0DN4wiKVqomyDWqO7dPr/tErSfq7urQ1Q37g==} + dependencies: + fast-glob: 3.2.12 + minimatch: 5.1.0 + mkdirp: 1.0.4 + path-browserify: 1.0.1 + dev: true + /@types/babel__core/7.1.19: resolution: {integrity: sha512-WEOTgRsbYkvA/KCsDwVEGkd7WAr1e3g31VHQ8zy5gul/V1qKullU/BU5I68X5v7V3GnB9eotmom4v5a5gjxorw==} dependencies: @@ -1024,6 +1037,10 @@ packages: resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} dev: true + /@types/node/14.18.32: + resolution: {integrity: sha512-Y6S38pFr04yb13qqHf8uk1nHE3lXgQ30WZbv1mLliV9pt0NjvqdWttLcrOYLnXbOafknVYRHZGoMSpR9UwfYow==} + dev: true + /@types/node/18.11.3: resolution: {integrity: sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==} dev: true @@ -1715,6 +1732,12 @@ packages: concat-map: 0.0.1 dev: true + /brace-expansion/2.0.1: + resolution: {integrity: sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==} + dependencies: + balanced-match: 1.0.2 + dev: true + /braces/3.0.2: resolution: {integrity: sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==} engines: {node: '>=8'} @@ -1945,6 +1968,10 @@ packages: engines: {iojs: '>= 1.0.0', node: '>= 0.12.0'} dev: true + /code-block-writer/11.0.3: + resolution: {integrity: sha512-NiujjUFB4SwScJq2bwbYUtXbZhBSlY6vYzm++3Q6oC+U+injTqfPYFK8wS9COOmb2lueqp0ZRB4nK1VYeHgNyw==} + dev: true + /code-point-at/1.1.0: resolution: {integrity: sha512-RpAVKQA5T63xEj6/giIbUEtZwJ4UFIc3ZtvEkiaUERylqe8xb5IvqcgOurZLahv93CLKfxcw5YI+DZcUBRyLXA==} engines: {node: '>=0.10.0'} @@ -2486,7 +2513,7 @@ packages: '@typescript-eslint/eslint-plugin': 5.40.1_ghcq2hxpzlnfizbnoz3v5uflw4 '@typescript-eslint/utils': 5.40.1_yqf6kl63nyoq5megxukfnom5rm eslint: 8.23.0 - jest: 28.1.3 + jest: 28.1.3_@types+node@14.18.32 transitivePeerDependencies: - supports-color - typescript @@ -3739,7 +3766,7 @@ packages: - supports-color dev: true - /jest-cli/28.1.3: + /jest-cli/28.1.3_@types+node@14.18.32: resolution: {integrity: sha512-roY3kvrv57Azn1yPgdTebPAXvdR2xfezaKKYzVxZ6It/5NCxzJym6tUI5P1zkdWhfUYkxEI9uZWcQdaFLo8mJQ==} engines: {node: ^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0} hasBin: true @@ -3756,7 +3783,7 @@ packages: exit: 0.1.2 graceful-fs: 4.2.10 import-local: 3.1.0 - jest-config: 28.1.3 + jest-config: 28.1.3_@types+node@14.18.32 jest-util: 28.1.3 jest-validate: 28.1.3 prompts: 2.4.2 @@ -3767,7 +3794,7 @@ packages: - ts-node dev: true - /jest-config/28.1.3: + /jest-config/28.1.3_@types+node@14.18.32: resolution: {integrity: sha512-MG3INjByJ0J4AsNBm7T3hsuxKQqFIiRo/AUqb1q9LRKI5UU6Aar9JHbr9Ivn1TVwfUD9KirRoM/T6u8XlcQPHQ==} engines: {node: ^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0} peerDependencies: @@ -3782,6 +3809,7 @@ packages: '@babel/core': 7.19.6 '@jest/test-sequencer': 28.1.3 '@jest/types': 28.1.3 + '@types/node': 14.18.32 babel-jest: 28.1.3_@babel+core@7.19.6 chalk: 4.1.2 ci-info: 3.5.0 @@ -4225,7 +4253,7 @@ packages: supports-color: 8.1.1 dev: true - /jest/28.1.3: + /jest/28.1.3_@types+node@14.18.32: resolution: {integrity: sha512-N4GT5on8UkZgH0O5LUavMRV1EDEhNTL0KEfRmDIeZHSV7p2XgLoY9t9VDUgL6o+yfdgYHVxuz81G8oB9VG5uyA==} engines: {node: ^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0} hasBin: true @@ -4238,7 +4266,7 @@ packages: '@jest/core': 28.1.3 '@jest/types': 28.1.3 import-local: 3.1.0 - jest-cli: 28.1.3 + jest-cli: 28.1.3_@types+node@14.18.32 transitivePeerDependencies: - '@types/node' - supports-color @@ -4617,6 +4645,13 @@ packages: brace-expansion: 1.1.11 dev: true + /minimatch/5.1.0: + resolution: {integrity: sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==} + engines: {node: '>=10'} + dependencies: + brace-expansion: 2.0.1 + dev: true + /minimist-options/4.1.0: resolution: {integrity: sha512-Q4r8ghd80yhO/0j1O3B2BjweX3fiHg9cdOwjJd2J76Q135c+NDxGCqdYKQ1SKBuFfgWbAUzBfvYjPUEeNgqN1A==} engines: {node: '>= 6'} @@ -4630,6 +4665,12 @@ packages: resolution: {integrity: sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==} dev: true + /mkdirp/1.0.4: + resolution: {integrity: sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==} + engines: {node: '>=10'} + hasBin: true + dev: true + /ms/2.0.0: resolution: {integrity: sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==} dev: true @@ -5039,6 +5080,10 @@ packages: lines-and-columns: 1.2.4 dev: true + /path-browserify/1.0.1: + resolution: {integrity: sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==} + dev: true + /path-exists/4.0.0: resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==} engines: {node: '>=8'} @@ -5788,7 +5833,7 @@ packages: dependencies: bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 28.1.3 + jest: 28.1.3_@types+node@14.18.32 jest-util: 28.1.3 json5: 2.2.1 lodash.memoize: 4.1.2 @@ -5798,6 +5843,13 @@ packages: yargs-parser: 21.1.1 dev: true + /ts-morph/16.0.0: + resolution: {integrity: sha512-jGNF0GVpFj0orFw55LTsQxVYEUOCWBAbR5Ls7fTYE5pQsbW18ssTb/6UXx/GYAEjS+DQTp8VoTw0vqYMiaaQuw==} + dependencies: + '@ts-morph/common': 0.17.0 + code-block-writer: 11.0.3 + dev: true + /tsconfig-paths/3.14.1: resolution: {integrity: sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==} dependencies: diff --git a/test/errors.test.ts b/test/errors.test.ts new file mode 100644 index 0000000..c200a9e --- /dev/null +++ b/test/errors.test.ts @@ -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.", + ] + `) +})