简体   繁体   English

如何配置 Jest 以处理所有导入?

[英]How to configure Jest to work with all imports?

I am new to Jest and trying to figure out how to configure it to connect with files that uses web app, expo and many different imports.我是 Jest 的新手,并试图弄清楚如何配置它以连接使用 web 应用程序、expo 和许多不同导入的文件。

my error我的错误

AIL screens/hiveInspection/HiveHealth.test.js ● Test suite failed to run AIL 屏幕/hiveInspection/HiveHealth.test.js ● 测试套件无法运行

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/../../tm-repo-se-team-5-a-hive-management-system/screens/hiveInspection/HiveHealth.js:1
import React, { useState } from "react";
^^^^^^

SyntaxError: Cannot use import statement outside a module

> 1 | hiveHealthTest = require("./HiveHealth");
    |                  ^
  2 | 
  3 | test('Lets see if hiveHealth connects', () => {
  4 |     expect(hiveHealthTest.expect(false));

imports in trial file (it applies to all the files with a bunch of imports在试用文件中导入(它适用于所有具有大量导入的文件

import React, { useState } from "react";
import { StyleSheet, View, TouchableOpacity, Text, ImagePropTypes, Button, Modal, Alert } from "react-native";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { Audio } from "expo-av";
import * as ImagePicker from 'expo-image-picker'

import SectionTitle from "../../components/sectionTitle";
import FormItem from "../../components/formItem";
import StarBox from "../../components/starBox";
import Colors from "../../constants/colors";
import ExpoStatusBar from "expo-status-bar/build/ExpoStatusBar";

my package.json我的 package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "test": "jest",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/datetimepicker": "^3.0.9",
    "expo": "~39.0.2",
    "expo-av": "~8.6.0",
    "expo-image-picker": "~9.1.0",
    "expo-sqlite": "~8.4.0",
    "expo-status-bar": "~1.0.2",
    "moment": "^2.29.1",
    "react": "16.13.1",
    "react-datepicker": "^3.3.0",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz",
    "react-native-elements": "^3.0.0-alpha.1",
    "react-native-gesture-handler": "~1.7.0",
    "react-native-masked-text": "^1.13.0",
    "react-native-web": "~0.13.12"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0",
    "@babel/preset-env": "^7.12.11",
    "jest": "^26.6.0",
    "jest-expo": "^40.0.2",
    "mocha": "^8.2.1",
    "react-test-renderer": "^17.0.1",
    "react-unit-test-generator": "^1.11.0"
  },
  "jest": {
    "verbose": true,
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less)$": "identity-obj-proxy",
      "\\.(gif|ttf|eot|svg)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "js",
      "jsx",
      "json" ],
    "moduleDirectories": [
      "node_modules", "bower_components", "shared"
    ],
    "transform": {
  
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js"
    },
    "transformIgnorePatterns": ["../node_modules/"]
  },
  "private": true
}

first: Are you using react-test-renderer ?第一:你在使用react-test-renderer吗?

With jest is different you need to have enzyme with it along with babel config. jest是不同的,你需要enzyme和 babel 配置。 https://enzymejs.github.io/enzyme/ https://enzymejs.github.io/enzyme/

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

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