Angular Testing - Using Jest with Protractor

不想你离开。 提交于 2020-03-05 05:16:08

问题


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

  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.

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.


回答1:


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) Can I run protractor tests (end to end) with jest? No you cannot.

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

3) 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.

4) 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.

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",
    "test": "./node_modules/protractor/bin/protractor protractor/compiled-js-files/protractor.conf.js",
    "e2e": "npm run tsc && ./node_modules/protractor/bin/protractor protractor/compiled-js-files/protractor.conf.js",
    "grid": "sh run-grid.sh && npm run tsc && 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"
  }
}

Hope it helps.



来源:https://stackoverflow.com/questions/58946139/angular-testing-using-jest-with-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!