Skip to content

Commit

Permalink
Infer value for moduleResolution tsconfig if not set (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
perrin4869 authored Jun 30, 2022
1 parent 396e878 commit c6c2ec4
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 7 deletions.
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ By default, `tsd` applies the following configuration:
"target": "es2017",
"lib": ["es2017"],
"module": "commonjs",
// The following option is set and is not overridable:
"moduleResolution": "node"
// The following option is set and is not overridable.
// It is set to `nodenext` if `module` is `nodenext`, `node16` if `module` is `node16` or `node` otherwise.
"moduleResolution": "node" | "node16" | "nodenext"
}
```

Expand All @@ -153,7 +154,7 @@ These options will be overridden if a `tsconfig.json` file is found in your proj
}
```

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

## Assertions

Expand Down
18 changes: 14 additions & 4 deletions source/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => {
cwd
);

const combinedCompilerOptions = {
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
};

const module = combinedCompilerOptions.module ?? ModuleKind.CommonJS;

return {
directory: 'test-d',
...pkgConfig,
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
lib: parseRawLibs(['es2017', 'dom', 'dom.iterable'], cwd),
module: ModuleKind.CommonJS,
module,
target: ScriptTarget.ES2017,
esModuleInterop: true,
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
moduleResolution: ModuleResolutionKind.NodeJs,
...combinedCompilerOptions,
moduleResolution: module === ModuleKind.NodeNext ?
ModuleResolutionKind.NodeNext :
module === ModuleKind.Node16 ?
ModuleResolutionKind.Node16 :
ModuleResolutionKind.NodeJs,
skipLibCheck: false
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const foo: string;

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

expectType<string>(foo);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "foo",
"type": "module",
"exports": "./index.js",
"tsd": {
"compilerOptions": {
"module": "node16"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const foo: string;

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

expectType<string>(foo);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "foo",
"type": "module",
"exports": "./index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "node16"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const foo: string;

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

expectType<string>(foo);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "foo",
"type": "module",
"exports": "./index.js",
"tsd": {
"compilerOptions": {
"module": "nodenext"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const foo: string;

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

expectType<string>(foo);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "foo",
"type": "module",
"exports": "./index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
}
}
24 changes: 24 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ test('allow specifying a lib in tsconfig.json', async t => {
verify(t, diagnostics, []);
});

test('use moduleResolution `nodenext` when module is `nodenext` in tsconfig.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-tsconfig-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution `nodenext` when module is `nodenext` in package.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-package-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution `node16` when module is `node16` in tsconfig.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-tsconfig-json')});

verify(t, diagnostics, []);
});

test('use moduleResolution `node16` when module is `node16` in package.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-package-json')});

verify(t, diagnostics, []);
});

test('add support for esm with esModuleInterop', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/esm')
Expand Down

0 comments on commit c6c2ec4

Please sign in to comment.