Red*_*ant 2 javascript typescript reactjs jestjs ts-jest
我试图用来ts-jest
运行tsx测试文件form.spec.tsx
。
该form.spec.tsx
进口React Quill
编辑器和一些插件。
我如何绕过SyntaxError: Unexpected identifier
来自导入的名为quill-mention的插件的错误Quill
?该模块涉及form.spec.tsx
。
我已经["<rootDir>/node_modules/"]
在笑话配置中添加到transformIgnorePatterns字段,但是/node_modules/quill-mention/src/quill.mention.js中仍然存在此问题
? 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";
| ^
Run Code Online (Sandbox Code Playgroud)
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: ["@", "#"],
},
}
/>
);
Run Code Online (Sandbox Code Playgroud)
package.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/"
]
}
Run Code Online (Sandbox Code Playgroud)
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"
]
}
Run Code Online (Sandbox Code Playgroud)
有人说allowJs: true
可以修复它,但是不起作用。我所有的测试都失败了JavaScript heap out of memory
。
问题在于Jest 没有转换该文件。并且在普通JS中import
是无效的。您需要配置Jest,以便它将转换该文件。
首先,将更transform
改为还处理带有扩展名js
或的文件jsx
(除了ts
文件:)
"jest": {
"transform": {
"^.+\\.(ts|js)x?$": "ts-jest"
},
Run Code Online (Sandbox Code Playgroud)
接下来,您需要将目录列入白名单,以便Jest对其进行转换。这将跳过转换node_modules
除quill-mention
目录之外的所有文件。
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!(quill-mention)/)"
]
Run Code Online (Sandbox Code Playgroud)
这应该克服了的问题quill-mention
。现在它应该失败ReferenceError: MutationObserver is not defined
,这是另一个问题,与它使用的Jest的JSm环境有关。您可以在此处阅读有关如何解决该问题的信息:
您可能还需要考虑移至babel-jest
+ @babel/preset-typescript
而不是使用ts-jest
。
归档时间: |
|
查看次数: |
263 次 |
最近记录: |