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.
Main purpose is to unify and expose Meteor's internal test driver API's, to be used directly in application (or other packages).
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 callback returns an error (or promise throws 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 an error, don't throw it 9 return new Error('Nope'); 10 } 11}); 12 13// Callback async mode 14onTest(done => { 15 runYourTests() 16 .then(() => done()) 17 .catch(error => done(error)) 18});
onInterrupt
This method is available only on the server, as there is no purpose for it on the client.
It's quite often 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
.
In case of async operation you should either return a promise or call done
when you're done.
Return values are ignored.
1import {onInterrupt} from 'meteor/universe:test-hooks'; 2 3onInterrupt(() => { 4 // do some cleanup 5});
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 using the same mocha instance as above 8import 'your/test/suites'; 9 10onTest(done => { 11 // Run the tests using Mocha 12 mocha.run(errorCount => { 13 // If some test fails, return any error. 14 done(errorCount > 0 ? new Error() : null); 15 }); 16});