Front end javascript testing using Require and Resharper

浪子不回头ぞ 提交于 2019-12-07 15:54:28
Tikkes

I finally got it working, took me like a day and a half.

Anyway I don't use resharper anymore, or it's test runner to be more precise. Chutzpah is the one I turned to in the end. This too took me some research but I got it to the point where it includes everything as I want it to.

Check this post for sure

Here is what I did:

My people.js looks like this:

define(['viewmodels/bla'], function (bla) {
    return {
        title: 'People page',
        bla: bla //testing dependencies on other viewmodels
    };
});

Then I also made a bla.js

define(function() {
    return {
       bla: "bla"
    };
});

And now for the tests:

describe('Blabla', function () {
it('require test', function (done) {
    require(['viewmodels/people'], function (people) {
        expect(people.title).toBe('People page');
        done();
    });
});

it('dependency on require test', function (done) {
    require(['viewmodels/people'], function (people) {
        console.log(people.bla);
        expect(people.bla.bla).toBe('bla');
        done();
    });
});
});

And then eventually, reading the answers on the link provided on top I had to create a Chutzpah config file to create a test harnass:

{
   "Framework": "jasmine",
   "TestHarnessReferenceMode": "AMD",
   "TestHarnessLocationMode": "SettingsFileAdjacent",
   "References" : [
       {"Path" : "../Scripts/require.js" }, 
       {"Path" : "requireConfig.js" }
   ],
   "Tests" : [
     {"Path": "specs"}
   ]
}

Now, running the tests with Visual studio test runner actually gets me everything I need and as you can see, I can now access all my viewmodels through require like so: require(['viewmodels/whateverviewmodel'], function(whateverviewmodel){....})

I hope this answer can get people on their way to testing your (Durandal)SPA using Jasmine and RequireJS.

I know my viewmodels in this answer, nor in the question itself, say much but this should get you an idea of how to go about all of this.

Small Edit

You can now also skip the callback mess with require([]... inside of the tests and build your tests like you do your viewmodels with define

define(['viewmodels/people'], function (people) {
    describe('Blabla', function () {
        it('require test', function () {
            expect(people.title).toBe('People page');
        });

        it('dependency on require test', function () {
            console.log(people.bla);
            expect(people.bla.bla).toBe('bla');
        });
    });
});

This gets you less indents and is more readable in itself.

Louis

The require call provided by RequireJS is inherently asynchronous so you need to do something like this:

it('returns true', function (done) {
    require(["people"], function(people) {
        expect(people.getTitle()).toBe('People piolmjage');

        done(); // Signal that the test is done.
    });
});

The first attempt you show in your question cannot work. That's the classical "trying to return values synchronously form asynchronous code" mistake. The second attempt with require("people") does not work either because this require call is pseudo-synchronous and will work only if the module requested is already loaded. See this answer for an explanation of how this pseudo-synchronous require works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!