啊!为什么node fooey.js
在NodeJS命令行上运行时工作正常,但从Jest / Jasmine运行时失败?
Jest / Jasmine错误:
it foo() returns a string.
- TypeError: this.bar is not a function
如果我使用下面注释掉的self = this
和self.bar()
行,测试就会通过。
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');
});
});