Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods on ES6 class instances don't get mocked #332

Closed
AsaAyers opened this issue Apr 17, 2015 · 2 comments · Fixed by #333
Closed

Methods on ES6 class instances don't get mocked #332

AsaAyers opened this issue Apr 17, 2015 · 2 comments · Fixed by #333

Comments

@AsaAyers
Copy link
Contributor

ES6 class methods are not enumerable, so they aren't being mocked by Jest.

I'm using Babel to do the transforms.

// es6_class.js
class Foo {
    foo() { return 'foo'; }
    bar() { return 'bar'; }
}
module.exports = new Foo();
// I can assign a prop to make it interable, then it gets mocked
module.exports.bar = module.exports.bar;
// __tests__/es6_class.js
describe('es6 class instance', function() {
    let instance;
    beforeEach(function() {
        instance = require('../es6_class.js');
    });
    it('has foo', function() {
        expect(instance.foo).toBeTruthy();
    });
    it('has bar', function() {
        // This one works only because I reassigned it.
        expect(instance.bar).toBeTruthy();
    });
});

I also tried exporting the class and that fails even more spectacularly. I can't even create an instance of that mock.

@AsaAyers
Copy link
Contributor Author

I thought maybe I could use Object.getOwnPropertyNames and Object.getOwnPropertyDescriptor to scan for the missing methods, but I couldn't get it to distinguish between foo above and built in methods.

For testing, if instead of checking the descriptor I just check that the prop === 'foo' then it fixes the code above. So I think if the filtering can be fixed it looks like a reasonable strategy.

var scan = function(obj) {
    var fakeObj = {};
    var prop;
    for (prop in obj) {
        fakeObj[prop] = true;
    }
    if (obj) {
        var props = [];
        var tmp = obj;
        var descriptor;
        do {
            props = Object.getOwnPropertyNames(tmp);

            for (var i = 0; i < props.length; i++) {
                prop = props[i];
                descriptor = Object.getOwnPropertyDescriptor(tmp, prop);
                // Try to find ES6 methods
                if (descriptor && !descriptor.enumerable && typeof descriptor.value === 'function') {
                    fakeObj[prop] = true;
                    // console.log(prop)
                    // This picks up many methods that can't be included:
                    // constructor
                    // toString
                    // toLocaleString
                    // valueOf
                    // hasOwnProperty
                    // isPrototypeOf
                    // propertyIsEnumerable
                    // __defineGetter__
                    // __lookupGetter__
                    // __defineSetter__
                    // __lookupSetter__
                    // Symbol(tie)_r.fdrvaoikracbx1or
                }
            }
        } while (tmp = Object.getPrototypeOf(tmp));
    }
    return fakeObj;
};
// from my local lib/moduleMocker.js
          for (var slot in scan(prototype)) { // line 109
    for (var slot in scan(metadata.members)) { // line 233
      for (var slot in scan(component)) { // line 295
      for (var slot in scan(metadata.members)) { // line 331

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant