Skip to content

Commit

Permalink
fix: correctly link types in package.json exports field (#1395)
Browse files Browse the repository at this point in the history
* fix: correctly link types in `package.json` `exports` field

* test: add typings tests for node16 module resolution

* test: allow to use per-version tsconfig files

* chore: add logs to typings tests ts version

* chore: use "ts-node" in typings tests

Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
ivanhofer and kettanaito committed Oct 3, 2022
1 parent dae755d commit 3902b8c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -9,9 +9,11 @@
"default": "./lib/index.js"
},
"./native": {
"types": "./lib/native/index.d.ts",
"default": "./lib/native/index.js"
},
"./node": {
"types": "./lib/node/index.d.ts",
"require": "./lib/node/index.js",
"default": "./lib/node/index.mjs"
}
Expand All @@ -31,7 +33,7 @@
"test:unit": "cross-env BABEL_ENV=test jest --maxWorkers=3",
"test:integration": "jest --config=./test/jest.config.js --maxWorkers=1",
"test:smoke": "./config/scripts/smoke.sh",
"test:ts": "yarn tsc -p ./test/typings/tsconfig.json",
"test:ts": "ts-node test/typings/run.ts",
"prepare": "yarn simple-git-hooks init",
"prepack": "yarn build",
"release": "release publish",
Expand Down
3 changes: 3 additions & 0 deletions test/typings/index.test-d.cts
@@ -0,0 +1,3 @@
export * from 'msw'
export { setupServer } from 'msw/node'
export { setupServer as setupNativeServer } from 'msw/native'
3 changes: 3 additions & 0 deletions test/typings/index.test-d.mts
@@ -0,0 +1,3 @@
export * from 'msw'
export { setupServer } from 'msw/node'
export { setupServer as setupNativeServer } from 'msw/native'
31 changes: 31 additions & 0 deletions test/typings/run.ts
@@ -0,0 +1,31 @@
import * as fs from 'fs'
import * as path from 'path'
import { spawnSync } from 'child_process'
import { invariant } from 'outvariant'
import tsPackageJson from 'typescript/package.json'

const tsInstalledVersion = tsPackageJson.version
invariant(
tsInstalledVersion,
'Failed to run typings tests: unable to determine TypeScript version',
)

const tsVersionMajorMinor = tsInstalledVersion.substring(
0,
tsInstalledVersion.lastIndexOf('.'),
)

const tsConfigPaths = [
path.resolve(__dirname, `tsconfig.${tsVersionMajorMinor}.json`),
path.resolve(__dirname, 'tsconfig.json'),
]
const tsConfigPath = tsConfigPaths.find((path) => fs.existsSync(path)) as string

console.log('Using tsconfig at "%s"', tsConfigPath)

const { status } = spawnSync('tsc', ['-p', tsConfigPath], {
cwd: path.resolve(__dirname, '../..'),
stdio: 'inherit',
})

process.exit(status || 0)
6 changes: 6 additions & 0 deletions test/typings/tsconfig.4.7.json
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"moduleResolution": "Node16"
}
}
3 changes: 3 additions & 0 deletions test/typings/tsconfig.4.8.json
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.4.7.json"
}
9 changes: 6 additions & 3 deletions test/typings/tsconfig.json
@@ -1,18 +1,21 @@
{
"compilerOptions": {
"strict": true,
"target": "esnext",
"module": "commonjs",
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "Node",
"types": ["node"],
"typeRoots": ["../../node_modules/@types"],
"lib": ["dom"],
"baseUrl": ".",
"typeRoots": ["node_modules/@types"],
"paths": {
"msw": ["../../"],
"msw": ["../.."],
"msw/*": ["../../*"]
}
},
"include": ["../../global.d.ts", "**/*.test-d.ts"],
"include": ["../../global.d.ts", "**/*.test-d.*"],
"exclude": ["node_modules"]
}

0 comments on commit 3902b8c

Please sign in to comment.