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

Add support for nodenext moduleResolution #149

Merged
merged 5 commits into from Jun 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions readme.md
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
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
@@ -0,0 +1,3 @@
declare const foo: string;

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

expectType<string>(foo);
@@ -0,0 +1,10 @@
{
"name": "foo",
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
"type": "module",
"exports": "./index.js",
"tsd": {
"compilerOptions": {
"module": "node16"
}
}
}
@@ -0,0 +1,3 @@
declare const foo: string;

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

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

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

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

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

expectType<string>(foo);
@@ -0,0 +1,5 @@
{
"name": "foo",
"type": "module",
"exports": "./index.js"
}
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
}
}
24 changes: 24 additions & 0 deletions source/test/test.ts
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