简体   繁体   English

开玩笑找不到我的测试中导入的组件

[英]Jest can't find component imported in my test

My branch: https://github.com/Futuratum/moonholdings.io/tree/JestTests 我的分支机构: https : //github.com/Futuratum/moonholdings.io/tree/JestTests

I'm using NextJS to build my app, and also using TypeScript. 我正在使用NextJS来构建我的应用程序,也正在使用TypeScript。 So there isn't a Webpack file I edit, Next handles that. 因此,没有我编辑的Webpack文件,Next可以处理。 Full package.json below. 完整的package.json如下。

My super simple test (no errors in VSCode): 我的超级简单测试(VSCode中没有错误):

import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';

import Astronaut from './Astronaut';

describe('<Astronaut /> component', () => {
  console.log('Astronaut', Astronaut);
  describe('when rendering', () => {
    const wrapper = shallow(<Astronaut {...props} />);

    it('should render a component matching the snapshot', () => {
      const tree = toJson(wrapper);
      expect(tree).toMatchSnapshot();
      expect(wrapper).toHaveLength(1);
    });
  });
});

Folder structure: 资料夹结构:

在此处输入图片说明

The error: 错误:

在此处输入图片说明

Any thoughts of why it's complaining? 对它为什么抱怨有任何想法吗?

My Jest config: 我的笑话配置:

"jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      "\\.(gql|graphql)$": "jest-transform-graphql",
      ".*": "babel-jest",
      "^.+\\.js?$": "babel-jest"
    }
  }

jest.setup.js jest.setup.js

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

configure({ adapter: new Adapter() });

package.json 的package.json

{
  "name": "moonholdings.io",
  "version": "2.0.0",
  "description": "Moonholdings.io",
  "main": "index.ts",
  "scripts": {
    "dev": "next -p 7777",
    "build": "next build",
    "start": "next start -p 8000",
    "test": "NODE_ENV=test jest --watch",
    "test-win": "SET NODE_ENV=test&& jest --watch",
    "heroku-postbuild": "next build"
  },
  "author": "Futuratum",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "@zeit/next-typescript": "^1.1.1",
    "apollo-boost": "^0.1.16",
    "apollo-client": "^2.4.2",
    "decko": "^1.2.0",
    "downshift": "^2.2.3",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "graphql": "^14.0.2",
    "graphql-tag": "^2.9.2",
    "graphql-tools": "^4.0.0",
    "lodash.debounce": "^4.0.8",
    "next": "^7.0.2",
    "next-with-apollo": "^3.1.3",
    "node-sass": "^4.11.0",
    "nprogress": "^0.2.0",
    "prop-types": "^15.6.2",
    "react": "^16.7.0",
    "react-adopt": "^0.6.0",
    "react-apollo": "^2.2.1",
    "react-dom": "^16.7.0",
    "react-transition-group": "^2.5.0",
    "styled-components": "^3.4.9",
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.2.4",
    "waait": "^1.0.2"
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.3.0",
    "@babel/preset-typescript": "^7.1.0",
    "@types/next": "^7.0.6",
    "@types/react": "^16.7.20",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^7.0.0",
    "@types/zeit__next-typescript": "^0.1.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "babel-plugin-styled-components": "^1.7.1",
    "casual": "^1.5.19",
    "enzyme-to-json": "^3.3.4",
    "jest": "^23.6.0",
    "jest-transform-graphql": "^2.1.0"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      "\\.(gql|graphql)$": "jest-transform-graphql",
      ".*": "babel-jest",
      "^.+\\.js?$\\.tx?$\\.tsx?$": "babel-jest"
    }
  }
}

您也可以将__tests__文件夹放置在Astronaut文件夹中,然后将Austronaut.tsx作为“ ../Astronaut.tsx”导入到测试中。

Ok for this to work, I needed to move my test into __tests__ in the root. 为了使此工作正常,我需要将测试移至__tests__的根目录中。 Then import the component like so: 然后像这样导入组件:

import Astronaut from '../components/Astronaut/Astronaut.tsx';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM