anagelcg
anagelcg

Reputation: 41

Grunt-Karma: Use Node.js fs-framework in Jasmine Testfile

I'm writing unit-tests with the Jasmine-framework.

I use Grunt and Karma for running the Jasmine testfiles.

I simply want to load the content of a file on my local file-system (e.g. example.xml).

I thought I can do this:

var fs = require('fs');
var fileContent = fs.readFileSync("test/resources/example.xml").toString();
console.log(fileContent);

This works well in my Gruntfile.js and even in my karma.conf.js file, but not in my Jasmine-file. My Testfile looks like this:

describe('Some tests', function() {
    it('load xml file', function() {
        var fs = require("fs");
        fileContent = fs.readFileSync("test/resources/example.xml").toString();
        console.log(fileContent);
    });
}); 

The first error I get is:

'ReferenceError: require is not defined'.

Does not know why I cannot use RequireJS here, because I can use it in Gruntfiel.js and even in karma.conf.js?!?!?

Okay, but when manually add require.js to the files-property in karma.conf.js-file, then I get the following message:

 Module name "fs" has not been loaded yet for context: _. Use require([]) 

With the array-syntax of requirejs, nothing happens.

I guess that is not possible to access Node.js functionality in Jasmine when running the testfiles with Karma. So when Karma runs on Node.js, why is it not possible to access the 'fs'-framework of Nodejs?

Any comment/advice is welcome. Thanks.

Upvotes: 3

Views: 2520

Answers (3)

Ph0en1x
Ph0en1x

Reputation: 10067

Your test do not work because karma - is a testrunner for client-side JavaScript (javascript who run in browser), but you want to test node.js code with it (which run on the server part). So karma just can't run server-side tests. You need different testrunner, for example take a look to jasmine-node.

Upvotes: 8

Kyle Hale
Kyle Hale

Reputation: 8120

Since this comes up first in the Google search, I received a similar error but wasn't using any node.js-style code in my project. Turns out the error was one of my bower components had a full copy of jasmine in it including its node.js-style code, and I had

{ pattern: 'src/**/*.js', included: false },

in my karma.conf.js.

So unfortunately Karma doesn't provide the best debugging for this sort of thing, dumping you out without telling you which file caused the issue. I had to just tear that pattern down to individual directories to find the offender.

Anyway, just be wary of bower installs, they bring a lot of code down into your project directory that you might not really care to have.

Upvotes: 1

Ben
Ben

Reputation: 10146

I think you're missing the point of unit testing here, because it seems to me that you're copying application logic into your test suite. This voids the point of a unit test because what it is supposed to do is run your existing functions through a test suite, not to test that fs can load an XML file. In your scenario if your XML handling code was changed (and introduced a bug) in the source file it would still pass the unit test.

Think of unit testing as a way to run your function through lots of sample data to make sure it doesn't break. Set up your file reader to accept input and then simply in the Jasmine test:

describe('My XML reader', function() {
    beforeEach(function() {
        this.xmlreader = new XMLReader();
    });
    it('can load some xml', function() {
        var xmldump = this.xmlreader.loadXML('inputFile.xml');
        expect(xmldump).toBeTruthy();
    });
});

Test the methods that are exposed on the object you are testing. Don't make more work for yourself. :-)

Upvotes: 0

Related Questions