Skip to content

Commit

Permalink
Add support for React.ComponentType in expectError assertion - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 4, 2020
1 parent 51fba2c commit f988ee1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 13 deletions.
7 changes: 4 additions & 3 deletions source/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ const ignoredDiagnostics = new Set<number>([
]);

// List of diagnostic codes which should be ignored inside `expectError` statements
const diagnosticCodesToIgnore = new Set<DiagnosticCode>([
const expectErrordiagnosticCodesToIgnore = new Set<DiagnosticCode>([
DiagnosticCode.ArgumentTypeIsNotAssignableToParameterType,
DiagnosticCode.PropertyDoesNotExistOnType,
DiagnosticCode.CannotAssignToReadOnlyProperty,
DiagnosticCode.TypeIsNotAssignableToOtherType,
DiagnosticCode.GenericTypeRequiresTypeArguments,
DiagnosticCode.ExpectedArgumentsButGotOther,
DiagnosticCode.NoOverloadMatches
DiagnosticCode.NoOverloadMatches,
DiagnosticCode.PropertyMissingInType1ButRequiredInType2
]);

/**
Expand All @@ -39,7 +40,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map<Location
return true;
}

if (!diagnosticCodesToIgnore.has(diagnostic.code)) {
if (!expectErrordiagnosticCodesToIgnore.has(diagnostic.code)) {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion source/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export enum DiagnosticCode {
ArgumentTypeIsNotAssignableToParameterType = 2345,
CannotAssignToReadOnlyProperty = 2540,
ExpectedArgumentsButGotOther = 2554,
NoOverloadMatches = 2769
NoOverloadMatches = 2769,
PropertyMissingInType1ButRequiredInType2 = 2741
}

export interface Diagnostic {
Expand Down
8 changes: 8 additions & 0 deletions source/test/fixtures/tsx/component-type/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ComponentType} from 'react';

interface UnicornProps {
unicorn: number;
rainbow: string;
}

export const Unicorn: ComponentType<UnicornProps>;
File renamed without changes.
9 changes: 9 additions & 0 deletions source/test/fixtures/tsx/component-type/index.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';
import {expectType, expectError} from '../../../..';
import {Unicorn} from '.';

expectType<JSX.Element>(<Unicorn unicorn={1} rainbow='🌈' />);

expectError(<Unicorn foo='bar' />);
expectError(<Unicorn unicorn={1} />);
expectError(<Unicorn rainbow='bar' />);
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Component} from 'react';

interface UnicornProps {
unicorn: number;
rainbow: string;
}

Expand Down
12 changes: 12 additions & 0 deletions source/test/fixtures/tsx/component/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const React = require('react');

export class Unicorn extends React.Component {
constructor(props) {
super(props);
}

render() {
return <h1>{this.props.rainbow}</h1>;
}
}
9 changes: 9 additions & 0 deletions source/test/fixtures/tsx/component/index.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';
import {expectType, expectError} from '../../../..';
import {Unicorn} from '.';

expectType<JSX.Element>(<Unicorn unicorn={1} rainbow='🌈' />);

expectError(<Unicorn foo='bar' />);
expectError(<Unicorn unicorn={1} />);
expectError(<Unicorn rainbow='bar' />);
10 changes: 10 additions & 0 deletions source/test/fixtures/tsx/component/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "foo",
"files": [
"index.js",
"index.d.ts"
],
"dependencies": {
"react": "*"
}
}
7 changes: 0 additions & 7 deletions source/test/fixtures/tsx/index.test-d.tsx

This file was deleted.

10 changes: 8 additions & 2 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
});
Expand Down

0 comments on commit f988ee1

Please sign in to comment.