我正在尝试使用ts-jest
运行 tsx 测试文件form.spec.tsx
。
form.spec.tsx
导入编辑器React Quill
和一些插件。
如何绕过SyntaxError: Unexpected identifier
来自名为quill-mention that import的插件的错误Quill
?该模块涉及form.spec.tsx
.
我已经在 jest 配置中添加了 transformIgnorePatterns 字段,但是/node_modules/quill-mention/src/quill.mention.js["<rootDir>/node_modules/"]
仍然存在这个问题
● Test suite failed to run
/home/web/node_modules/quill-mention/src/quill.mention.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Quill from 'quill';
^^^^^
SyntaxError: Unexpected identifier
1 | import React from "react"
> 2 | import "quill-mention";
| ^
form.spec.tsx:
import {render, RenderResult, waitForElement} from "react-testing-library";
import ReactQuill, {Quill} from 'react-quill';
import "quill-mention";
const renderResult = render(
<ReactQuill
modules={
{
mention: {
allowedChars: /^[A-Za-z\sÅÄÖåäö]*$/,
mentionDenotationChars: ["@", "#"],
},
}
/>
);
包.json
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.jest.json"
},
"window": {}
},
"testRegex": "(/watch/web/__tests__/.*|(\\.|/)(test|spec))\\.(jsxxxx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"modulePaths": [
"<rootDir>"
],
"moduleNameMapper": {
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy",
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/FileMock.js"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
]
}
tsconfig.jest.json:
{
"compilerOptions": {
"jsx": "react",
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",
"removeComments": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"noLib": false,
"declaration": false,
"emitDecoratorMetadata": true,
"lib": ["es6", "dom"],
"types": ["jest","reflect-metadata"],
"inlineSources":true,
"skipLibCheck": true,
"esModuleInterop": true
},
"exclude": [
"node_modules"
]
}
有人说allowJs: true
可以修复它,但它不起作用。我所有的测试都失败了JavaScript heap out of memory
。