eli*_*itu 18 javascript testing typescript jestjs enzyme
我正在尝试为我的 React 组件编写一个测试,使用 TypeScript、Jest 作为我的测试运行程序和 Enzyme 来测试我的 React 组件。每当我将我的组件传递给shallowEnzyme 函数时,我都会收到 ts 错误“'Navbar' 指的是一个值,但在此处被用作类型。”,并且在下面我收到 eslint 错误“解析错误:'>' 预期” .
我在其他一些组件上尝试过,当shallow作为参数传递给函数时,所有组件都出现相同的错误。我怀疑这可能与我的 TS 配置有关,但对于我的生活,我似乎无法找到解决方案。
这是 Navbar.tsx 的代码:
import React from 'react';
import { shallow } from 'enzyme';
import Navbar from './Navbar';
describe('Navbar component', () => {
let component;
beforeEach(() => {
component = shallow(<Navbar />); // Error being desplayed here
});
it('should render without errors', () => {
expect(component).toMatchInlineSnapshot();
expect(component.length).toBe(1);
expect(component).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
还发布我的配置文件:
配置:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
}
Run Code Online (Sandbox Code Playgroud)
jest.config.ts:
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
snapshotSerializers: ['enzyme-to-json/serializer'],
setupTestFrameworkScriptFile: '<rootDir>/src/setupEnzyme.ts',
};
Run Code Online (Sandbox Code Playgroud)
酶设置:
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';
configure({ adapter: new Adapter(), disableLifecycleMethods:
Run Code Online (Sandbox Code Playgroud)
Ern*_*sto 43
您是否尝试过更改文件名?MyComponent.test.tsx 另外,你是否安装了 jest 和 stuff 的类型
npm i -D @types/jest。我的意思是我这么说是因为如果你看一下 jest 配置,它说 testRegex。你有这样
__tests__/*.(test|spec).txs的测试必须在一个测试文件夹中,它必须有名称:MyComponent。test.tsx
| 归档时间: |
|
| 查看次数: |
13195 次 |
| 最近记录: |