2

I am trying to learn how to unit test a JavaScript file with Jasmine. At this time, I have the following

/dist/MyJavaScriptFile.min.js

function MyClass() {}
module.exports = MyClass;

MyClass.prototype.test = function(msg) {
    console.log(msg);
};

I want to test the functions in this class using Jasmine. In an attempt to do this, I have the following:

/test/unit/MyJavaScriptFile.spec.js

describe('MyClass', function() {
    beforeEach(function() {
        module('MyClass');
    });

    describe('Test', function () {
        it('should write a message', function() {
          MyClass.test('hello from the test.');
        });
    });
});

When I attempt to run these tests, from Gulp, I get an error. The error is:

Failures: 
1) MyClass Test should write a message
1.1) TypeError: object is not a function

My belief is that this is happening because /dist/MyJavaScriptFile.min.js or MyClass is not loaded as a dependency. I thought I was doing that with the module('MyClass'); line. However, I do not see how the .spec.js file or the Jasmine test runner would know the location of the definition of MyClass. What am I doing wrong?

4
  • Are you using gulp-jasmine, and where this syntax from: module('MyClass'); Maybe some kind of new js module system? Commented Jul 17, 2015 at 14:19
  • Yes I am using gulp-jasmine. Commented Jul 17, 2015 at 14:22
  • Are you using browserify for your js? Commented Jul 17, 2015 at 14:23
  • I am not using browserify. I'm running the tests purely from command-line. Commented Jul 17, 2015 at 14:28

1 Answer 1

2

For the testing with gulp-jasmine I've includes my js code to be tested to my specs using something like this:

// Path is relative to specs location
var someLibToTest = require('path/to/my/js/file'); 

As for as you using here commonjs module syntax, you can do it the same way. But this works only for console tests, not for browser.

Sign up to request clarification or add additional context in comments.

3 Comments

I think its closer. Now, I get an error that says TypeError: undefined is not a function. I added var MyLibrary = require('path/to/file'); at the top of my spec.js file. Then, in the test, I added if (MyLibrary) { console.log('library loaded'); } else { console.log('no library'); } MyLibrary.test('hello from the test.'); When this code is executed, I see library loaded and then the error. So the library is getting loaded, however, the function is not. It doesn't make sense to me.
When you write smth like this: MyClass.prototype.test = function(){...}, that means you add a method for instance of MyClass, but not to MyClass. So try var my_inst = new MyClass(); and then my_inst.test('...');
It was sooooo hard to find this clue to how to test a standalone JavaScript file with Jasmine via Node. I can't believe how hard it was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.