Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: unit tests #250

Merged
merged 5 commits into from
Apr 4, 2016
Merged

Conversation

sfount
Copy link
Contributor

@sfount sfount commented Apr 1, 2016

This pull request adds the karma unit test runner as described in #234.

This pull request focusses on demonstrating the testing of services and directives as controllers will mostly act as 'connectors' or 'glue' between these entities - controllers can also be tested using protractor end to end tests.

Unit testing directives and services as shown in this pull request will provide a number of benefits:

  1. Increase code coverage (functionality tested)
  2. Allow for more specific, smaller (reliable) tests

Where end to end tests have been used to verify components and directives, small and specific unit tests can be used in their place - this allows for far fewer variables and should increase the number of cases each test can account for

  1. Test previously non testable code - angular services have not yet been tested (despite being used across many controllers)
  2. Increase the speed of testing by 99.99*%!

Currently all of our client side testing is done using protractor/ selenium end to end tests - this provides a very broad test recreating the exact environment that BHIMA will run in. Currently these end to end tests take 5 minutes to run on an up to date development machine (Chromebook Pixel 2015).

Unit tests run extremely fast as they do not have to not need to run a server, load all vendor files and bootstrap angular exactly as it would when a client is using it - they are designed to test very specific (and small) pieces of code.

Any end to end tests that are currently designed to test form validation or directives/ components can be removed allowing a focus on more broad module functionality in end to end tests (and removing many of the tests!)

The following components have been tested as examples in this pull request:

  • directive : bh-unique
  • directive : bh-integer
  • service : SessionService

These tests demonstrate how to:

  • Set up the test environment
  • Include any required angular components for testing
  • Compile angular elements into scopes (in the case of directives)
  • Mock angular services with known responses

Performance
Tests performed generally put running these 9 tests (testing 3 core components) at taking 0.01 seconds.

[2016-04-01 08:38:30.431] [DEBUG] config - Loading config /home/sfount/IMA/BHIMA/bhima-2.X/karma.conf.js
 Chrome 48.0.2564 (Linux 0.0.0): Executed 9 of 9 SUCCESS (0 secs / 0.009 secs)
.
Chrome 48.0.2564 (Linux 0.0.0): Executed 9 of 9 SUCCESS (0.109 secs / 0.009 secs)

shell screenshot

  • Session service injection is temperamental across branches - succeeding and failing Update: session service test issue caused by re basing with master. Session service was updated with a breaking API change.
  • Add performance metrics

Closes #234


  • Run your code through JSHint. Check out our styleguide
  • Run integration tests.
  • Run end-to-end tests.
  • Accurately described the changes your are making in this pull request.

@sfount sfount changed the title Testing unit tests Testing: unit tests Apr 1, 2016
@sfount sfount added this to the 2.x Process milestone Apr 1, 2016
@jniles
Copy link
Collaborator

jniles commented Apr 1, 2016

This looks awesome. It will be nice to offload some of our end-to-end test work into faster, smaller unit tests.

This commit introduces the karma testing framework. This will allow us
to unit test angular components, directives, controllers, services etc.
Unit tests are independent of the BHIMA framework, run very quickly will
allow us to test very small units of functionality independent of end to
end tests (freeing up end to end tests to focus on more abstract
functionality).

Karma and angular mocks dependencies are added to package and bower JSON
files respectively.

`npm install -g karma-cli` can also be run in order to configure the
karma server independently of the build process.

Example use (independent of build process):
`karma start` - starts the karma server and opens up a Google
Chrome instance. This is what will power the test .spec.js files

`karma run` - runs all karma tests specified in the karma.conf.js

Karma is currently configured to watch for and re-run on file changes,
any changes to `client/test/unit/**.*.spec.js` will trigger a test run.
This commit updates the previously commited session service unit test;
* Updated dependency injection insuring required services are available
  to the test
* Updated framework syntax (expect) to ensure all assertions are
  compatible with chai
* Ensure all tests make logical sense and pass when run in with the
  `karma` test runner
This commit adds an initial attempt at running a directive unit test.
Testing the `bh-integer` directive.

The steps for testing a directive:
1. Load the module `bhima.directives`
2. Inject `$compile` and `$rootScope`
3. Compile a basic template including the directive into the root scope
4. Set, update and remove values - make assertions on results

This test runner verifies that the Integer validation directive:
* validates an integer value
* blocks non integer values (string/ decimal)
This commit implements karma unit tests for the bh-unique angular
directive. This is included in this pull request to demonstrate key unit
test features:
* Mocking service responses that are injected into
* controllers/directives/services (using $provide)
* Proving why separate small components are much easier to test -
* mocking up services allows providing guaranteed fake data for more
* complex validation (in the unique directive)
Testing the more complex $asyncValidators on the directives

This test validates that the bh-unique directive responses to two
different input:
* 'rejects a value that already exists'
Value already exists in the mock service (Array) -> $scope model should
have no value, form should be $invalid

* 'accepts a value that in unique'
Value does not exist in the mock service (Array) -> $scope model should
exist and equal the input value, form should be $valid
The client side SessionService was updated in a pull request that was
accepted into `master`. This test no longer makes sense with the new API
and should be re-created to fit the new service.
@sfount sfount merged commit 0fed278 into IMA-WorldHealth:master Apr 4, 2016
@sfount sfount deleted the testing-unit-tests branch April 4, 2016 10:46
jniles referenced this pull request in jniles/bhima Feb 5, 2017
This commit removes the notify.warn() message that shows if you have
been logged in previously and still have a valid session on the client
but not on the server.

It also improves the speed of the checks by converting them to cached
regular expressions.

Closes #250.
jniles referenced this pull request in jniles/bhima Feb 5, 2017
This commit removes the notify.warn() message that shows if you have
been logged in previously and still have a valid session on the client
but not on the server.

It also improves the speed of the checks by converting them to cached
regular expressions.

Closes #250.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants