Skip to content

RequireJS with Teaspoon

jejacks0n edited this page Jun 16, 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 ran. Similar to below:

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

require(['Teaspoon/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, you can use the 'Teaspoon' path prefix to include that file. If you had your behavior in the specs/javascripts/behaviors directory, it would look like so:

SomeModel_spec.js

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