Skip to content

Commit c6c2ec4

Browse files
authoredJun 30, 2022
Infer value for moduleResolution tsconfig if not set (#149)
1 parent 396e878 commit c6c2ec4

File tree

21 files changed

+114
-7
lines changed

21 files changed

+114
-7
lines changed
 

‎readme.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ By default, `tsd` applies the following configuration:
135135
"target": "es2017",
136136
"lib": ["es2017"],
137137
"module": "commonjs",
138-
// The following option is set and is not overridable:
139-
"moduleResolution": "node"
138+
// The following option is set and is not overridable.
139+
// It is set to `nodenext` if `module` is `nodenext`, `node16` if `module` is `node16` or `node` otherwise.
140+
"moduleResolution": "node" | "node16" | "nodenext"
140141
}
141142
```
142143

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

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

158159
## Assertions
159160

‎source/lib/config.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,29 @@ export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => {
2727
cwd
2828
);
2929

30+
const combinedCompilerOptions = {
31+
...tsConfigCompilerOptions,
32+
...packageJsonCompilerOptions,
33+
};
34+
35+
const module = combinedCompilerOptions.module ?? ModuleKind.CommonJS;
36+
3037
return {
3138
directory: 'test-d',
3239
...pkgConfig,
3340
compilerOptions: {
3441
strict: true,
3542
jsx: JsxEmit.React,
3643
lib: parseRawLibs(['es2017', 'dom', 'dom.iterable'], cwd),
37-
module: ModuleKind.CommonJS,
44+
module,
3845
target: ScriptTarget.ES2017,
3946
esModuleInterop: true,
40-
...tsConfigCompilerOptions,
41-
...packageJsonCompilerOptions,
42-
moduleResolution: ModuleResolutionKind.NodeJs,
47+
...combinedCompilerOptions,
48+
moduleResolution: module === ModuleKind.NodeNext ?
49+
ModuleResolutionKind.NodeNext :
50+
module === ModuleKind.Node16 ?
51+
ModuleResolutionKind.Node16 :
52+
ModuleResolutionKind.NodeJs,
4353
skipLibCheck: false
4454
}
4555
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const foo: string;
2+
3+
export default foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import foo from 'foo';
2+
import {expectType} from '../../../../index.js';
3+
4+
expectType<string>(foo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "foo",
3+
"type": "module",
4+
"exports": "./index.js",
5+
"tsd": {
6+
"compilerOptions": {
7+
"module": "node16"
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const foo: string;
2+
3+
export default foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import foo from 'foo';
2+
import {expectType} from '../../../../index.js';
3+
4+
expectType<string>(foo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "foo",
3+
"type": "module",
4+
"exports": "./index.js"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const foo: string;
2+
3+
export default foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import foo from 'foo';
2+
import {expectType} from '../../../../index.js';
3+
4+
expectType<string>(foo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "foo",
3+
"type": "module",
4+
"exports": "./index.js",
5+
"tsd": {
6+
"compilerOptions": {
7+
"module": "nodenext"
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const foo: string;
2+
3+
export default foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import foo from 'foo';
2+
import {expectType} from '../../../../index.js';
3+
4+
expectType<string>(foo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "foo",
3+
"type": "module",
4+
"exports": "./index.js"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"module": "nodenext"
4+
}
5+
}

‎source/test/test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ test('allow specifying a lib in tsconfig.json', async t => {
138138
verify(t, diagnostics, []);
139139
});
140140

141+
test('use moduleResolution `nodenext` when module is `nodenext` in tsconfig.json', async t => {
142+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-tsconfig-json')});
143+
144+
verify(t, diagnostics, []);
145+
});
146+
147+
test('use moduleResolution `nodenext` when module is `nodenext` in package.json', async t => {
148+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/nodenext-from-package-json')});
149+
150+
verify(t, diagnostics, []);
151+
});
152+
153+
test('use moduleResolution `node16` when module is `node16` in tsconfig.json', async t => {
154+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-tsconfig-json')});
155+
156+
verify(t, diagnostics, []);
157+
});
158+
159+
test('use moduleResolution `node16` when module is `node16` in package.json', async t => {
160+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/module-resolution/node16-from-package-json')});
161+
162+
verify(t, diagnostics, []);
163+
});
164+
141165
test('add support for esm with esModuleInterop', async t => {
142166
const diagnostics = await tsd({
143167
cwd: path.join(__dirname, 'fixtures/esm')

0 commit comments

Comments
 (0)
Please sign in to comment.