majus:testing

v0.0.3Published 2 years ago

Overview

Common API used for an automated testing in Meteor.

Exports

  • Common testing API from @majus/testing Node package tossed through: sinon, chai, etc.

  • Refactored StubCollections API: with support for models from jagi:astronomy Meteor package

Installation

meteor add majus:testing

StubCollections API

Inspired by hwillson:stub-collections Meteor package:

Easily stub out Meteor collections with in-memory local collections. The idea here is to allow the use of things like Factories for unit tests and styleguides without having to restrict ourselves to making components "pure". So a component (ie. a template) can still call Widgets.findOne(widgetId), it's just that we will have stubbed out Widgets to point to a local collection that we can completely control in our test.

  • StubCollections.stub(collection1, collection2, ...) – stub a specific list of collections
  • StubCollections.restore() – undo stubbing (call at the end of tests, on routing away, etc.)

Usage examples

1import { expect, sinon, StubCollections } from 'meteor/majus:testing';
2import { MyModel, MyCollection, myFunction } from './source';
3
4describe('My tests', () => {
5
6  beforeEach(() => {
7    StubCollections.stub(MyCollection, MyModel.getCollection());
8  });
9
10  afterEach(() => {
11    StubCollections.restore();
12    sinon.restore();
13  });
14
15  test('myFunction returns current user id', () => {
16    const itemId = MyCollection.insert({});
17    sinon.stub(Meteor, 'userId').returns('xxx');
18    const result = myFunction(itemId);
19    expect(result).to.be.equal('xxx');
20  });
21  
22});