Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (50 sloc) 1.86 KB
title description disqusPage
Writing Tests is so joyful!
Chapter 3: Testing


Because your code grows and you make less mistakes. Because it's often faster for you to write a service and test it in a simple unit-test, rather then going on browser for manual testing.

There are so many articles about why testing is mandatory. Just take my word for it.


This article assumes you thoroughly read:

If you decoupled your code nicely, it's so easy to write tests:

meteor add practicalmeteor:mocha
meteor npm i --save-dev chai sinon
// package.json
  "scripts": {
     "test": "meteor test --driver-package practicalmeteor:mocha --port 3050"

Now you can easily do in your terminal: npm run test. Then open your browser on http://localhost:3050 to see the results, they will be updated live as you write your test.

Test files should end in *.test.js and they should have a parent (not necessarily direct) with name client or server depending where we want to run them.

// file: /imports/api/posts/testing/server/PostService.test.js

import {assert} from 'chai';
import PostService from '/imports/api/posts/services/PostService';

describe('Post Service', function () {
    it('Should be able to create a post', function () {
        const postId = PostService.createPost({title: 'Hello'})


Continous Integration

Whenever someone does a Pull-Request, you can see if the code breaks some tests or not. This is invaluable, and so easy to implement. Really!

Some good CI tools: