Skip to content

RequireJS with Teaspoon

Greg Leppert edited this page Jul 2, 2013 · 8 revisions

Setting up Teaspoon

If you're using RequireJS and want full integration with Teaspoon you can set the option of "use_require" in your suite config.

config/initializers/teaspoon.rb

config.suite do |suite|
  suite.use_require = true
end

In your spec helper, you will need to include the require.js library that you use, using sprockets.

spec/javascripts/spec_helper.js

//= require require

When this test suite runs, it will embed a require configuration and a require block to load all the specs that need to be run, similar to below. If you're using a library that stores a requirejs config at Rails.application.config.requirejs.user_config, such as requirejs-rails, this config will be used, with baseUrl overridden to '/assets'.

require.config({
  paths: {
    baseUrl: '/assets'
  }
});

require(['SomeModel_spec.js'] , function () {
  Teaspoon.execute();
});

Writing your specs

In your spec, you can include your other assets. If you have custom paths setup in a require config, you may need to include that using sprockets in your spec helper.

SomeModel_spec.js

define(['SomeModel'] , function (SomeModel) {
  describe('SomeModel' , function () {
    // ADD YOUR SPEC
  });
});

Shared behaviors

If you have any shared behaviors you'd like to require, for instance in the directory specs/javascripts/behaviors, it would look like so:

SomeModel_spec.js

define(['SomeModel' , 'behaviors/SharedModel_behavior'] , function (SomeModel , SharedModel_behavior) {
  describe('SomeModel' , function () {
    SharedModel_behavior();
  });
});