Angular 测试 - 将 Jest 与 Protractor 一起使用 [英] Angular Testing - Using Jest with Protractor

查看:25
本文介绍了Angular 测试 - 将 Jest 与 Protractor 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Angular testing, and I want to perform 2 kinds of test for my application:

  1. 单元测试 - 我选择使用 Jest,因为我可以在不打开浏览器的情况下运行我的测试,并且它还支持使用 --testNamePatern 对特定情况进行测试.
  2. 端到端测试 - 我想试用 Protractor,因为它可以在 Angular 中使用,而且还有一个庞大的 Angular 社区可以使用.
  1. Unit Test - I choose to use Jest since I can run my test without opening the browser, and it also supports testing for specific cases with --testNamePatern.
  2. End to end test - I want to try out Protractor since it is available in Angular and also has a big Angular community to work with.

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

My question is, can I use both Jest and Protractor in my application? If yes, do I need to configure anything to use both of them in my application.

推荐答案

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

You can use both jest and protractor in your application. By default the new angular cli release gives you a karma runner for unit tests and a protractor runner for end to end tests inside the same application. You are just changing Karma with Jest.

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

  1. Can I run protractor tests (end to end) with jest? No you cannot.

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

Can I run unit tests using protractor? No you cannot.

我可以在同一个应用程序中运行 protractor 进行端到端测试和 jest 进行单元测试吗?是的你可以.你只需要告诉 jest 选择哪些文件,使用量角器也是如此.

Can I run protractor for end to end tests and jest for unit tests in the same application? Yes you can. You will just need to tell jest which files to pick up and the same with protractor.

我可以在单个文件中或在一次运行中获得两个报告吗?你不能.您必须配置您的 jest runner 以打印与量角器报告不同的报告.

Can I get both the reports in a single file or a single run? No you cannot. You will have to configure your jest runner to print reports which will be different from the protractor reports.

您可以同时使用 jest 和量角器,而无需配置任何特殊的东西.这是我用于使用量角器运行 e2e 测试和使用 jest 进行灯塔测试的 package.json 的片段.

You can use both jest and protractor without configuring anything special. Here is a snippet of the package.json I am using for running e2e tests with protractor and 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"
  }
}

希望有帮助.

这篇关于Angular 测试 - 将 Jest 与 Protractor 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