我试图写一个测试我的React组件,使用TypeScript,笑话作为我的测试运行程序和酶测试我的React组件。每当我将我的组件传递到浅
酶函数中时,我都会得到ts错误"'Navbar'指的是一个值,但在这里被用作一种类型。",下面我得到eslint错误"解析错误:'
我在其他一些组件上尝试了它,它们在作为参数传递到shallow
函数时都有相同的错误。我怀疑这可能与我的TS配置有关,但就我的一生而言,我似乎无法找到解决方案。
这是导航栏的代码。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();
});
});
同时发布我的配置文件:
tsconfig:
{
"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"]
}
开玩笑配置。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',
};
酶设置:
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';
configure({ adapter: new Adapter(), disableLifecycleMethods:
您是否尝试更改文件名?MyComponent。测验tsx还有,您是否安装了jest和stuff的类型npmi-D@types/jest
。我的意思是,我这么说是因为如果你看jest配置,它说testRegex。您有这样的\uuuu测试\uuuu/*。(测试规范)。txs
测试必须位于测试文件夹中,并且必须具有名称:MyComponent<代码>测试。tsx
首先npm安装-D@types/jest