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

Don't mock non-enum accessors #390

Merged
merged 1 commit into from
Jun 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/lib/__tests__/moduleMocker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ describe('moduleMocker', function() {
expect(bar.x()).toBe('Bar');
});

it('does not mock non-enumerable getters', function() {
var foo = Object.defineProperties({}, {
nonEnumMethod: {
value: function() {}
},
nonEnumGetter: {
get: function() { throw 1; }
}
});
var fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo)
);

expect(typeof foo.nonEnumMethod).toBe('function');

expect(fooMock.nonEnumMethod.mock).not.toBeUndefined();
expect(fooMock.nonEnumGetter).toBeUndefined();
});

it('mocks ES6 non-enumerable methods', function() {
// ES6: class ClassFoo { foo() { } }
// Converted with https://babeljs.io/repl/
Expand Down
5 changes: 4 additions & 1 deletion src/lib/moduleMocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ function getSlots(object) {
// Simply attempting to access any of these throws an error.
var forbiddenProps = [ 'caller', 'callee', 'arguments' ];
var collectProp = function(prop) {
if (forbiddenProps.indexOf(prop) === -1) {
var propDesc = Object.getOwnPropertyDescriptor(object, prop);
if (forbiddenProps.indexOf(prop) === -1 &&
// Include only enumerable accessors.
(propDesc.enumerable || !propDesc.get)) {
slots[prop] = true;
}
};
Expand Down