universe:test-hooks

v1.0.0-rc.1Published 6 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

universe:test-hooks (Meteor Test Driver)

This package is a Meteor Test Driver - special Atmosphere package intended to run in a test command, e.g.

meteor test --driver-package universe:test-hooks

Installation

Package could be used directly in an app:

meteor add universe:test-hooks

or as an dependency of another test driver package:

1Package.onUse(api => {
2    /* ... */
3    api.use('universe:test-hooks');
4    /* ... */
5});

Usage

universe:test-hooks are simple and minimalistic by design.

Only purpose is to unify and expose Meteor's internal test driver API's to main app (or other packages) to make use of them.

There are two public methods exported by this package you can make use of:

onTest

Takes a callback that will be called when app is ready to be tested. In this callback you should orchestrate your testing framework and run the tests.

Callback you should either return a promise or provide return value to a done callback, otherwise tests will never end!

If return value is an error then tests are considered failed, otherwise they pass.

1import {onTest} from 'meteor/universe:test-hooks';
2
3// Promise async mode
4onTest(async () => {
5    const isOK = await runYourTests();
6    
7    if (!isOK) {
8        return new Error('Nope');
9    }
10});
11
12// Callback async mode
13onTest(done => {
14    runYourTests()
15        .then(() => done())
16        .catch(error => done(error))
17});

onInterrupt

Its quite ofter that tests are interrupted in the process, e.g. when Meteor restarts due to code change. You may want to react to this situation and do some cleaning, e.g. close browser windows in case of E2E test etc. onInterrupt gives you such possibility.

API is consistent with onTest, it takes a callback and you should either return a promise or call done when you're done :)

1import {onInterrupt} from 'meteor/universe:test-hooks';
2
3onInterrupt(() => {
4    // do some cleanup
5});

This method is available only on the server, as there is no purpose for it on the client.

Examples

With Mocha

1import {onTest} from 'meteor/universe:test-hooks';
2import Mocha from 'mocha';
3
4// Create new Mocha instance, you probably want to do it in some other file
5export const mocha = new Mocha();
6
7// Make sure that your test code (describe, it etc.) are registered before and using above mocha instance
8import 'your/test/suites';
9
10onTest(done => {
11    // Run the tests using Mocha
12    mocha.run(errorCount => {
13        done(errorCount > 0 ? new Error() : null);
14    });
15});