Sure. I’m hoping to do a screencast on the whole workflow testing my http://react-ive.meteor.com/ project. However, as soon as I record it it’s going to be outdated in a few months with 1.3 Perhaps i’ll use it as a second screencast for migrating to 1.3.
Anyhow, the basic gist is that you want to load all of your files with the same rough pattern that Meteor uses (in the files array). It’s easiest to start with one single folder/file and make sure that works then work outwards until you get the hang of Karma and how it’s importing (so everything isn’t blowing up at once basically).
Additionally when you discover this process of things ‘blowing up’, you’ll slowly build up stubs that have empty functions (like Meteor.user
). This is what the tests/karma/mocks.js
file is doing. Adding just enough so that it doesn’t blow up. Then with jasmine I might do something like this:
spyOn(Meteor, 'user').and.returnValue({profile: {name: 'Adam'}});
so that in my unit tests the user is pre set and only has enough to make the unit tests pass.
Here’s an older gist focused on React that has all the files in my tests/karma
directory. Note, the package.json file for packages.
https://gist.github.com/AdamBrodzinski/aeb5b669a74259da8af8
Also here’s the karma config for this. Note, you may not want to import all of the meteor files with karma like this because this is meant for unit testing… you may pull in things that you don’t want executed and at best those files would be integration tests. I tend to just manually add them in the config one at a time (or a folder) and all React components since they’re easy to unit test.
// tests/karma/karma-config.js
module.exports = function(config) {
config.set({
basePath: '../../',
frameworks: ['jasmine', 'jquery-2.1.0'],
plugins: [
'karma-babel-preprocessor',
'karma-jquery',
'karma-jasmine',
'karma-mocha-reporter',
],
autoWatch: true,
reporters: ['mocha'],
files: [
'tests/karma/mocks.js',
'tests/karma/node_modules/react/dist/react-with-addons.js',
'tests/karma/spec_helper.js',
// import lib code first
'client/lib/*.js',
'both/lib/*.js',
// import a file before all else if manually if it fails
'client/utils.js',
// you could tech. load each folder manually here if you *have* to
'client/alpha',
'client/bravo',
// otherwise load all js code #yolo
'client/*.js',
// import all tests in client dir, this allows you to have them in
// client/components/widget/tests/widet_spec.js
'client/**/*_spec.js',
],
preprocessors: {
'**/*.js': ['babel'],
'**/*.jsx': ['babel'],
'**/*_spec.js': ['babel']
},
babelPreprocessor: {
options: {
sourceMap: 'inline',
// mimics features Meteor supports, poss. out of date now?
whitelist: [
"react",
"flow",
'es3.propertyLiterals',
'es3.memberExpressionLiterals',
'es6.arrowFunctions',
'es6.templateLiterals',
'es6.classes',
'es6.blockScoping',
"es6.properties.shorthand",
"es6.properties.computed",
"es6.parameters.rest",
"es6.parameters.default",
"es6.spread",
"es6.destructuring",
"es6.constants",
"es7.objectRestSpread",
'es7.trailingFunctionCommas',
]
},
// can't remember what this is for? perhaps unneeded lol
filename: function (file) {
return file.originalPath.replace(/\.js$/, '.es5.js');
},
sourceFileName: function (file) {
return file.originalPath;
}
}
});
};