question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mangled exception handling with `chai-as-promised` leads to mocha silently exiting without reporting an error

See original GitHub issue

I don’t know if this is supported or not, but it causes a very vicious silent error which causes the rest of the unit tests to not run and mocha to immediately exit with 0, reporting a global success - which means that automated unit testing doesn’t report any error

It is somewhat random, depending on the Node version - I have a repo that can reproduce it with 100% probability on Node 14 on Ubuntu 20.04

Normally ds.root.arrays.getAsync('time') returns a Promise that resolves with a gdal.MDArray It is a promisified function of a function that calls a callback In this particular case getAsync() is throwing synchronously from C++ and then proceeding to invoke its callback which causes the exception to be propagated in a very unusual way going higher up the chain.

it('should have "getAsync()" method', (done) => {
  assert.eventually.instanceOf(ds.root.arrays.getAsync('time'), gdal.MDArray).then(done)
})

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mmomtchevcommented, Nov 1, 2021
0reactions
mmomtchevcommented, Oct 20, 2021

@sukrosono I was able to reproduce the problem without any C++ shenanigans

npm i mocha chai chai-as-promised

mocha.test.js

const chai = require('chai')
const assert = chai.assert
const chaiAsPromised = require('chai-as-promised')
chai.use(chaiAsPromised)

function asyncIterator() {
  let i = 0
  const count = 10

  return {
    next: () => Promise.resolve(count)
      .then((count) => {
        if (i++ < count) {
          return Promise.resolve(i).then((value) => ({ done: false, value }))
        }
        return { done: true, value: null }
      })
  }
}

it('asyncIterator', (done) => {
  const w = []
  const it = asyncIterator()
  const recurse = (r) => {
    if (r.done) {
      assert(w.length == 0) // This will throw
      done()
    } else {
      w.push(r.value)
      it.next().then(recurse)
    }
  }
  it.next().then(recurse)
})

.mocharc.json

{
  "timeout": 0
}

If you run this test, mocha will silently exit with a 0 exit code, indicating success, upon hitting the exception. If the file contain other tests, they will be silently ignored.

Read more comments on GitHub >

github_iconTop Results From Across the Web

silent errors in mocha tests with generator functions
In my unit test I make a false assertion but mocha doesn't report it, it just times out: //function to test function start(data, ......
Read more >
Beware silently skipped tests in Mocha - Adam Coster
It turns out that errors thrown in async contexts can cause Mocha to exit early without registering all tests, all while swallowing the ......
Read more >
chaijs - Bountysource
Typescript test says error TS2339: Property 'by' does not exist on type 'Assertion'. ... Mangled exception handling with `chai-as-promised` leads to mocha ......
Read more >
Changelog - Cypress Documentation
Fixes issue where cookies were not handled within cy.origin for requests other than ... Fixed an issue where Cypress would throw an error...
Read more >
expect(await fn()) vs await expect(fn()) for error tests with chai ...
The problem scenario. Ah, writing tests for errors. It's fun, isn't it? Have you ever written a unit test expecting to catch an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found