Testing requires the use of JSine async methods with Jasmine

I am trying to test a function that requires a module using jasmine and requirejs . Here is the 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 do a test with it?

Note. I do not want to change my code, just my tests describe .

+4
source share
1 answer

To test asynchronous verification of material runs() , waits() and waitsFor() :

https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

Although this method looks a little ugly, as for me, so you can also consider the following options.

1. I would recommend you try jasmine.async , which allows you to write asynchronous test cases as follows:

 // Run an async expectation async.it("did stuff", function(done) { // Simulate async code: setTimeout(function() { expect(1).toBe(1); // All async stuff done, and spec asserted done(); }); }); 

2. You can also run tests inside the require callback:

 require([ 'testModule', 'anotherTestModule' ], function(testModule, anotherTestModule) { describe('My Great Modules', function() { describe('testModule', function() { it('should be defined', function() { expect(testModule).toBeDefined(); }); }); describe('anotherTestModule', function() { it('should be defined', function() { expect(anoterTestModule).toBeDefined(); }); }); }); }); 

3. Another point: I think this code does not work as you expect:

 var test = function() { require(['testModule'], function(testModule) { return testModule + 1; }); }; 

Because if you call test() , it will not return you testModule + 1 .

+4
source

Source: https://habr.com/ru/post/1480807/


All Articles