How to mock specific RequireJs dependencies while unit testing

573 views Asked by At

I use qUnit in the browser to unit test my pet project. The module I am unit testing can look like:

// Filename: views/base.js

define(['../manager'], function(Manager){

  return Backbone.View.extend({

   //...

});

I want to mock out the dependency "../manager" (manager.js) I tried using testr.js and Squire.js both of which suggest to modify the path to the config for paths.

Example:

testr.config({
    root: '../',
    baseUrl: 'src',
    stubUrl: 'tests/mocks',
    ignore: ['jquery', 'underscore', 'backbone'],
    whitelist: ['tests/unit']
});

The problem I see after I run this code is that the module I am testing (base.js) is also mocked out as well as unit test module because they are included as dependencies in unit-test runner:

define([
  'tests/unit/test-unit',
  'tests/unit/views/base-view-unit',
], function(Test, BaseViewTest){

// ...

});

Console output:

http://localhost:63342/project/tests/mocks/js/views/base.stub.js 404 (Not Found)
http://localhost:63342/project/tests/mocks/tests/unit/views/base-view-unit.stub.js 404 (Not Found)

I could add those two files into white list but that won't resolve my problem.

I guess my question is: how to mock out the deps of the file I am testing right now but not the file itself when it is included into the unit test runner?

1

There are 1 answers

0
etual On

Ok I've found the possible answer in this post saying that I need to use the separate config.js to Load Mock Dependencies From Different Paths. Yuk.