this.func()在Jest / Jasmine测试中失败

时间:2016-05-10 01:46:57

标签: javascript jasmine jestjs

啊!为什么node fooey.js在NodeJS命令行上运行时工作正常,但从Jest / Jasmine运行时失败?

Jest / Jasmine错误: it foo() returns a string. - TypeError: this.bar is not a function

如果我使用下面注释掉的self = thisself.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');
   });
});

0 个答案:

没有答案