From f988ee1abc4156228bb6536ed977af1452b8247a Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Sat, 4 Jul 2020 13:55:55 +0200 Subject: [PATCH] Add support for React.ComponentType in `expectError` assertion - fixes #42 --- source/lib/compiler.ts | 7 ++++--- source/lib/interfaces.ts | 3 ++- source/test/fixtures/tsx/component-type/index.d.ts | 8 ++++++++ .../test/fixtures/tsx/{ => component-type}/index.js | 0 .../fixtures/tsx/component-type/index.test-d.tsx | 9 +++++++++ .../fixtures/tsx/{ => component-type}/package.json | 0 source/test/fixtures/tsx/{ => component}/index.d.ts | 1 + source/test/fixtures/tsx/component/index.js | 12 ++++++++++++ source/test/fixtures/tsx/component/index.test-d.tsx | 9 +++++++++ source/test/fixtures/tsx/component/package.json | 10 ++++++++++ source/test/fixtures/tsx/index.test-d.tsx | 7 ------- source/test/test.ts | 10 ++++++++-- 12 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 source/test/fixtures/tsx/component-type/index.d.ts rename source/test/fixtures/tsx/{ => component-type}/index.js (100%) create mode 100644 source/test/fixtures/tsx/component-type/index.test-d.tsx rename source/test/fixtures/tsx/{ => component-type}/package.json (100%) rename source/test/fixtures/tsx/{ => component}/index.d.ts (88%) create mode 100644 source/test/fixtures/tsx/component/index.js create mode 100644 source/test/fixtures/tsx/component/index.test-d.tsx create mode 100644 source/test/fixtures/tsx/component/package.json delete mode 100644 source/test/fixtures/tsx/index.test-d.tsx diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index 05a1c783..c56a28c8 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -16,14 +16,15 @@ const ignoredDiagnostics = new Set([ ]); // List of diagnostic codes which should be ignored inside `expectError` statements -const diagnosticCodesToIgnore = new Set([ +const expectErrordiagnosticCodesToIgnore = new Set([ DiagnosticCode.ArgumentTypeIsNotAssignableToParameterType, DiagnosticCode.PropertyDoesNotExistOnType, DiagnosticCode.CannotAssignToReadOnlyProperty, DiagnosticCode.TypeIsNotAssignableToOtherType, DiagnosticCode.GenericTypeRequiresTypeArguments, DiagnosticCode.ExpectedArgumentsButGotOther, - DiagnosticCode.NoOverloadMatches + DiagnosticCode.NoOverloadMatches, + DiagnosticCode.PropertyMissingInType1ButRequiredInType2 ]); /** @@ -39,7 +40,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map; diff --git a/source/test/fixtures/tsx/index.js b/source/test/fixtures/tsx/component-type/index.js similarity index 100% rename from source/test/fixtures/tsx/index.js rename to source/test/fixtures/tsx/component-type/index.js diff --git a/source/test/fixtures/tsx/component-type/index.test-d.tsx b/source/test/fixtures/tsx/component-type/index.test-d.tsx new file mode 100644 index 00000000..9a566007 --- /dev/null +++ b/source/test/fixtures/tsx/component-type/index.test-d.tsx @@ -0,0 +1,9 @@ +import * as React from 'react'; +import {expectType, expectError} from '../../../..'; +import {Unicorn} from '.'; + +expectType(); + +expectError(); +expectError(); +expectError(); diff --git a/source/test/fixtures/tsx/package.json b/source/test/fixtures/tsx/component-type/package.json similarity index 100% rename from source/test/fixtures/tsx/package.json rename to source/test/fixtures/tsx/component-type/package.json diff --git a/source/test/fixtures/tsx/index.d.ts b/source/test/fixtures/tsx/component/index.d.ts similarity index 88% rename from source/test/fixtures/tsx/index.d.ts rename to source/test/fixtures/tsx/component/index.d.ts index 74067858..23bacd5a 100644 --- a/source/test/fixtures/tsx/index.d.ts +++ b/source/test/fixtures/tsx/component/index.d.ts @@ -1,6 +1,7 @@ import {Component} from 'react'; interface UnicornProps { + unicorn: number; rainbow: string; } diff --git a/source/test/fixtures/tsx/component/index.js b/source/test/fixtures/tsx/component/index.js new file mode 100644 index 00000000..6db2eca6 --- /dev/null +++ b/source/test/fixtures/tsx/component/index.js @@ -0,0 +1,12 @@ +'use strict'; +const React = require('react'); + +export class Unicorn extends React.Component { + constructor(props) { + super(props); + } + + render() { + return

{this.props.rainbow}

; + } +} diff --git a/source/test/fixtures/tsx/component/index.test-d.tsx b/source/test/fixtures/tsx/component/index.test-d.tsx new file mode 100644 index 00000000..9a566007 --- /dev/null +++ b/source/test/fixtures/tsx/component/index.test-d.tsx @@ -0,0 +1,9 @@ +import * as React from 'react'; +import {expectType, expectError} from '../../../..'; +import {Unicorn} from '.'; + +expectType(); + +expectError(); +expectError(); +expectError(); diff --git a/source/test/fixtures/tsx/component/package.json b/source/test/fixtures/tsx/component/package.json new file mode 100644 index 00000000..09392033 --- /dev/null +++ b/source/test/fixtures/tsx/component/package.json @@ -0,0 +1,10 @@ +{ + "name": "foo", + "files": [ + "index.js", + "index.d.ts" + ], + "dependencies": { + "react": "*" + } +} diff --git a/source/test/fixtures/tsx/index.test-d.tsx b/source/test/fixtures/tsx/index.test-d.tsx deleted file mode 100644 index afa2157e..00000000 --- a/source/test/fixtures/tsx/index.test-d.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import * as React from 'react'; -import {expectType, expectError} from '../../..'; -import {Unicorn} from '.'; - -expectType(); - -expectError(); diff --git a/source/test/test.ts b/source/test/test.ts index 2e9af6b7..f7462ec4 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -202,8 +202,14 @@ test('missing import', async t => { ]); }); -test('tsx', async t => { - const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/tsx')}); +test('tsx component', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/tsx/component')}); + + verify(t, diagnostics, []); +}); + +test('tsx component type', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/tsx/component-type')}); verify(t, diagnostics, []); });