Meteor test driven development

TddMeteor

Tdd Problem Overview


I don't see how to do test driven development in meteor.

I don't see it mentioned anywhere in documentation or FAQ. I don't see any examples or anything like that.

I see that some packages are using Tinytest.

I would need response from developers, what is roadmap regarding this. Something along the lines of:

  • possible, no documentation, figure it out yourself
  • meteor is not built in a way that you can make testable apps
  • this is planned feature
  • etc

Tdd Solutions


Solution 1 - Tdd

Update 3: As of Meteor 1.3, meteor includes a testing guide with step-by-step instructions for unit, integration, acceptance, and load testing.

Update 2: As of November 9th, 2015, Velocity is no longer maintained. Xolv.io is focusing their efforts on Chimp, and the Meteor Development Group must choose an official testing framework.

Update: Velocity is Meteor's official testing solution as of 0.8.1.


Not much has been written about automated testing with Meteor at this time. I expect the Meteor community to evolve testing best-practices before establishing anything in the official documentation. After all, Meteor reached 0.5 this week, and things are still changing rapidly.

The good news: you can use Node.js testing tools with Meteor.

For my Meteor project, I run my unit tests with Mocha using Chai for assertions. If you don't need Chai's full feature set, I recommend using should.js instead. I only have unit tests at the moment, though you can write integration tests with Mocha as well.

Be sure to place your tests in the "tests" folder so that Meteor does not attempt to execute your tests.

Mocha supports CoffeeScript, my choice of scripting language for Meteor projects. Here's a sample Cakefile with tasks for running your Mocha tests. If you are using JS with Meteor, feel free to adapt the commands for a Makefile.

Your Meteor models will need a slight bit of modification to expose themselves to Mocha, and this requires some knowledge of how Node.js works. Think of each Node.js file as being executed within its own scope. Meteor automatically exposes objects in different files to one another, but ordinary Node applications—like Mocha—do not do this. To make our models testable by Mocha, export each Meteor model with the following CoffeeScript pattern:

# Export our class to Node.js when running
# other modules, e.g. our Mocha tests
#
# Place this at the bottom of our Model.coffee
# file after our Model class has been defined.
exports.Model = Model unless Meteor?

...and at the top of your Mocha test, import the model you wish to test:

# Need to use Coffeescript's destructuring to reference
# the object bound in the returned scope
# http://coffeescript.org/#destructuring
{Model} = require '../path/to/model'

With that, you can start writing and running unit tests with your Meteor project!

Solution 2 - Tdd

Hi all checkout laika - the whole new testing framework for meteor http://arunoda.github.io/laika/

You can test both the server and client at once.

Disclaimer: I'm the author of Laika.

Solution 3 - Tdd

I realize that this question is already answered, but I think this could use some more context, in the form of an additional answer providing said context.

I've been doing some app development with meteor, as well as package development, both by implementing a package for meteor core, as well as for atmosphere.

It sounds like your question might be actually a question in three parts:

  1. How does one run the entire meteor test suite?
  2. How does one write and run tests for individual smart packages?
  3. How does one write and run tests for his own application?

And, it also sounds like there may be a bonus question in there somewhere: 4. How can one implement continuous integration for 1, 2, and 3?

I have been talking and begun collaborating with Naomi Seyfer (@sixolet) on the meteor core team to help get definitive answers to all of these questions into the documentation.

I had submitted an initial pull request addressing 1 and 2 to meteor core: https://github.com/meteor/meteor/pull/573.

I had also recently answered this question: https://stackoverflow.com/questions/10760601/how-do-you-run-the-meteor-tests/14035031#14035031

I think that @Blackcoat has definitively answered 3, above.

As for the bonus, 4, I would suggest using circleci.com at least to do continuous integration for your own apps. They currently support the use case that @Blackcoat had described. I have a project in which I've successfully gotten tests written in coffeescript to run unit tests with mocha, pretty much as @Blackcoat had described.

For continuous integration on meteor core, and smart packages, Naomi Seyfer and I are chatting with the founder of circleci to see if we can get something awesome implemented in the near term.

Solution 4 - Tdd

RTD has now been deprecated and replaced by Velocity, which is the official testing framework for Meteor 1.0. Documentation is still relatively new as Velocity is under heavy development. You can find some more information on the Velocity Github repo, the Velocity Homepage and The Meteor Testing Manual (paid content)

Disclaimer: I'm one of the core team members of Velocity and the author of the book.


Check out RTD, a full testing framework for Meteor here rtd.xolv.io. It supports Jasmine/Mocha/custom and works with both plain JS and coffee. It includes test coverage too that combines unit/server/client coverage.

And an example project here

A blog to explain unit testing with Meteor here

An e2e acceptance testing approach using Selenium WebdriverJS and Meteor here

Hope that helps. Disclaimer: I am the author of RTD.

Solution 5 - Tdd

I used this page a lot and tried all of the answers, but from my beginner's starting point, I found them quite confusing. Once I had any trouble, I was flummoxed as to how to fix them.

This solution is really simple to get started with, if not fully documented yet, so I recommend it for people like myself who want to do TDD but aren't sure how testing in JavaScript works and which libraries plug into what:

https://github.com/mad-eye/meteor-mocha-web

FYI, I found that I also need to use the router Atmosphere package to make a '/tests' route to run and display the results from the tests, as I didn't want it to clutter my app every time it loads.

Solution 6 - Tdd

