We are currently using the dispatch:mocha-phantomjs
package as a driver package, with practicalmeteor:chai
for assertion, as well as xolvio:cleaner
, which allow us to:
- Use Mocha to write tests.
- Use Chai for assertion.
- Use PhantomJS for front-end tests.
- Clear our test databases using Xolv.io Cleaner.
The Meteor docs have a great introduction about different types of tests, but basically the general steps are:
- Create a file that follows the test naming convention: *.test[s].* (e.g.
methods.tests.js
) - Include the required variables/methods from Meteor and/or npm packages. Example:
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { Accounts } from 'meteor/accounts-base';
import { assert } from 'meteor/practicalmeteor:chai';
import { resetDatabase } from 'meteor/xolvio:cleaner';
- Import the file(s) we need to test (or use an exported variable from in our tests). Example:
// exported variables from /lib/common.js:
import { Instances, Questions, Answers, Votes } from '/lib/common.js';
// we are testing methods from ./methods.js:
import './methods.js';
- Specify where we want to run the test. Example:
if (Meteor.isServer) {
// tests here
}
- Start writing tests using Mocha/Chai. Example:
describe('#something()', function () {
it('should return something else.', function () {
// this is the actual test
assert.equal(something(), 'something else');
});
});
- Passing arrow functions to Mocha is discouraged.
- Tests run asynchronously, so don't depend on the result of another test in yours, and isolate the variables/database entries/clear the database between tests to ensure every test runs in the way/environment that you expect.
The test command is included in the app's package.json. So you can just run: npm test
in the app's directory and it will run all the tests once.
This will also run our "pretest" script, which runs ESLint. Read more about the code style guide, and what errors to expect from that here.