jasmine
Easily write and run Jasmine 2.1 tests for all your Meteor code.
Installation
meteor add sanjo:jasmine
You also need to install a Velocity Reporter package to see the test results.
meteor add velocity:html-reporter
Getting started
To learn more about testing with Meteor you should buy the Meteor Testing Manual.
History
You can find the latest changes here.
Usage
Testing an application
Tests run automatically while the app runs in development mode locally. The test results are outputted by the reporter that you have additionally installed.
Testing a package
You can also test packages directly. You can find an example here.
You can run the tests for this package with:
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package velocity:html-reporter package-to-test
For CI you just need to add the --velocity
flag:
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package velocity:html-reporter --velocity package-to-test
If your package is not located in an app you can test it with:
VELOCITY_TEST_PACKAGES=1 meteor test-packages --driver-package velocity:html-reporter --velocity ./
You can find a list of all available command options here.
Troubleshooting
Each test mode (except server unit mode) creates a log file in the folder .meteor/local/log/
. If something is not working you should have a look in the log file.
If you need help, look for an existing GitHub issue that describes your problem. If you don't find one that is exactly the same issue, create a new one.
Further reading
Examples and Tutorials
- Leaderboard example with Jasmine tests (by Jonas Aschenbrenner)
- Bullet-proof Meteor applications (by Tomas Trescak)
- Introduction to Client Integration Tests (by Web Tempest)
Testing modes
Each testing mode has different characteristics. Each testing mode has an own folder.
Server
Server Integration Test Mode
- You can run unit and integration tests inside a copy of your app.
- Place your server integration tests in the folder
tests/jasmine/server/integration/
or a subfolder of it.
Server Unit Test Mode
- You can unit test server app code.
- The Meteor API and all packages are stubbed in this mode.
- Place your server unit tests in the folder
tests/jasmine/server/unit/
or a subfolder of it.
Client
In both client modes jasmine-jquery is available.
Client Integration Test Mode
- You can test client code.
- The tests are executed directly inside the browser in a copy of your app.
- Nothing is automatically stubbed.
- Place your client integration tests in the folder
tests/jasmine/client/integration/
or a subfolder of it.
Tip: Use this mode when you want to test the communication between client and server. In other cases you should probably use the Client Unit Test mode.
Client Unit Test Mode
- You can test client code.
- The tests are executed directly inside the browser.
- Nothing is automatically stubbed (currently).
- Place your client unit tests in the folder
tests/jasmine/client/unit/
or a subfolder of it.
By default tests run in Google Chrome browser. To run in another browser use the JASMINE_BROWSER
environment variable. For example:
JASMINE_BROWSER=PhantomJS meteor [options]
Note: Tests currently only run in Google Chrome, PhantomJS, and Firefox. If you need support for another Browser please open an issue.
If you want to use PhantomJS for running your tests, you must install PhantomJS
globally with npm install -g phantomjs
.
Disabling testing modes
By default all test modes are activated. If you don't use some of the testing modes you can disable them with an environment variable:
JASMINE_SERVER_UNIT=0
JASMINE_SERVER_INTEGRATION=0
JASMINE_CLIENT_UNIT=0
JASMINE_CLIENT_INTEGRATION=0
Running tests in Continuous Integration
Use the commmand:
meteor --test --release velocity:METEOR@1.1.0.2_3
The release velocity:METEOR@1.1.0.2_3
contains a fix for running
the client integration tests.
Mocks
Mocking Meteor
This package ships with mocks for the Meteor API. You can mock the Meteor API in your tests with:
beforeEach(function () {
MeteorStubs.install();
});
afterEach(function () {
MeteorStubs.uninstall();
});
This is done automatically for server unit tests. To disable on the server for certain packages set the environment variable JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS
. For example
export JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS=dburles:factory
You need to do it yourself for your client tests if you want to write unit tests that run in the browser.
Mocking objects
You can mock any object with the global helper function mock
.
This will avoid unwanted side effects that can affect other tests.
Here is an example how to mock the Players collection of the Leaderboard example:
beforeEach(function () {
mock(window, 'Players');
});
This will mock the Players collection for each test. The original Players collection is automatically restored after each test.
Creating more complex mocks
You can also create mocks manually. I would suggest to use the following pattern:
Create a mock service with a method install
and uninstall
(example for Meteor)
- install: Saves the original object and mocks it
- uninstall: Restores the original object
This pattern allows you to enable mocks only for specific tests and have a clean and independent mock for each test.
Copyright
The code is licensed under the MIT License (see LICENSE file).
The boot.js scripts are based on code that is part of Jasmine 2.0 (LICENSE).