chaijs / chai

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
https://chaijs.github.io
MIT License
8.11k stars 694 forks source link

assert.throws() does not work correctly in node/vm spawned context #1604

Closed myocytebd closed 3 months ago

myocytebd commented 6 months ago

(chai 5.1.0 from npm, node 18.14.0, v8 10.2.154.23-node.22) It seems that assert.throws() does not work correctly in vm spawned context.

const vm = require('node:vm');

const context = {};
vm.createContext(context);
setImmediate(runTest);

async function runTest() {
    context.chai = await import('chai');
    context.chai.assert.throws(() => { throw new Error('aaa.bbb'); }, /bbb/); // OK
    vm.runInContext(code, context); // Failure
}

const code = `
chai.assert.throws(() => { throw new Error('aaa.bbb'); }, /bbb/);
`;

Default context assert.throws() work as expected, but vm context assert.throws() asserts where it should pass.

43081j commented 6 months ago

what are you expecting to happen?

the code does throw, so i'd expect no errors to occur and that's what im seeing when i try this locally

if you can give a little more info, might be able to help

myocytebd commented 6 months ago

what are you expecting to happen?

the code does throw, so i'd expect no errors to occur and that's what im seeing when i try this locally

if you can give a little more info, might be able to help

I got assertion failures with node 18/20/21. (on code)

.../script.common/node_modules/chai/chai.js:1419
    throw new AssertionError(
    ^

AssertionError: expected [Function] to throw /bbb/ but Error: aaa.bbb was thrown
    at evalmachine.<anonymous>:2:13
    at Script.runInContext (node:vm:133:12)
    at Object.runInContext (node:vm:287:6)
    at Immediate.runTest (.../script.common/bbb.js:10:8) {
  actual: Error: aaa.bbb
      at evalmachine.<anonymous>:2:34
      at Proxy.assertThrows (.../script.common/node_modules/chai/chai.js:2668:5)
      at Proxy.methodWrapper (.../script.common/node_modules/chai/chai.js:1582:25)
      at assert.throws (.../script.common/node_modules/chai/chai.js:3451:71)
      at evalmachine.<anonymous>:2:13
      at Script.runInContext (node:vm:133:12)
      at Object.runInContext (node:vm:287:6)
      at Immediate.runTest (.../script.common/bbb.js:10:8),
  expected: /bbb/,
  showDiff: true,
  operator: 'strictEqual'
}

Here it doesn't look correct: node_modules/chai/chai.js:2668:5 if (errorLike instanceof RegExp || typeof errorLike === "string") { Built-in globals are per-JSContext and cannot be tested by instanceof.

43081j commented 6 months ago

ah i get you! i was missing the error matcher in my local test

you are correct. we should instead do Object.prototype.toString.call(errorLike) === '[object RegExp]'

koddsson commented 3 months ago

@43081j is this fixed now after https://github.com/chaijs/chai/pull/1609?

43081j commented 3 months ago

yup we can close this! 🥳

fixed by #1609