To @chmacās original question ā in our applications, any code thatās supposed to run on startup is exported through the package and invoked in the applicationās Meteor.startup
. We donāt put any startup
calls in packages because itās very obfuscated then as to whatās actually happening when your application is running.
One of the cool things about Meteorās packaging system is that the package manifests are dynamic (.js
), rather than static (.json
). So, like you mentioned earlier, you can conditionally exports symbols when testing. Hereās a relevant snippet from one of our packages:
// packages/grove-receiver/package.js
if ( process.env.TESTING_FLAG === '1' ) {
// In testing environment export these so they can be
// stubbed/observed appropriately
api.export([
'ParseEvent',
'Receiver',
'Grove',
'Groves'
], 'server');
}
api.export([
'OpenGroveStream',
'Spark',
], 'server');
This is from an application that has nothing in it except for the Meteor.startup
on the server. This is literally the only file, the entire application:
/// server/main.js
Meteor.startup(() => {
Spark.login({
username: process.env.SPARK_USERNAME,
password: process.env.SPARK_PASSWORD
})
.then( Meteor.bindEnvironment(function(token) {
console.log('Logged into Spark:', token);
OpenGroveStream();
}))
.catch(function(err) {
console.error('Login to Spark failed:', err);
Kadira.trackError('spark-login-error', err);
});
});
We also maintain a package.json
file for each of our applications so we can run tests much more easily. The startMongo and stopMongo stuff is because thereās a separate database instance running in addition to the default one (needed when doing high-velocity database updates). With this, running tests are as simple as saying npm test
. Or if you want to the nice HTML reporter with tests automatically re-running for a specific package, npm run pretty-receiver-tests
.
// grove-cloud package.json
{
"name": "grove-cloud",
"version": "0.1.0",
"description": "Meteor application for receiving published events from Grove hardware",
"scripts": {
"start" : "npm run startMongo && source config/development/env.sh && meteor",
"stop": "npm run killMongo",
"test" : "npm run receiver-tests",
"receiver-tests" : "npm run startMongo && source packages/grove-receiver/tests/env.sh && meteor test-packages --velocity grove:grove-receiver && npm run killMongo",
"pretty-receiver-tests" : "npm run startMongo && source packages/grove-receiver/tests/env.sh && meteor test-packages --driver-package respondly:test-reporter grove:grove-receiver && npm run killMongo",
"startMongo": "mongod --smallfiles --dbpath .db --fork --logpath /dev/null",
"killMongo": "mongo admin --eval 'db.shutdownServer()'"
},
}