diff --git a/package.json b/package.json index 5319c201..96008e9f 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/source/lib/index.ts b/source/lib/index.ts index 9bd6bc26..a58b04e5 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -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; diff --git a/source/test/fixtures/lib-config/failure-missing-lib/index.d.ts b/source/test/fixtures/lib-config/failure-missing-lib/index.d.ts index b34a6db6..440e7c96 100644 --- a/source/test/fixtures/lib-config/failure-missing-lib/index.d.ts +++ b/source/test/fixtures/lib-config/failure-missing-lib/index.d.ts @@ -1,3 +1,3 @@ -declare const document: Document; +declare const window: Window; -export default document; +export default window; diff --git a/source/test/fixtures/lib-config/failure-missing-lib/index.js b/source/test/fixtures/lib-config/failure-missing-lib/index.js index 674f77d5..92505d8a 100644 --- a/source/test/fixtures/lib-config/failure-missing-lib/index.js +++ b/source/test/fixtures/lib-config/failure-missing-lib/index.js @@ -1 +1 @@ -module.exports.default = window.document; +module.exports.default = window; diff --git a/source/test/fixtures/lib-config/failure-missing-lib/index.test-d.ts b/source/test/fixtures/lib-config/failure-missing-lib/index.test-d.ts index d7585f79..ef7346a0 100644 --- a/source/test/fixtures/lib-config/failure-missing-lib/index.test-d.ts +++ b/source/test/fixtures/lib-config/failure-missing-lib/index.test-d.ts @@ -1,4 +1,4 @@ import {expectType} from '../../../..'; -import document from '.'; +import window from '.'; -expectType(document); +expectType(window); diff --git a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.d.ts b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.d.ts index b38f8937..3a53f3d2 100644 --- a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.d.ts +++ b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.d.ts @@ -1,5 +1,5 @@ /// -declare const document: Document; +declare const window: Window; -export default document; +export default window; diff --git a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.js b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.js index 674f77d5..92505d8a 100644 --- a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.js +++ b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.js @@ -1 +1 @@ -module.exports.default = window.document; +module.exports.default = window; diff --git a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.test-d.ts b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.test-d.ts index d7585f79..ef7346a0 100644 --- a/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.test-d.ts +++ b/source/test/fixtures/lib-config/lib-as-triple-slash-reference/index.test-d.ts @@ -1,4 +1,4 @@ import {expectType} from '../../../..'; -import document from '.'; +import window from '.'; -expectType(document); +expectType(window); diff --git a/source/test/fixtures/test-directory/tsx/index.d.ts b/source/test/fixtures/test-directory/tsx/index.d.ts new file mode 100644 index 00000000..74067858 --- /dev/null +++ b/source/test/fixtures/test-directory/tsx/index.d.ts @@ -0,0 +1,7 @@ +import {Component} from 'react'; + +interface UnicornProps { + rainbow: string; +} + +export class Unicorn extends Component {} diff --git a/source/test/fixtures/test-directory/tsx/index.js b/source/test/fixtures/test-directory/tsx/index.js new file mode 100644 index 00000000..6db2eca6 --- /dev/null +++ b/source/test/fixtures/test-directory/tsx/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/test-directory/tsx/package.json b/source/test/fixtures/test-directory/tsx/package.json new file mode 100644 index 00000000..09392033 --- /dev/null +++ b/source/test/fixtures/test-directory/tsx/package.json @@ -0,0 +1,10 @@ +{ + "name": "foo", + "files": [ + "index.js", + "index.d.ts" + ], + "dependencies": { + "react": "*" + } +} diff --git a/source/test/fixtures/test-directory/tsx/test-d/unicorn.tsx b/source/test/fixtures/test-directory/tsx/test-d/unicorn.tsx new file mode 100644 index 00000000..24473abf --- /dev/null +++ b/source/test/fixtures/test-directory/tsx/test-d/unicorn.tsx @@ -0,0 +1,7 @@ +import * as React from 'react'; +import {expectType, expectError} from '../../../../..'; +import {Unicorn} from '..'; + +expectType(); + +expectError(); diff --git a/source/test/fixtures/tsx/index.d.ts b/source/test/fixtures/tsx/index.d.ts new file mode 100644 index 00000000..74067858 --- /dev/null +++ b/source/test/fixtures/tsx/index.d.ts @@ -0,0 +1,7 @@ +import {Component} from 'react'; + +interface UnicornProps { + rainbow: string; +} + +export class Unicorn extends Component {} diff --git a/source/test/fixtures/tsx/index.js b/source/test/fixtures/tsx/index.js new file mode 100644 index 00000000..6db2eca6 --- /dev/null +++ b/source/test/fixtures/tsx/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/index.test-d.tsx b/source/test/fixtures/tsx/index.test-d.tsx new file mode 100644 index 00000000..afa2157e --- /dev/null +++ b/source/test/fixtures/tsx/index.test-d.tsx @@ -0,0 +1,7 @@ +import * as React from 'react'; +import {expectType, expectError} from '../../..'; +import {Unicorn} from '.'; + +expectType(); + +expectError(); diff --git a/source/test/fixtures/tsx/package.json b/source/test/fixtures/tsx/package.json new file mode 100644 index 00000000..09392033 --- /dev/null +++ b/source/test/fixtures/tsx/package.json @@ -0,0 +1,10 @@ +{ + "name": "foo", + "files": [ + "index.js", + "index.d.ts" + ], + "dependencies": { + "react": "*" + } +} diff --git a/source/test/test.ts b/source/test/test.ts index 750f6543..12664087 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -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 => { @@ -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); @@ -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')}); @@ -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); +});