How do I test template helper functions?

Hi All

I recently took over a meteor project and need to write unit tests for them.

I’m new to meteor, and new to web application testing in general so please be patient with me. I been reading the forums and doing research online for about 2 days now… but I can’t find the examples / answers I’m looking for.

I upgrade from 1.2 to 1.3 and installed praticalmeteor:mocha and chai… but I do not want to split the code to ES2015. I have the http://localhost:3100/coverage running and reading the test files from the test-package.

I’m looking for a simple syntax where I can call and template helper and get back a value…

Something like this…

var expect = chai.expect;

describe(‘Template.foo.helpers’, function () {
it(‘bar should be called and return a value’, function(){

var bar = Meteor.Template.foo.helpers[' bar'](); **// <---- this is the part I can't figure out.**

expect(bar).to.equal(whatever);

});

The error I get is TypeError: Cannot read property ‘foo’ of undefined

Thank you, any advice will be appreciated.

Same issue there with Meteor 1.3. I am looking forward for a solution, it seems that we need extra work to load templates and thus set up Template.foo with the correct data. When running Meteor, Template.foo is eagerly set up after Meteor read the .html file and found <template name="foo">, I am trying to figure how to emulate this behaviour.

Edit : you simply need to import foo.html (or whatever file containing the foo template html definition) then foo.js (or whatever file containing your template helpers definition).

An example :

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Foo } from '/collections/foo.js';
import { assert } from 'meteor/practicalmeteor:chai';
// Will define Template.foo
import './foo.html'; // your template
 // Will set up Template.foo.__helpers[' bar']
import './foo.js'; // your helpers


describe('foo handlers', () => {
    it("Should test bar", () => {
       var bar = Template.foo.__helpers[' bar'].apply(); // don't forget the space, method is ' bar' not 'bar'
    });
});

That makes sense, but how does it work with template helpers.
Template.registerHelper says Template is undefined.

1 Like