diff --git a/readme.md b/readme.md index bcf5fc39..a6a0f30d 100644 --- a/readme.md +++ b/readme.md @@ -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" } ``` @@ -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 diff --git a/source/lib/config.ts b/source/lib/config.ts index a57af9b4..93d19d89 100644 --- a/source/lib/config.ts +++ b/source/lib/config.ts @@ -27,6 +27,13 @@ export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => { cwd ); + const combinedCompilerOptions = { + ...tsConfigCompilerOptions, + ...packageJsonCompilerOptions, + }; + + const module = combinedCompilerOptions.module ?? ModuleKind.CommonJS; + return { directory: 'test-d', ...pkgConfig, @@ -34,12 +41,15 @@ export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => { 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 } }; diff --git a/source/test/fixtures/module-resolution/node16-from-package-json/index.d.ts b/source/test/fixtures/module-resolution/node16-from-package-json/index.d.ts new file mode 100644 index 00000000..eb851a2b --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-package-json/index.d.ts @@ -0,0 +1,3 @@ +declare const foo: string; + +export default foo; diff --git a/source/test/fixtures/module-resolution/node16-from-package-json/index.js b/source/test/fixtures/module-resolution/node16-from-package-json/index.js new file mode 100644 index 00000000..7f0fda40 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-package-json/index.js @@ -0,0 +1 @@ +module.exports.default = 'foo'; diff --git a/source/test/fixtures/module-resolution/node16-from-package-json/index.test-d.ts b/source/test/fixtures/module-resolution/node16-from-package-json/index.test-d.ts new file mode 100644 index 00000000..09a2d6c1 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-package-json/index.test-d.ts @@ -0,0 +1,4 @@ +import foo from 'foo'; +import {expectType} from '../../../../index.js'; + +expectType(foo); diff --git a/source/test/fixtures/module-resolution/node16-from-package-json/package.json b/source/test/fixtures/module-resolution/node16-from-package-json/package.json new file mode 100644 index 00000000..e6db8b83 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-package-json/package.json @@ -0,0 +1,10 @@ +{ + "name": "foo", + "type": "module", + "exports": "./index.js", + "tsd": { + "compilerOptions": { + "module": "node16" + } + } +} diff --git a/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.d.ts b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.d.ts new file mode 100644 index 00000000..eb851a2b --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.d.ts @@ -0,0 +1,3 @@ +declare const foo: string; + +export default foo; diff --git a/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.js b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.js new file mode 100644 index 00000000..7f0fda40 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.js @@ -0,0 +1 @@ +module.exports.default = 'foo'; diff --git a/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.test-d.ts b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.test-d.ts new file mode 100644 index 00000000..09a2d6c1 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/index.test-d.ts @@ -0,0 +1,4 @@ +import foo from 'foo'; +import {expectType} from '../../../../index.js'; + +expectType(foo); diff --git a/source/test/fixtures/module-resolution/node16-from-tsconfig-json/package.json b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/package.json new file mode 100644 index 00000000..75bc3f28 --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "type": "module", + "exports": "./index.js" +} diff --git a/source/test/fixtures/module-resolution/node16-from-tsconfig-json/tsconfig.json b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/tsconfig.json new file mode 100644 index 00000000..18c102cd --- /dev/null +++ b/source/test/fixtures/module-resolution/node16-from-tsconfig-json/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "node16" + } +} diff --git a/source/test/fixtures/module-resolution/nodenext-from-package-json/index.d.ts b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.d.ts new file mode 100644 index 00000000..eb851a2b --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.d.ts @@ -0,0 +1,3 @@ +declare const foo: string; + +export default foo; diff --git a/source/test/fixtures/module-resolution/nodenext-from-package-json/index.js b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.js new file mode 100644 index 00000000..7f0fda40 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.js @@ -0,0 +1 @@ +module.exports.default = 'foo'; diff --git a/source/test/fixtures/module-resolution/nodenext-from-package-json/index.test-d.ts b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.test-d.ts new file mode 100644 index 00000000..09a2d6c1 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-package-json/index.test-d.ts @@ -0,0 +1,4 @@ +import foo from 'foo'; +import {expectType} from '../../../../index.js'; + +expectType(foo); diff --git a/source/test/fixtures/module-resolution/nodenext-from-package-json/package.json b/source/test/fixtures/module-resolution/nodenext-from-package-json/package.json new file mode 100644 index 00000000..dde0bd2a --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-package-json/package.json @@ -0,0 +1,10 @@ +{ + "name": "foo", + "type": "module", + "exports": "./index.js", + "tsd": { + "compilerOptions": { + "module": "nodenext" + } + } +} diff --git a/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.d.ts b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.d.ts new file mode 100644 index 00000000..eb851a2b --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.d.ts @@ -0,0 +1,3 @@ +declare const foo: string; + +export default foo; diff --git a/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.js b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.js new file mode 100644 index 00000000..7f0fda40 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.js @@ -0,0 +1 @@ +module.exports.default = 'foo'; diff --git a/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.test-d.ts b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.test-d.ts new file mode 100644 index 00000000..09a2d6c1 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/index.test-d.ts @@ -0,0 +1,4 @@ +import foo from 'foo'; +import {expectType} from '../../../../index.js'; + +expectType(foo); diff --git a/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/package.json b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/package.json new file mode 100644 index 00000000..75bc3f28 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "type": "module", + "exports": "./index.js" +} diff --git a/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/tsconfig.json b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/tsconfig.json new file mode 100644 index 00000000..7ce85cd7 --- /dev/null +++ b/source/test/fixtures/module-resolution/nodenext-from-tsconfig-json/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "nodenext" + } +} diff --git a/source/test/test.ts b/source/test/test.ts index e84275f0..4c09266e 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -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')