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

8.2.0 does not work with sinon.useFakeTimers/setTimeout stubbing #4484

Closed
4 tasks done
hldswrth opened this issue Oct 22, 2020 · 3 comments
Closed
4 tasks done

8.2.0 does not work with sinon.useFakeTimers/setTimeout stubbing #4484

hldswrth opened this issue Oct 22, 2020 · 3 comments
Labels
status: waiting for author waiting on response from OP - more information needed

Comments

@hldswrth
Copy link

hldswrth commented Oct 22, 2020

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Mocha 8.2 introduces unexpected behaviour when using sinon.fakeTimers()

Steps to Reproduce

Tests which don't call done() are failing with a message saying that done() has been called multiple times. Other tests which are supposed to call done are ending before done() is called.

Here's an example:

const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const AsyncLock = require('async-lock')
const P = require('bluebird')
const asyncLock = new AsyncLock({ Promise: P })

describe('timeout-test', () => {
  describe('using async-lock', () => {
    it('says done is called multiple times', () => {
      let clock = sinon.useFakeTimers()
      sinon.stub(clock, 'setTimeout').callsFake((delayedFunction, delay, ...args) => {
        delayedFunction(...args)
      })

      const rejectInLock = function () {
        return asyncLock.acquire('LOCK', () => {
          return P.reject(new Error('999'))
        })
      }

      return expect(rejectInLock()).to.be.rejectedWith('999')
    })
  })
})

Expected behavior: [What you expect to happen]
The test should pass. for 8.1.3 and below it works, 8.2.0 it breaks.

Actual behavior: [What actually happens]
The test fails with this message

Error: done() called multiple times in test <timeout-test using async-lock says done is called multiple times> of file ....\donetest.js
    at tryCatcher (server\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (server\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (server\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (server\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (server\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (server\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (server\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (server\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (server\node_modules\bluebird\js\release\async.js:15:14)
Error: 999

Reproduces how often: [What percentage of the time does it reproduce?]
100%

Versions

8.2.0

  • The output of mocha --version and node node_modules/.bin/mocha --version:
    'mocha' is not recognized as an internal or external command, operable program or batch file.
    8.2.0
  • The output of node --version:
    v10.22.0
  • Your operating system
    • name and version: Windows 10
    • architecture (32 or 64-bit): 64 bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): cmd
  • Your browser and version (if running browser tests): N/A
  • Any third-party Mocha-related modules (and their versions):
    "async-lock": "^1.2.0",
    "bluebird": "^3.4.0",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "mocha": "^8.0.1",
    "sinon": "^9.0.0",
    "sinon-chai": "^3.2.0",
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): None

Additional Information

@boneskull
Copy link
Member

this may be fixed by #4489

@boneskull
Copy link
Member

please try v8.2.1

@boneskull boneskull added the status: waiting for author waiting on response from OP - more information needed label Nov 10, 2020
@hldswrth
Copy link
Author

I can confirm test passes with v8.2.1, so closing this issue, thanks.

I actually did like the uncaught error reports in v8.2.0, it forced us to check our code and tests very carefully for unhandled promise rejections and in a couple of cases did identify actual errors in our code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

No branches or pull requests

2 participants