我最近清理和更新了一个私有包,它将用Vite构建。
在清理之前,package.json看起来像这样:
{
"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"
]
}
}
这是新的和当前的package.json。我做了Vite脚手架,只改变了我必须:
{
"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"
}
}
这个包安装在两个同样使用Vite的项目中。
一个包用Vitest做单元测试,我可以在那个包中运行单元测试,没有问题。
另一个项目使用Jest进行单元测试,在那个项目中,对于导入包的每个文件,我都会收到如下错误:
Cannot find module '@myRegistry/my-package' from 'src/fileToTest.ts'
Require stack:
src/fileToTest.ts
tests/unit/fileToTest.spec.ts
下面是该项目的jest.config.js:
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/']
}
我只是不知道我错过了什么,并希望得到任何帮助。
1条答案
按热度按时间rekjcdws1#
问题是我们从
my-package
中移除了CommonJS支持,用CommonJS支持重新构建包就解决了这个问题,或者干脆放弃jest而使用vitest。