@usebruno/js 0.45.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usebruno/js",
3
- "version": "0.45.0",
3
+ "version": "0.45.1",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -13,7 +13,12 @@ chai.use(function (chai, utils) {
13
13
  // Custom assertion for checking if a variable is JSON
14
14
  chai.Assertion.addProperty('json', function () {
15
15
  const obj = this._obj;
16
- const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) && obj.constructor === Object;
16
+ // Use Object.prototype.toString instead of constructor check for cross-realm compatibility.
17
+ // Objects created inside Node's vm.createContext() have a different Object constructor,
18
+ // so obj.constructor === Object fails for objects passed via res.setBody() from scripts.
19
+ // Note: toString check is more permissive than constructor check — custom class instances
20
+ const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj)
21
+ && Object.prototype.toString.call(obj) === '[object Object]';
17
22
 
18
23
  this.assert(isJson, `expected ${utils.inspect(obj)} to be JSON`, `expected ${utils.inspect(obj)} not to be JSON`);
19
24
  });
@@ -58,6 +58,27 @@ const addBruShimToContext = (vm, __brunoTestResults) => {
58
58
  globalThis.test = Test(__brunoTestResults);
59
59
  `
60
60
  );
61
+
62
+ // Register custom chai assertion for isJson (expect(...).to.be.json)
63
+ // The bundled chai only exposes { expect, assert } — no Assertion class.
64
+ // Access the prototype through an expect() instance instead.
65
+ vm.evalCode(
66
+ `
67
+ (function() {
68
+ var proto = Object.getPrototypeOf(expect(null));
69
+ Object.defineProperty(proto, 'json', {
70
+ get: function () {
71
+ var obj = this._obj;
72
+ var isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) &&
73
+ Object.prototype.toString.call(obj) === '[object Object]';
74
+ this.assert(isJson, 'expected #{this} to be JSON', 'expected #{this} not to be JSON');
75
+ return this;
76
+ },
77
+ configurable: true
78
+ });
79
+ })();
80
+ `
81
+ );
61
82
  };
62
83
 
63
84
  module.exports = addBruShimToContext;