Skip to content

Latest commit

 

History

History
55 lines (46 loc) · 2.5 KB

TESTING.md

File metadata and controls

55 lines (46 loc) · 2.5 KB

Writing and Running Tests

Tools:

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:

How to write a test:

The Meteor docs have a great introduction about different types of tests, but basically the general steps are:

  1. Create a file that follows the test naming convention: *.test[s].* (e.g. methods.tests.js)
  2. 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';
  1. 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';
  1. Specify where we want to run the test. Example:
  if (Meteor.isServer) {
    // tests here
  }
  1. 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');
    });
  });

Notes:

  • 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.

Running Tests:

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.