【发布时间】:2022-01-16 13:53:01
【问题描述】:
Jest 似乎无法获取导入的库。我一直在阅读它并尝试各种选择。其中之一是this。但是,它们似乎都不起作用。
//xyz.ts
..
/* eslint-disable */
import * as csv from 'csv-stringify/sync';
/* eslint-enable */
..
运行 Jest 后:
Test suite failed to run
Cannot find module 'csv-stringify/sync' from 'xyz.ts'
正在使用的库 - https://csv.js.org/stringify/。
我怀疑我做错了什么,因为如果我不禁用它的检查,即使 ESLint 也会抱怨它。
Unable to resolve path to module 'csv-stringify/sync' import/no-unresolved
图书馆的结构:
此外,如果我尝试导入为
import * as csv from 'csv-stringify/lib/sync';
然后我在运行 Jest 后收到此错误
Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
<redacted>/node_modules/csv-stringify/lib/sync.js:2
import { Stringifier } from './index.js';
^^^^^^
我想这里可能需要某种转换?
其他文件:
jest.config.js
module.exports = {
// Root path
roots: [''],
// testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
// Only test TS files, don't test JS
testMatch: ['**/?(*.)+(spec|test).+(ts)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
// Include below folders to be checked for modules
moduleDirectories: ['node_modules', 'src'],
};
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
"strictNullChecks": true
},
"compileOnSave": true,
"include": [
"coverage",
"jest.config.js",
"src",
"test"
]
}
解决以下建议:
从 dist 导入它 - lib 是源(未编译的)文件,这就是您收到第二个错误的原因。
试过 cjs 和 esm - 都没有工作
import * as csv from 'csv-stringify/dist/cjs/sync';
..
错误
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/cjs/sync' is not defined by "exports"
您是否尝试过 import { stringify } from 'csv-stringify/sync';正如图书馆所建议的那样?
是的 - 我收到另一个错误。
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/sync' is not defined by "exports" in <redacted>/node_modules/csv-stringify/package.json
package.jsonexports 部分
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
},
"./browser/esm/": "./dist/esm/"
}
【问题讨论】:
-
从 dist 导入它 - lib 是源(未转译)文件,这就是您收到第二个错误的原因。
-
您是否按照图书馆的建议尝试过
import { stringify } from 'csv-stringify/sync';? -
谢谢。这两个建议都没有奏效。我已将它们添加到我的原始帖子中。
标签: typescript csv ts-jest