Skip to content

Commit

Permalink
Add support for JSX - fixes #15 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored Sep 19, 2019
1 parent 03c170b commit c445879
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
},
"devDependencies": {
"@types/node": "^11.10.4",
"@types/react": "^16.9.2",
"@types/update-notifier": "^2.2.0",
"ava": "^1.4.1",
"cpy-cli": "^2.0.0",
"del-cli": "^1.1.0",
"react": "^16.9.0",
"tslint": "^5.11.0",
"tslint-xo": "^0.9.0"
}
Expand Down
14 changes: 7 additions & 7 deletions source/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ const findTypingsFile = async (pkg: any, options: Options) => {

const findTestFiles = async (typingsFile: string, options: Options & {config: Config}) => {
const testFile = typingsFile.replace(/\.d\.ts$/, '.test-d.ts');
const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx');
const testDir = options.config.directory;

const testFileExists = await pathExists(path.join(options.cwd, testFile));
let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd});

const testDirExists = await pathExists(path.join(options.cwd, testDir));

if (!testFileExists && !testDirExists) {
throw new Error(`The test file \`${testFile}\` does not exist. Create one and try again.`);
if (testFiles.length === 0 && !testDirExists) {
throw new Error(`The test file \`${testFile}\` or \`${tsxTestFile}\` does not exist. Create one and try again.`);
}

let testFiles = [testFile];

if (!testFileExists) {
testFiles = await globby(`${testDir}/**/*.ts`, {cwd: options.cwd});
if (testFiles.length === 0) {
testFiles = await globby([`${testDir}/**/*.ts`, `${testDir}/**/*.tsx`], {cwd: options.cwd});
}

return testFiles;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare const document: Document;
declare const window: Window;

export default document;
export default window;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports.default = window.document;
module.exports.default = window;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from '../../../..';
import document from '.';
import window from '.';

expectType<Document>(document);
expectType<Window>(window);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference lib="dom"/>

declare const document: Document;
declare const window: Window;

export default document;
export default window;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports.default = window.document;
module.exports.default = window;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from '../../../..';
import document from '.';
import window from '.';

expectType<Document>(document);
expectType<Window>(window);
7 changes: 7 additions & 0 deletions source/test/fixtures/test-directory/tsx/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Component} from 'react';

interface UnicornProps {
rainbow: string;
}

export class Unicorn extends Component<UnicornProps> {}
12 changes: 12 additions & 0 deletions source/test/fixtures/test-directory/tsx/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>;
}
}
10 changes: 10 additions & 0 deletions source/test/fixtures/test-directory/tsx/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: 7 additions & 0 deletions source/test/fixtures/test-directory/tsx/test-d/unicorn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';
import {expectType, expectError} from '../../../../..';
import {Unicorn} from '..';

expectType<JSX.Element>(<Unicorn rainbow='🌈' />);

expectError(<Unicorn foo='bar' />);
7 changes: 7 additions & 0 deletions source/test/fixtures/tsx/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Component} from 'react';

interface UnicornProps {
rainbow: string;
}

export class Unicorn extends Component<UnicornProps> {}
12 changes: 12 additions & 0 deletions source/test/fixtures/tsx/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>;
}
}
7 changes: 7 additions & 0 deletions source/test/fixtures/tsx/index.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';
import {expectType, expectError} from '../../..';
import {Unicorn} from '.';

expectType<JSX.Element>(<Unicorn rainbow='🌈' />);

expectError(<Unicorn foo='bar' />);
10 changes: 10 additions & 0 deletions source/test/fixtures/tsx/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": "*"
}
}
22 changes: 17 additions & 5 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('throw if no type definition was found', async t => {
});

test('throw if no test is found', async t => {
await t.throwsAsync(m({cwd: path.join(__dirname, 'fixtures/no-test')}), 'The test file `index.test-d.ts` does not exist. Create one and try again.');
await t.throwsAsync(m({cwd: path.join(__dirname, 'fixtures/no-test')}), 'The test file `index.test-d.ts` or `index.test-d.tsx` does not exist. Create one and try again.');
});

test('return diagnostics', async t => {
Expand Down Expand Up @@ -91,19 +91,19 @@ test('overridden config defaults to `strict` if `strict` is not explicitly overr
t.is(column, 19);
});

test('fail if types are used from a lib that wasn\'t explicitly specified', async t => {
test('fail if types are used from a lib that was not explicitly specified', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/lib-config/failure-missing-lib')});

t.is(diagnostics.length, 2);

t.true(/failure-missing-lib\/index.d.ts$/.test(diagnostics[0].fileName));
t.is(diagnostics[0].message, 'Cannot find name \'Document\'.');
t.is(diagnostics[0].message, 'Cannot find name \'Window\'.');
t.is(diagnostics[0].severity, 'error');
t.is(diagnostics[0].line, 1);
t.is(diagnostics[0].column, 24);
t.is(diagnostics[0].column, 22);

t.true(/failure-missing-lib\/index.test-d.ts$/.test(diagnostics[1].fileName));
t.is(diagnostics[1].message, 'Cannot find name \'Document\'.');
t.is(diagnostics[1].message, 'Cannot find name \'Window\'.');
t.is(diagnostics[1].severity, 'error');
t.is(diagnostics[1].line, 4);
t.is(diagnostics[1].column, 11);
Expand Down Expand Up @@ -177,6 +177,12 @@ test('support default test directory', async t => {
t.true(diagnostics.length === 0);
});

test('support tsx in subdirectory', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-directory/tsx')});

t.true(diagnostics.length === 0);
});

test('support setting a custom test directory', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-directory/custom')});

Expand Down Expand Up @@ -244,3 +250,9 @@ test('missing import', async t => {
t.true(diagnostics[0].message === 'Cannot find name \'Primitive\'.');
t.true(diagnostics[0].severity === 'error');
});

test('tsx', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/tsx')});

t.true(diagnostics.length === 0);
});

0 comments on commit c445879

Please sign in to comment.