Aur*_*tas 5 csv typescript ts-jest
Jest 似乎无法获取导入的库。我一直在阅读相关内容并尝试各种选择。其中之一就是这个。然而它们似乎都不起作用。
\n//xyz.ts\n\n..\n/* eslint-disable */\nimport * as csv from \'csv-stringify/sync\';\n/* eslint-enable */\n..\nRun Code Online (Sandbox Code Playgroud)\n运行 Jest 后:
\nTest suite failed to run\n\n Cannot find module \'csv-stringify/sync\' from \'xyz.ts\'\nRun Code Online (Sandbox Code Playgroud)\n正在使用的库 - https://csv.js.org/stringify/。
\n我怀疑我做错了什么,因为如果我不禁用它的检查,即使 ESLint 也会抱怨这一点。
\n Unable to resolve path to module \'csv-stringify/sync\' import/no-unresolved\nRun Code Online (Sandbox Code Playgroud)\n图书馆的结构:
\n\n此外,如果我尝试导入为
\nimport * as csv from \'csv-stringify/lib/sync\';\nRun Code Online (Sandbox Code Playgroud)\n然后我在运行 Jest 后收到此错误
\nTest suite failed to run\n\n Jest encountered an unexpected token\n\n 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.\n\n Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n By default "node_modules" folder is ignored by transformers.\n\n Here\'s what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\n You\'ll find more details and examples of these config options in the docs:\n https://jestjs.io/docs/configuration\n For information about custom transformations, see:\n https://jestjs.io/docs/code-transformation\n\n Details:\n\n <redacted>/node_modules/csv-stringify/lib/sync.js:2\n import { Stringifier } from \'./index.js\';\n ^^^^^^\nRun Code Online (Sandbox Code Playgroud)\n我想这里可能需要某种转变?
\n其他文件:
\njest.config.js
module.exports = {\n // Root path\n roots: [\'\'],\n // testMatch: [\'**/__tests__/**/*.+(ts|tsx|js)\', \'**/?(*.)+(spec|test).+(ts|tsx|js)\'],\n // Only test TS files, don\'t test JS\n testMatch: [\'**/?(*.)+(spec|test).+(ts)\'],\n transform: {\n \'^.+\\\\.(ts|tsx)$\': \'ts-jest\',\n },\n // Include below folders to be checked for modules\n moduleDirectories: [\'node_modules\', \'src\'],\n};\nRun Code Online (Sandbox Code Playgroud)\ntsconfig.json
//xyz.ts\n\n..\n/* eslint-disable */\nimport * as csv from \'csv-stringify/sync\';\n/* eslint-enable */\n..\nRun Code Online (Sandbox Code Playgroud)\n解决以下建议:
\n\n\n从 dist 导入它 - lib 是源(未转换)文件,这就是您收到第二个错误的原因。
\n
尝试cjs过esm- 都不起作用
import * as csv from \'csv-stringify/dist/cjs/sync\';\n..\nRun Code Online (Sandbox Code Playgroud)\n错误
\n Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath \'./dist/cjs/sync\' is not defined by "exports" \nRun Code Online (Sandbox Code Playgroud)\n\n\n您是否尝试过 import { stringify } from \'csv-stringify/sync\'; 正如图书馆建议的那样?
\n
是的 - 我收到另一个错误。
\n Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath \'./lib/sync\' is not defined by "exports" in <redacted>/node_modules/csv-stringify/package.json\nRun Code Online (Sandbox Code Playgroud)\npackage.json exports部分
"exports": {\n ".": {\n "import": "./lib/index.js",\n "require": "./dist/cjs/index.cjs"\n },\n "./sync": {\n "import": "./lib/sync.js",\n "require": "./dist/cjs/sync.cjs"\n },\n "./browser/esm/": "./dist/esm/"\n }\nRun Code Online (Sandbox Code Playgroud)\n
小智 1
ESM/CJS 导入系统可能存在问题。
尝试配置Jest为使用 ESM 并添加以下内容jest.config.ts:
{
// ...
"extensionsToTreatAsEsm": [".ts"]
}
Run Code Online (Sandbox Code Playgroud)
如果失败尝试使用 CJS 导入样式
const { stringify } = require('csv-stringify/sync');
| 归档时间: |
|
| 查看次数: |
1353 次 |
| 最近记录: |