Ahh! Why does node fooey.js
work fine when run on the NodeJS command line, but fails when run from Jest/Jasmine?
Jest/Jasmine ERROR:
it foo() returns a string.
- TypeError: this.bar is not a function
If I use the commented out lines below for self = this
and self.bar()
the tests pass OK.
fooey.js
var Fooey = function() {
// var self = this;
this.foo = function(phrase) {
var result = this.bar(phrase);
// var result = self.bar(phrase);
return result;
};
this.bar = function(greeting) {
var result = greeting + " Watz up?"
console.log(result);
return result;
};
};
module.exports = Fooey;
var fooey = new Fooey();
fooey.foo("Hi Bob!");
fooey.bar("Yo Carl!");
fooey-test.js
jest.unmock('../fooey'); // unmock to use the actual implementation.
describe('foo()', () => {
const Fooey = require('../fooey');
const fooey = new Fooey();
const foo = fooey.foo;
const bar = fooey.bar;
it('foo() returns a string.', () => {
var result = foo("Hi Bob!");
expect(typeof result).toBe('string');
});
});