Recently, I went through a cleanup and update process for a private package to make it compatible with Vite.
Initially, the package.json file had the following structure:
{
"name": "@myRegistry/my-package",
"version": "7.15.1",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"scripts": {
"build": "tsc --outDir dist/",
"build:docs": "typedoc ./index.ts",
"test": "jest",
"prepublishOnly": "npm run build"
},
"dependencies": {
"portable-fetch": "^3.0.0"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "^13.13.0",
"jest": "^25.4.0",
"ts-jest": "^25.4.0",
"typedoc": "^0.20.36",
"typescript": "^3.8.3"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
After the cleanup, the modified package.json looks like this:
{
"name": "@myRegistry/my-package",
"version": "7.16.0",
"type": "module",
"files": [
"build"
],
"main": "./build/my-package.js",
"module": "./build/my-package.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"import": "./build/my-package.js"
}
},
"scripts": {
"build": "tsc && vite build",
"docs": "typedoc ./src/index.ts"
},
"devDependencies": {
"typedoc": "^0.23.21",
"typescript": "^4.6.4",
"vite": "^3.2.3",
"vite-plugin-dts": "^1.7.1"
},
"dependencies": {
"isomorphic-fetch": "^3.0.0",
"url": "^0.11.0"
}
}
This updated package is being used in two projects that are also based on Vite.
One project runs unit tests using Vitest without any issues, while the other project uses Jest for unit testing and encounters errors like the following for each file importing the package:
Cannot find module '@myRegistry/my-package' from 'src/fileToTest.ts'
Require stack:
src/fileToTest.ts
tests/unit/fileToTest.spec.ts
Below is the jest.config.js file of the problematic project:
module.exports = {
coverageReporters: ['cobertura'],
moduleDirectories: [
'node_modules',
'src'
],
moduleFileExtensions: ['js', 'ts', 'vue'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
modulePaths: ['<rootDir>', '<rootDir>/node_modules'],
preset: 'ts-jest/presets/default-esm',
rootDir: './',
setupFilesAfterEnv: ['<rootDir>tests/setup.ts'],
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/tests/unit/**/?(*.)+(spec|test).[jt]s?(x)'],
transform: {
'.*\\.(vue)$': '@vue/vue2-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.(ts|tsx)?$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/']
}
If anyone could provide some insights into what might be missing or causing these issues, I would greatly appreciate it.