Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly merge default compiler options #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ When you have spread your tests over multiple files, you can store all those fil

Now you can put all your test files in the `my-test-dir` directory.

### Custom TypeScript config

By default, `tsd-check` applies the following configuration:

```json5
{
"strict": true,
"jsx": "react",
"target": "es2017",
// The following options are set and are not overridable:
"moduleResolution": "node",
"skipLibCheck": true
}
```

If you wish to override these options, you have the possibility to provide a custom TypeScript config to `tsd-check` by specifying it in `package.json`.

*Default options will still apply if you don't override them explicitly.* You can't override the `moduleResolution` and `skipLibCheck` options.

```json
{
"name": "my-module",
"tsd-check": {
"compilerOptions": {
"strict": false
}
}
}
```

## Assertions

Expand Down
18 changes: 7 additions & 11 deletions source/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ import {Config} from './interfaces';
* @param pkg - The package.json object.
* @returns The config object.
*/
export default (pkg: any): Config => {
const config = {
export default (pkg: {'tsd-check'?: Partial<Config>}): Config => {
const pkgConfig = pkg['tsd-check'] || {};

return {
directory: 'test-d',
...pkgConfig,
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
target: ScriptTarget.ES2017
},
...pkg['tsd-check']
};

return {
...config,
compilerOptions: {
...config.compilerOptions,
target: ScriptTarget.ES2017,
...pkgConfig.compilerOptions,
...{
moduleResolution: ModuleResolutionKind.NodeJs,
skipLibCheck: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function (foo: number): number | null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = foo => {
return foo > 0 ? foo : null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from '../../..';
import aboveZero from '.';

expectType<number>(aboveZero(1));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo",
"tsd-check": {
"compilerOptions": {
}
}
}
19 changes: 16 additions & 3 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,22 @@ test('fail if tests don\'t pass in strict mode', async t => {

const {fileName, message, severity, line, column} = diagnostics[0];
t.true(/failure-strict-null-checks\/index.test-d.ts$/.test(fileName));
t.is(
message,
`Argument of type 'number | null' is not assignable to parameter of type 'number'.
t.is(message, `Argument of type 'number | null' is not assignable to parameter of type 'number'.
Type \'null\' is not assignable to type 'number'.`
);
t.is(severity, 'error');
t.is(line, 4);
t.is(column, 19);
});

test('overridden config defaults to `strict` if `strict` is not explicitly overridden', async t => {
const diagnostics = await m({
cwd: path.join(__dirname, 'fixtures/strict-null-checks-as-default-config-value')
});

const {fileName, message, severity, line, column} = diagnostics[0];
t.true(/strict-null-checks-as-default-config-value\/index.test-d.ts$/.test(fileName));
t.is(message, `Argument of type 'number | null' is not assignable to parameter of type 'number'.
Type \'null\' is not assignable to type 'number'.`
);
t.is(severity, 'error');
Expand Down