With some help from Google, I found a solution to this. I canβt say that it is perfect, but it works at least, and in each test the required module is in it intact.
Firstly, you need to have a test file main.js , like this, it is almost the same as in docs , except for tests that are not defined as modules and are not included as dependencies:
require.config({file baseUrl: '/base', paths: {}, shim: {}, deps: [], callback: window.__karma__.start });
This should be present in the main configuration of karma (you need to adapt your own paths)
files: [ {pattern: 'src/**/*.js', included: false}, 'tests/unit/**/*.js' ],
And in the tests themselves, we use the capabilities of asynchronous jasmine tests and the requirejs.undef method to "clean up" the module (which will reset the internal state of the bootloader to forget about the previous module definition, as indicated in docs ).
This method has one implication that it will not reset other modules that are already loaded and depend on the reset module, but this should not be a problem if you write its tests correctly, that is, it checks only one module (and there may be some parent , from which the child inherits some behavior), so it should not be difficult to undef several modules in them.
describe('Some/Module tests', function() { 'use strict' var M beforeEach(function(done) { requirejs.undef('src/Some/GParentModule') requirejs.undef('src/Some/ParentModule') requirejs.undef('src/Some/Module') require(['src/Some/Module'], function(m) { M = m done() }) }) it('some description', function() { var i = 0 for (var prop in M) { i++ } expect(i).toEqual(1) expect(prop).toEqual('init') expect(M.init).toEqual(jasmine.any(Function)) }) })
I checked this, between it change calls are not saved in the module, so I believe it is safe to use this approach.
Thank you all for your help, if anyone has a better way, answer here.