How to connect a function declaration between Node and Jasmine files?

39 Views Asked by At

In my pigLatin Jasmine file I am trying to get the following code to work:

var pigLatin = require("./pigLatin.js");

describe('#translate', function() {
    it('translates a word beginning with a vowel', function() {
    s = translate("apple");
    expect(pigLatin.s).toEqual('appleay');
    });
});

Here is my Node file:

function translate(argument) {
    return "appleay";
}


module.exports = {
    translate
}

I think this has something to do with the spy feature but I am having a bit of trouble wrapping my head around what it does exactly. Thank you for any help in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks to Brad I was able to figure this out. Here is the fixed solution:

var pigLatin = require("./pigLatin.js");

describe('#translate', function() {
it('translates a word beginning with a vowel', function() {
    s = pigLatin.translate("apple");
    expect(s).toEqual('appleay');
});
2
On

Your pigLatin.js file only exports the function translate, so when you import the file, you are storing the function into the variable pigLatin.

So for your describe you'd want something more like...

var translate = require("./pigLatin.js");

describe('#translate', function() {
    it('translates a word beginning with a vowel', function() {
    s = translate("apple"); // we imported the translate function
    expect(s).toEqual('appleay'); // `s` is the result of the translation
    });
});

Whatever is exported by a module is what is returned by the require function.

Hope this helps!