TypeError:响应不是Expo SDK 33中的构造函数

时间:2019-07-04 04:20:59

标签: reactjs typescript jestjs expo

从32升级到Expo SDK 33后,玩笑中存在运行问题。在模拟获取返回值时,Jest抱怨Response不是构造函数。

const mockGet = jest.fn();
mockGet.mockReturnValue(new Response(
    JSON.stringify({
        data: true
        }),
    { status: 200 }
    ));

以下是相关软件包的当前package.json的摘要。

{
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@types/enzyme": "3.9.1",
    "@types/expo": "^32.0.13",
    "@types/jest": "23.3.10",
    "@types/react": "^16.5.0",
    "@types/react-native": "^0.57.20",
    "babel-core": "^7.0.0-0",
    "babel-eslint": "10.0.1",
    "babel-jest": "23.6.0",
    "babel-preset-expo": "^5.0.0",
    "enzyme": "3.9.0",
    "enzyme-adapter-react-16": "1.12.1",
    "enzyme-to-json": "3.3.5",
    "eslint": "5.11.0",
    "jest": "24.8.0",
    "jest-cli": "24.8.0",
    "jest-expo": "^33.0.0",
    "react-dom": "16.8.6",
    "raf": "3.3.2",
    "ts-jest": "^24.0.2",
    "tslint": "5.12.0",
    "typescript": "3.5.2"
  },
  "scripts": {
    "jest": "node node_modules/jest-expo/bin/jest.js --runInBand",
    "lint": "tslint src/**/*.ts",
    "test": "npm run jest && tslint src/**/*.tsx && eslint .",
  },
  "jest": {
    "preset": "jest-expo",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json"
    ],
    "setupFiles": [
      "raf/polyfill",
      "<rootDir>/test-setup.js"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
      "\\.(ts|tsx)$": "ts-jest"
    }
  },
  "dependencies": {
    "expo": "^33.0.0",
    "react": "16.8.3",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
  }

从32升级到33之前,使用Response对象的这些测试已成功进行。现在,升级后,测试套件会引发TypeError。

更改版本的唯一模块是jest-expo(32.0.0-> ^ 33.0.0),ts-jest(23.10.5-> ^ 24.0.2),typescript( 3.4.3-> 3.5.2),最后是expo(^ 32.0.0-> ^ 33.0.0)。 react-native模块也已从Expo react-native存档文件sdk-32更新为sdk-33。

我尝试了不同版本的模块,以尝试查看Response的定义在哪里更改。例如,我将jest-expo从v33降级到v32,而expo是v33。这没有帮助,因为它导致测试套件完全失败,如下所示。

● Test suite failed to run

    Cannot find module 'expo-react-native-adapter' from 'setup.js'

      at Resolver.resolveModule (node_modules/@jest/core/node_modules/jest-resolve/build/index.js:230:17)
      at Object.<anonymous> (node_modules/jest-expo/src/setup.js:203:6)

在测试期间如何阻止TypeError出现的任何帮助将不胜感激。注意TypeScript在intellij中未检测到此类型错误也可能会有所帮助。

1 个答案:

答案 0 :(得分:0)

我发现的问题是jest-expo模块内部的模拟如何定义。通过创建补丁程序(使用patch-package模块),我能够解决此问题。该修补程序在下面。您可以将其添加到根目录中的“ patches / jest-expo + 33.X.X.patch”,然后将“ postinstall”:“ patch-package”安装后钩子添加到package.json文件。

driver.find_element_by_xpath("//*[@class='toolbar__menu-toggle-icon']").click();