Angular 测试 - 将 Jest 与 Protractor 结合使用

Angular Testing - Using Jest with Protractor

我是 Angular 测试的新手,我想为我的应用程序执行两种测试:

  1. 单元测试 - 我选择使用 Jest 因为我可以 运行 我的测试而无需打开浏览器,它还支持使用 [=15] 测试特定情况=]--testNamePattern.
  2. 端到端测试 - 我想试用 Protractor,因为它在 Angular 中可用,并且还有一个很大的 Angular 社区可以使用.

我的问题是,我可以在我的应用程序中同时使用 Jest 和 Protractor 吗?如果是,我是否需要配置任何东西才能在我的应用程序中同时使用它们。

您可以在应用程序中同时使用 jest 和量角器。默认情况下,新的 angular cli 版本为您提供了用于单元测试的业力 运行ner 和用于同一应用程序内端到端测试的量角器 运行ner。你只是在用 Jest 改变 Karma。

  1. 我可以 运行 开玩笑地测试量角器(端到端)吗? 不,你不能。

  2. 我可以 运行 使用量角器进行单元测试吗? 不,你不能。

  3. 我可以在同一应用程序中 运行 用于端到端测试的量角器和用于单元测试的笑话吗? 是的你可以。你只需要告诉开玩笑要选择哪些文件,用量角器也一样。

  4. 我能否在单个文件或单个 运行 中同时获取报告? 你不能。您将必须配置您的玩笑 运行ner 以打印与量角器报告不同的报告。

您可以同时使用 jest 和 protractor,无需进行任何特殊配置。这是 package.json 的一个片段,我正在使用 运行ning e2e testing with protractor 和 lighthouse tests with jest。

{
  "name": "performance-tests",
  "version": "1.0.0",
  "description": "Performance tests and end to end tests.",
  "main": "jest.js",
  "scripts": {
    "debug": "node --inspect-brk ./node_modules/.bin/protractor protractor.conf.js",
    "pretest": "npm run tsc && npm run webdriver-update",
    "e2e": "npm run tsc && ./node_modules/protractor/bin/protractor protractor/compiled-js-files/protractor.conf.js",
    "grid": "sh run-grid.sh && npm run e2e",
    "tsc": "./node_modules/typescript/bin/tsc",
    "webdriver-update": "./node_modules/protractor/bin/webdriver-manager update --standalone --versions.standalone=3.8.0 --chrome --versions.chrome=78.0.3904.97",
    "lighthouse": "./node_modules/jest/bin/jest.js --verbose -t=lighthouse",
    "lighthouse-reports": "./node_modules/jest/bin/jest.js --verbose -t=lighthouse && node ./lighthouse/db.js"
  },
  "repository": {
    "type": "",
    "url": ""
  },
  "author": "Sankalan Parajuli",
  "license": "ISC",
  "bugs": {
    "url": ""
  },
  "homepage": "",
  "dependencies": {
    "@types/jasmine": "^3.3.12",
    "@types/jasminewd2": "^2.0.6",
    "@types/node": "^12.12.14",
    "jasmine": "^3.3.1",
    "lighthouse": "^4.0.0-beta",
    "protractor": "5.4.2",
    "protractor-beautiful-reporter": "^1.3.3"
  },
  "devDependencies": {
    "@types/request": "^2.48.3",
    "@types/selenium-webdriver": "^4.0.0",
    "csvtojson": "^2.0.8",
    "jest": "^23.4.1",
    "moment": "^2.24.0",
    "mongodb": "^3.1.13",
    "puppeteer": "^1.6.0",
    "request-promise": "^4.2.5",
    "ts-node": "^8.5.2",
    "typescript": "2.8.1"
  }
}

希望对您有所帮助。