Jasmine and demand in Reshar 7

I am trying to run JavaScript code using jasmine and Resharper 7 in visual studio 2012. I am following the AMD pattern using requirejs. However, I was unable to complete my test run in the Resharper tester.

Could anyone do something like this?

+4
source share
3 answers

Use the requireJS named module

define("my/sut", function () { var MySut = function () { return { answer: 42 }; }; return MySut; }); 

And initialize SUT with asyncronus Jasmine support. Do not forget the links!

 /// <reference path="~/Scripts/require.js"/> /// <reference path="../code/sut.js" /> describe("requireJS with Jasmine and Resharper", function () { it("should be executed", function () { // init SUT async var sut; runs(function () { require(['my/sut'], function (MyModel) { sut = new MyModel(); }); }); waitsFor(function () { return sut; }, "The Value should be incremented", 100); // run the test runs(function () { expect(sut.answer).toBe(42); }); }); }); 

I hope this works with a lot of modules. In my case, it worked with waitsFor '0' ms.

+5
source

A simplified version of mgsdev's answer, put the download code in the beforeEach statement, so you can write short expectations.

module:

 define("stringCalculator", function () { return { calculate: function (string) { var result = 0; string.split("+").forEach(function(number) { result += parseInt(number); }); return result; } }; }); 

test file: if the calculator is undefined, it will try to load it before each wait.

 /// <reference path="~/Scripts/require.js"/> /// <reference path="~/code/stringCalculator.js"/> describe("string calculator", function () { var calculator; beforeEach(function () { if (!calculator) { require(["stringCalculator"], function (stringCalculator) { calculator = stringCalculator; }); waitsFor(function () { return calculator; }, "loading external module", 1000); } }); it("should add 1 and 2", function () { var result = calculator.calculate("1+2"); expect(result).toEqual(3); }); it("should add 2 and 2", function () { var result = calculator.calculate("2+2"); expect(result).toEqual(4); }); it("should add 1, 2 and 3", function() { var result = calculator.calculate("1+2+3"); expect(result).toEqual(6); }); }); 
+1
source

If you are using Jasmine 2.0 or later, you can support asynchronous calls in your code. Just pass the done parameter to the function for it unit test, then call done () when you loaded your modules:

 describe("add function", function(){ it("calls renderNew", function(done){ define("taskRenderer", [], function() { return { renderNew: function(){} }; }); require(["taskRenderer"], function(taskRenderer) { spyOn(taskRenderer, "renderNew"); taskRenderer.renderNew(); expect(taskRenderer.renderNew).toHaveBeenCalled(); done(); }); } } 

Thus, you do not run your test until the modules are loaded and the test is prematurely marked as completed.

0
source

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


All Articles