I am trying to test a function that requires a module using jasmine and requirejs. Here is a dummy code:
define("testModule", function() {
return 123;
});
var test = function() {
require(['testModule'], function(testModule) {
return testModule + 1;
});
}
describe("Async requirejs test", function() {
it("should works", function() {
expect(test()).toBe(124);
});
});
It fails, because it is an async method. How can I perform a test with it?
Note: I dont want to change my code, just my tests describe
function.
For testing of an asynchronous stuff check
runs()
,waits()
andwaitsFor()
:https://github.com/pivotal/jasmine/wiki/Asynchronous-specs
Though this way looks a bit ugly as for me, therefore you could also consider following options.
1. I'd recommend you to try jasmine.async that allows you to write asynchronous test cases in this way:
2. Also you can run your tests inside
require
's callback:3. Another point is I guess that this code is not working as you're expecting:
Because if you call
test()
, it won't return youtestModule + 1
.