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

Sinon.assert does not work with new dates form Sinon timers #2630

Open
krystianolech opened this issue Dec 2, 2024 · 1 comment
Open

Sinon.assert does not work with new dates form Sinon timers #2630

krystianolech opened this issue Dec 2, 2024 · 1 comment

Comments

@krystianolech
Copy link

Hello, I noticed that on Sinon 19 some of my test stoped working.
Case where it happen is related to situations where fakeTimers are used with something that is passed to function and later asserted by Sinon.match.

Here is minimal code to reproduce it:

import * as Sinon from "sinon";

const now = new Date("2024-01-01");
const clock = Sinon.useFakeTimers({
  now,
  toFake: ["Date"],
});

const stub = Sinon.stub();

stub(new Date());

Sinon.assert.calledWith(stub, now);

This code works fine on v18 but it fails on v19.

@fatso83
Copy link
Contributor

fatso83 commented Dec 2, 2024

This is interesting. The code fails when running with the latest Sinon from NPM, but it does not fail with the latest source code in the repo 🤔

✦ ❯ cat my-test.mjs
import * as Sinon from "sinon";
import { test } from "mocha";

test("test date", function () {
  const now = new Date("2024-01-01");
  const clock = Sinon.useFakeTimers({
    now,
    toFake: ["Date"],
  });

  const stub = Sinon.stub();

  stub(new Date());

  Sinon.assert.calledWith(stub, now);
});
✦ ❯ npx mocha my-test.mjs


  1) test date

  0 passing (7ms)
  1 failing

  1) test date:
     AssertError: expected stub to be called with arguments
Mon Jan 01 2024 01:00:00 GMT+0100 (GMT+01:00)
      at Object.fail (file:///private/tmp/node_modules/sinon/pkg/sinon-esm.js:131:27)
      at failAssertion (file:///private/tmp/node_modules/sinon/pkg/sinon-esm.js:274:20)
      at assert.<computed> [as calledWith] (file:///private/tmp/node_modules/sinon/pkg/sinon-esm.js:303:17)
      at Context.<anonymous> (file:///private/tmp/my-test.mjs:15:16)
      at process.processImmediate (node:internal/timers:478:21)

Now using a reference to the build in locally

# First did this
cd ~/dev/sinon
npm run build

Changed the top line to read

import * as Sinon from "./pkg/sinon-esm.js";

then ran it:

npx mocha my-test.mjs


  ✔ test date

  1 passing (5ms)

Not expected.

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

No branches or pull requests

2 participants