Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize unit tests and e2e tests in AngularJS?

I need to organize unit tests and end to end tests for my JavaScript Single Page Application. I'm using AngularJS Protractor/Cucumber for e2e testing and Chai for unit tests.

I have e2e and unit tests in two different folders (unit and e2e folder) and I'm currently not taking advantage of the page object design patterm. The files are unstructured and don't share a lot of code, so I'm repeating myself many times.

I recognize this approach doesn't scale up

Is there a best practice to reorganize the tests in such a way I write the least amount of code, keeping the test code DRY?

like image 854
Gianluca Ghettini Avatar asked Nov 16 '15 15:11

Gianluca Ghettini


People also ask

What is the difference between unit testing and end to end testing in Angular?

Unit tests ensure that the individual functionality of your components, services, and other Angular entities are running properly. End-to-end tests build on this by ensuring that the user's interaction with these components and services is behaving as it should.

Is E2E a unit test?

End-to-end testing is a testing process in which the tester tests a software application from the user's perspective. Unit testing is a testing process where the developer verifies that individual units of source code work correctly.


1 Answers

First of all, you should definitely switch to using the Page Object pattern and keep your page objects under a separate directory - I think it's recommended to call the directory po.

Here is a sample for you, the project structure we currently have:

$ cd e2e
$ tree -L 1
.
├── config
├── db
├── helpers
├── mocks
├── po
└── specs

config is special directory where we keep our protractor configs - there could be multiple configs - for instance, for local testing and for testing on, say, BrowserStack.

helpers is, basically, our "libs"/"utils" directory. We keep custom jasmine matchers, additional "helper" modules with helper functions. Also, we have localStorage and sessionStorage modules that are convenient wrappers around window.localStorage and window.sessionStorage objects.

mocks is a directory where we keep protractor-http-mock mocks.

po is a directory where Page Objects are defined. Each Page Object in a separate file.

specs is where all of our specs live - they are organized into subdirectories logically.


Some of the helpers libraries are made globally available via global, example:

onPrepare: function () {
     global.helpers = require("../helpers/helpers.js");
     // ...
},

Also, to make the helpers and po import more convenient and avoid traversing the directories up in the tree and to better handle the nestedness, we've switched to using requirePO and requireHelper helper function suggested by @Michael Radionov, see:

  • Using require with relative paths

I also really like the idea, proposed by @finspin, to make a node package out of each Page Object.

like image 168
alecxe Avatar answered Oct 11 '22 09:10

alecxe