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

Should style API of chai.js doesn't always work on test doubles since 1.6.2 #134

Closed
njdancer opened this issue Sep 18, 2016 · 3 comments
Closed

Comments

@njdancer
Copy link

1.6.2 added a patch that modified td.object the traverse the prototypal chain of a constructor and add all methods on it's prototype. This has a side effect for getters defined on the Object type.

Chai.js for example can(if configured) define a getter on Object for the should property to enable a should style assertion API. When accessed chai creates an assertion object using this as the value to conduct tests on. The current behaviour of 1.6.2 I believe is causing an assertion to be created for the prototype object itself. This is then statically assigned to the outputted object.

The following should demonstrate this.

let td = require("testdouble");
let chai = require("chai");
chai.should();

function Vehicle() {}
Vehicle.prototype.drive = function drive() {
};

function Truck() {}
Truck.prototype = Object.create(Vehicle.prototype);
Truck.prototype.loadCargo = function loadCargo() {
};

let fakeTruck = td.object(Truck);
fakeTruck.should.equal(fakeTruck);
// AssertionError: expected { Object (loadCargo) } to equal { Object (loadCargo, constructor, ...) }

This runs on 1.6.1 and throws an error > 1.6.2.

Ultimately I believe the desired behaviour in this instance should be to ignore this particular property when creating a test double object so that the assertion will continue to be generated at the point the property is accessed.

I can appreciate however this is not always the case and am not exactly sure where to draw the line. Something like ignoring all properties on Object seems a little too blunt. Maybe specifying properties to ignore globally.

@searls
Copy link
Member

searls commented Sep 18, 2016

Hey thanks a lot for reporting this and explaining it as clearly as you did. That said, I'm still (and always have been) a bit dense about how Chai actually works and would like some more help in understanding what a minimally impactful change would look like to resolve this issue

@njdancer
Copy link
Author

That's totally fine. I'm happy to have a stab at patching this but don't want to introduce uneccessary side effects that are outside my use case.

There is a way to correct the assertion after being copied from getter but this means testdouble needs knowledge of how chai works which seems the wrong way to approach it. I'm thinking a setting to ignore certain properties might be the best way to go. This keeps the solution generic so it will solve all issues of this type and allows the consumer of testdouble to configure the exact behaviour they need.

I noticed you have something similar already.
https://github.com/testdouble/testdouble.js/blob/master/src/object.coffee#L15
I believe you're currently using it to exclude certain methods from being handled by a proxy object. Does this need to be limited to proxies? If so we could expose it publicly to allow it to be configured and use that. This would allow should to be ignored by td.object() allowing it to be handled by the getter defined on Object.prototype.

@searls
Copy link
Member

searls commented Apr 30, 2018

Closing due to inactivity. If this is still an issue, let's talk about its behavior in the current version of the lib

@searls searls closed this as completed Apr 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Bugs & Requests
Need to Triage
Development

No branches or pull requests

2 participants