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

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

Open
myocytebd opened this issue Feb 17, 2024 · 3 comments
Open

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

myocytebd opened this issue Feb 17, 2024 · 3 comments

Comments

@myocytebd
Copy link

(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
Copy link
Contributor

43081j commented Feb 19, 2024

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
Copy link
Author

myocytebd commented Feb 21, 2024

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
Copy link
Contributor

43081j commented Feb 21, 2024

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]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants