-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix proxies for property assertions (#205)
This PR fixes Chai 4's proxy protection for property assertions. Without this fix, proxy protection works for method assertions and chainable method assertions, but not property assertions. The reason it doesn't work for property assertions is because the getters for property assertions incidentally de-proxify the assertion object before returning it. This PR re-proxifies those de-proxified assertion objects from property assertions before returning them.
- Loading branch information
Showing
2 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use strict"; | ||
const chai = require("chai"); | ||
const chaiAsPromised = require(".."); | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
function shouldGuard(fn, msg) { | ||
fn.should.throw("Invalid Chai property: " + msg); | ||
} | ||
|
||
describe("Proxy guard", () => { | ||
const number = 42; | ||
const promise = Promise.resolve(42); | ||
|
||
before(function () { | ||
if (typeof Proxy === "undefined" || typeof Reflect === "undefined" || chai.util.proxify === undefined) { | ||
/* eslint-disable no-invalid-this */ | ||
this.skip(); | ||
/* eslint-enable no-invalid-this */ | ||
} | ||
}); | ||
|
||
it("should guard against invalid property following `.should`", () => { | ||
shouldGuard(() => number.should.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following overwritten language chain", () => { | ||
shouldGuard(() => number.should.to.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following overwritten property assertion", () => { | ||
shouldGuard(() => number.should.ok.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following uncalled overwritten method assertion", () => { | ||
shouldGuard(() => number.should.equal.pizza, "equal.pizza. See docs"); | ||
}); | ||
|
||
it("should guard against invalid property following called overwritten method assertion", () => { | ||
shouldGuard(() => number.should.equal(number).pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following uncalled overwritten chainable method assertion", () => { | ||
shouldGuard(() => number.should.a.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following called overwritten chainable method assertion", () => { | ||
shouldGuard(() => number.should.a("number").pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following `.eventually`", () => { | ||
shouldGuard(() => promise.should.eventually.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following `.fulfilled`", () => { | ||
shouldGuard(() => promise.should.fulfilled.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following `.rejected`", () => { | ||
shouldGuard(() => promise.should.rejected.pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following called `.rejectedWith`", () => { | ||
shouldGuard(() => promise.should.rejectedWith(42).pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following uncalled `.rejectedWith`", () => { | ||
shouldGuard(() => promise.should.rejectedWith.pizza, "rejectedWith.pizza. See docs"); | ||
}); | ||
|
||
it("should guard against invalid property following called `.become`", () => { | ||
shouldGuard(() => promise.should.become(42).pizza, "pizza"); | ||
}); | ||
|
||
it("should guard against invalid property following uncalled `.become`", () => { | ||
shouldGuard(() => promise.should.become.pizza, "become.pizza. See docs"); | ||
}); | ||
}); |