繁体   English   中英

如何在 react-native 项目中安装 Detox + jest (ts-jest) + Typescript?

[英]How to install Detox + jest (ts-jest) + Typescript in a react-native project?

我试图在排毒测试中使用 Typescript。 这个要点是我能找到的最多的。 我收到一条错误消息,指出 jasmine 未定义。 通过搜索 Detox 的问题,我发现他们现在只支持 jest-circus ( https://github.com/wix/Detox/issues/2469 )。 这与我无法使用ts-jest的事实有关吗? 有没有办法用 Typescript 编写我的测试?

有人可以告诉我如何在 typescript 项目中安装它的更新示例吗?

这是我到目前为止所拥有的:

e2e/config.json

{
    "maxWorkers": 1,
    "testRunner": "jest-circus/runner",
    "testTimeout": 120000,
    "testRegex": "\\.e2e\\.ts$",
    "reporters": ["detox/runners/jest/streamlineReporter"],
    "verbose": true,
    "preset": "ts-jest",
    "testEnvironment": "node",
    "setupTestFrameworkScriptFile": "./init.ts"
}


e2e/enviroment.js

const {
  DetoxCircusEnvironment,
  SpecReporter,
  WorkerAssignReporter,
} = require('detox/runners/jest-circus')

class CustomDetoxEnvironment extends DetoxCircusEnvironment {
  constructor(config, context) {
    super(config, context)

    // Can be safely removed, if you are content with the default value (=300000ms)
    this.initTimeout = 300000

    // This takes care of generating status logs on a per-spec basis. By default, Jest only reports at file-level.
    // This is strictly optional.
    this.registerListeners({
      SpecReporter,
      WorkerAssignReporter,
    })
  }
}

module.exports = CustomDetoxEnvironment


e2e/init.ts

import {cleanup, init} from 'detox'
import 'jasmine'

const config = require('../package.json').detox as Detox.DetoxConfig
const adapter = require('detox/runners/jest/adapter')

// eslint-disable-next-line no-magic-numbers
jest.setTimeout(120000)
jasmine.getEnv().addReporter(adapter)

beforeAll(async () => {
  await init(config, {initGlobals: false})
})

beforeEach(async () => {
  await adapter.beforeEach()
})

afterAll(async () => {
  await adapter.afterAll()
  await cleanup()
})


Dev dependencies on package.json

"devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "@types/jasmine": "^3.10.3",
    "@types/jest": "^27.4.1",
    "@types/lodash.debounce": "^4.0.6",
    "@types/lodash.unescape": "^4.0.6",
    "@types/react-native": "^0.67.1",
    "@types/react-native-htmlview": "^0.12.2",
    "@types/react-native-vector-icons": "^6.4.10",
    "@types/react-native-video": "^5.0.12",
    "@types/react-test-renderer": "^17.0.1",
    "@types/styled-components": "^5.1.24",
    "@types/styled-components-react-native": "^5.1.3",
    "@types/underscore": "^1.11.4",
    "@typescript-eslint/eslint-plugin": "^5.13.0",
    "@typescript-eslint/parser": "^5.13.0",
    "babel-jest": "^26.6.3",
    "detox": "^19.5.1",
    "eslint": "^8.10.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-config-airbnb-typescript": "^16.1.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-config-react-app": "^7.0.0",
    "eslint-import-resolver-typescript": "^2.5.0",
    "eslint-loader": "^4.0.2",
    "eslint-plugin-const-case": "^1.2.2",
    "eslint-plugin-detox": "^1.0.0",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-jest": "^26.1.1",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-no-null": "^1.0.2",
    "eslint-plugin-prefer-arrow": "^1.2.3",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.29.2",
    "eslint-plugin-react-hooks": "^4.3.0",
    "husky": "^7.0.0",
    "jest": "^27.5.1",
    "jest-circus": "^27.5.1",
    "lint-staged": "^11.1.1",
    "metro-react-native-babel-preset": "^0.66.2",
    "patch-package": "^6.4.7",
    "plop": "^2.7.4",
    "postinstall-postinstall": "^2.1.0",
    "prettier": "^2.2.0",
    "prettier-eslint": "^11.0.0",
    "prettier-standard": "^16.4.1",
    "react-test-renderer": "17.0.2",
    "reactotron-react-native": "^5.0.0",
    "reactotron-redux": "^3.1.3",
    "ts-jest": "^27.1.3",
    "ts-toolbelt": "^8.0.7",
    "typescript": "^4.3.5"
  }

我当前的错误是:

Cannot find module 'jasmine' from 'init.ts'

要修复错误,您需要安装jasmine模块

您可以通过运行yarn add jasminenpm install jasmine

暂无
暂无

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

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