简体   繁体   English

“导航栏指的是一个值,但在这里被用作一种类型”在测试时尝试渲染我的组件的浅表副本

[英]“Navbar refers to a value, but is being used as a type here” when trying to render a shallow copy of my component when testing

I am trying to write a test to my React component, using TypeScript, Jest as my test runner and Enzyme for testing my React components.我正在尝试为我的 React 组件编写一个测试,使用 TypeScript、Jest 作为我的测试运行器和 Enzyme 来测试我的 React 组件。 Whenever I pass my component into the shallow Enzyme function, I get the ts error "'Navbar' refers to a value, but is being used as a type here.", and underneath I get eslint error "Parsing error: '>' expected".每当我将组件传递到shallow酶 function 时,我都会收到 ts 错误“'Navbar' 指的是一个值,但在这里被用作一个类型。”,在下面我得到 eslint 错误“解析错误:'>' 预期”。

I tried it on some other components, all have the same error when being passed into the shallow function as arguments.我在其他一些组件上进行了尝试,当作为 arguments 传递到shallow function 时,它们都有相同的错误。 I suspect it may have something to do with my TS configurations, but for the life of me I cannot seem to be finding a solution.我怀疑这可能与我的 TS 配置有关,但对于我来说,我似乎无法找到解决方案。

Here's the code for Navbar.tsx:这是 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();
    });
});

Also posting my config files:还发布我的配置文件:

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"]
}

jest.config.ts: 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',
};

Enzyme setup:酶设置:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';

configure({ adapter: new Adapter(), disableLifecycleMethods: 

Have you tried to change the name of the file?您是否尝试过更改文件的名称? MyComponent.test.tsx Also, did you install the types of jest and stuff npm i -D @types/jest . MyComponent.test.tsx 另外,您是否安装了 jest 的类型和东西npm i -D @types/jest I mean I'm saying this because if you look at the jest config where it says testRegex.我的意思是我这么说是因为如果你看一下它说 testRegex 的开玩笑配置。 You have it like this __tests__/*.(test|spec).txs the test must be inside a tests folder and it has to have the name: MyComponent.你有这样的__tests__/*.(test|spec).txs测试必须在测试文件夹中,并且它必须具有名称:MyComponent。 test .tsx test .tsx

First npm install -D @types/jest & and write the test files as fileName.test.tsx My mistake was I was writing all the test files as fileName.test.ts首先npm install -D @types/jest & 并将测试文件写入fileName.test.tsx我的错误是我将所有测试文件都写入fileName.test.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出时类型“仅指一种类型,但在此处用作值” - Type “only refers to a type, but is being used as a value here” when exporting 元素指的是一个值,但在这里被用作类型 - element refers to a value, but is being used as a type here &#39;ItemIsLoading&#39;on指的是一种类型,但此处被用作值 - 'ItemIsLoading' on refers to a type, but is being used as a value here 指的是一个值,但在这里被用作一个类型 - refers to a value, but is being used as a type here &#39;MapEditServiceConfig&#39;仅引用类型,但在此处用作值 - 'MapEditServiceConfig' only refers to a type, but is being used as a value here “承诺”仅指类型,但在此处用作值 - 'Promise' only refers to a type, but is being used as a value here “&#39;输出&#39;仅指一种类型,但在此处使用了一个值”错误 - "'Output' only refers to a type, but is being used a value here" error “post”仅指一种类型,但在此处用作值 - 'post' only refers to a type, but is being used as a value here ionic 'DocumentScanner' 指的是一个值,但在这里被用作一个类型 - ionic 'DocumentScanner' refers to a value, but is being used as a type here IFormContext 只引用一个类型,但在这里用作一个值 - IFormContext only refers to a type, but is used as a value here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM