How to test js spa modules with resharper testr jasmin?

After reading about unit testing javascript / bdd in VS, I found that you can use a combination:

- ReSharper - support for PhantomJS headless + Jasmine/QUnit - Testr - mock Require dependencies 

I used Jasmine in a test script and was able to successfully run some simple tests with functions declared in the same file.

However, I could not find / build the working end to the end for testing the js module with dependencies. I am trying to use the example used in the John Pap Jumpstart spa example.

Therefore, the module view.mod.js is specified, which has a dependency on datacontext.js:

 define(['services/datacontext'], function (datacontext) { var peopleViewModel = { title: 'People page' }; return peopleViewModel; }) 

Folder structure:

 /App/Viewmodels : people.js /App/Services : datacontext.js /App/Tests : peopletests.js 

What do I need to add to the peopletests.js file to run this test?

 describe("My Tests Set", function () { it("People Title Test", function () { expect(peopleViewModel.title()).toEqual("People page"); }); }); 
+1
source share
1 answer

try the following:

  • add require.js as a reference path
  • add require.config script as a reference path
  • require loading modules.

peopletests.js:

 /// <reference path="~/Scripts/require.js"/> /// <reference path="~/App/requireConfig.js"/> describe("My Tests Set", function () { var people; beforeEach(function () { if (!people) { //if people is undefined it will try to load it require(["people"], function (peopleViewModel) { people = peopleViewModel; }); //waits for people to be defined (loaded) waitsFor(function () { return people; }, "loading external module", 1000); } }); it("People Title Test", function () { expect(people.title).toEqual("People page"); }); }); 

requireConfig.js:

 //beware of the port, if your app is runing in port 8080 //you need to specified that to require since resharper whould use a random port //when running tests require.config({ baseUrl: 'http://localhost:8080/App/', paths: { people: 'ViewModels/people', dataContext: 'Services/datacontext' } }); 

people.js

 define(['dataContext'], function (datacontext) { var peopleViewModel = { title: 'People page' }; return peopleViewModel; }) 
+1
source

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


All Articles