About the usage of tinytest, you may want to take a look at those useful ressources:

  1. The basics are explained in this screencast: https://www.eventedmind.com/feed/meteor-testing-packages-with-tinytest

  2. Once you understood the idea, you'll want the public API documentation for tinytest. For now, the only documentation for that is at the end of the source of the tinytest package: https://github.com/meteor/meteor/tree/devel/packages/tinytest

  3. Also, the screencast talks about test-helpers, you may want to have a look at all the available helpers in here: https://github.com/meteor/meteor/tree/devel/packages/test-helpers There often is some documentation inside each file

  4. Digging in the existing tests of meteor's packages will provide a lot of examples. One way of doing this is to make a search for Tinytest. or test. in the package directory of meteor's source code

Solution 7 - Tdd

Testing becomes a core part of Meteor in the upcoming 1.3 release. The initial solution is based on Mocha and Chai.

The original discussions of the minimum viable design can be found here and the details of the first implementation can be found here.

MDG have produced the initial bones of the guide documentation for the testing which can be found here, and there are some example tests here.

This is an example of a publication test from the link above:

> it('sends all todos for a public list when logged in', (done) => { > const collector = new PublicationCollector({userId}); > collector.collect('Todos.inList', publicList._id, (collections) => { > chai.assert.equal(collections.Todos.length, 3); > done(); > }); > });

Solution 8 - Tdd

I'm doing functional/integration tests with Meteor + Mocha in the browser. I have something along the lines of the following (in coffeescript for better readability):

On the client...

Meteor.startup ->
    Meteor.call 'shouldTest', (err, shouldTest) ->
        if err? then throw err
        if shouldTest then runTests()

# Dynamically load and run mocha. I factored this out in a separate method so
# that I can (re-)run the tests from the console whenever I like.
# NB: This assumes that you have your mocha/chai scripts in .../public/mocha.
# You can point to a CDN, too.
runTests = ->
    $('head').append('<link href="/mocha/mocha.css" rel="stylesheet" />')
    $.getScript '/mocha/mocha.js', ->
      $.getScript '/mocha/chai.js', ->
        $('body').append('<div id="mocha"> </div>')
        chai.should() # ... or assert or explain ...
        mocha.setup 'bdd'
        loadSpecs() # This function contains your actual describe(), etc. calls.
        mocha.run()

...and on the server:

Meteor.methods 'shouldTest': -> true unless Meteor.settings.noTests  # ... or whatever.

Of course you can do your client-side unit testing in the same way. For integration testing it's nice to have all Meteor infrastructure around, though.

Solution 9 - Tdd

As Blackcout said, Velocity is the official TDD framework for Meteor. But at this moment velocity's webpage doesn't offer good documentation. So I recommend you to watch:

Solution 10 - Tdd

Another option, made easily available since 0.6.0, is to run your entire app out of local smart packages, with a bare minimum amount of code outside of packages to boot your app (possibly invoking a particular smart package that is the foundation of your app).

You can then leverage Meteor's Tinytest, which is great for testing Meteor apps.

Solution 11 - Tdd

Ive successfully been using xolvio:cucumber and velocity to do my testing. Works really well and runs continuously so you can always see that your tests are passing.

Solution 12 - Tdd

Meteor + TheIntern

Somehow I managed to test Meteor application with TheIntern.js.

Though it is as per my need. But still I think it may lead someone to the right direction and I am sharing what I have done to resolve this issue.

There is a execute function which allows us to run JS code thorugh which we can access browsers window object and hence Meteor also.

Want to know more about execute

This is how my test suite looks for Functional Testing

define(function (require) {
    var registerSuite = require('intern!object');
    var assert = require('intern/chai!assert');
    registerSuite({
        name: 'index',

        'greeting form': function () {
            var rem = this.remote;
            return this.remote
                .get(require.toUrl('localhost:3000'))
                .setFindTimeout(5000)
                .execute(function() {
                        console.log("browser window object", window)
                        return Products.find({}).fetch().length
                    })
                .then(function (text) {
                    console.log(text)
                    assert.strictEqual(text, 2,
                        'Yes I can access Meteor and its Collections');
                });
        }
    });
});

To know more, this is my gist

Note: I am still in very early phase with this solution. I don't know whether I can do complex testing with this or not. But i am pretty much confident about it.

Solution 13 - Tdd

Velocity is not mature yet. I am facing setTimeout issues to use velocity. For server side unit testing you can use this package.

It is faster than velocity. Velocity requires a huge time when I test any spec with a login. With Jasmine code we can test any server side method and publication.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRubycutView Question on Stackoverflow
Solution 1 - TddBlackcoatView Answer on Stackoverflow
Solution 2 - TddArunoda SusiripalaView Answer on Stackoverflow
Solution 3 - TddzealoushackerView Answer on Stackoverflow
Solution 4 - TddXolv.ioView Answer on Stackoverflow
Solution 5 - TddpipedreambombView Answer on Stackoverflow
Solution 6 - TddWilliam LedouxView Answer on Stackoverflow
Solution 7 - TddtomRedoxView Answer on Stackoverflow
Solution 8 - TddjericoView Answer on Stackoverflow
Solution 9 - TddAdrian LopezView Answer on Stackoverflow
Solution 10 - Tddmatb33View Answer on Stackoverflow
Solution 11 - TddPhilip BeadleView Answer on Stackoverflow
Solution 12 - TddHarpreet SinghView Answer on Stackoverflow
Solution 13 - TddZahedView Answer on Stackoverflow