javascript - Testing requireJS methods async with Jasmine -
i trying test function requires module using jasmine , requirejs. here 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 async method. how can perform test it?
note: dont want change code, tests describe
function.
for testing of asynchronous stuff check runs()
, waits()
, waitsfor()
:
https://github.com/pivotal/jasmine/wiki/asynchronous-specs
though way looks bit ugly me, therefore consider following options.
1. i'd recommend try jasmine.async allows write asynchronous test cases in way:
// run async expectation async.it("did stuff", function(done) { // simulate async code: settimeout(function() { expect(1).tobe(1); // async stuff done, , spec asserted done(); }); });
2. can run tests inside require
's callback:
require([ 'testmodule', 'anothertestmodule' ], function(testmodule, anothertestmodule) { describe('my great modules', function() { describe('testmodule', function() { it('should defined', function() { expect(testmodule).tobedefined(); }); }); describe('anothertestmodule', function() { it('should defined', function() { expect(anotertestmodule).tobedefined(); }); }); }); });
3. point guess code not working you're expecting:
var test = function() { require(['testmodule'], function(testmodule) { return testmodule + 1; }); };
because if call test()
, won't return testmodule + 1
.
Comments
Post a Comment