Sinon: Date object is undefined

Created on 30 Aug 2016  Â·  12Comments  Â·  Source: sinonjs/sinon

Hi there!

What did you expect to happen?

Date object should be defined

What actually happens

Date object is undefined

How to reproduce

One of the files that fails is this one https://github.com/franleplant/reform/blob/master/src/validators/date.js

You can clone the repo and run npm test or just check the travis output.

I first reported the bug to Jest https://github.com/facebook/jest/issues/1502 but after some couple debugging the Jest crew found that the problem didn't happened if we didn't import sinon.

I can confirm that the problem goes away by not importing sinon. I haven't pushed those changes because other parts of the tests fail without sinon (Im moking some functions), but the Date error goes away and I can actually see it's defined.

The stack trace can be seen here (travis): https://travis-ci.org/franleplant/reform

Any help is very much appreciated thanks!

1.x

All 12 comments

What environment is this? I just vaguely know Jest, but from a quick glance it seems that it runs the tests in jsdom in Node, which is not among the listed supported platforms for Sinon. Does the error happen when running in Chrome, Firefox or Edge? Assuming Jest supports running tests in the browser (a la Karma), which might not be the case.

Hi! Jest runs tests in Jsdom by default but this one is set to run in _Node_. I've inspected the global object and it has the Date property specifically set to undefined. Do you know any ways sinon might be doing that?
Thanks!

Not from the top of my head... A large test case with multiple dependencies
makes it even harder to untangle :-(

Hi! I've stripped down this to the maximum in this branch https://github.com/franleplant/reform/tree/sinon-test

I've removed _all_ previous tests and added this new test https://github.com/franleplant/reform/blob/sinon-test/src/__tests__/test1.js

With sinon the tests fails because Date is undefined
Without sinon it works.

Please note that there's no dependencies in the new test. Only Jest (as test runner) and Sinon

I hope this helps
Thanks!

Hi! Any news? Im kind of blocked by this.
Thanks a lot!

@franleplant: generally you can't be sure (or expect) anyone on the core maintainer team to be working on the issues that are not verified to be an issue with sinon. We rely on the greater community to contribute in solving these issues and tracking down whether the fault is in sinon or something else.

That being said, I will be on the train for a few hours, so I'll be looking into this. No guarantees though - net stability is shaky at best :)

Ok, no troubles, I've figured this would be a hard thing to debug.

I will be switching to an ad hoc solution to spies, since is the only Sinon feature I'm using. I'll leave the failing branch if it's useful somehow to you for debugging.

Sorry I cannot invest time in fixing this myself but I'm working on this other open source project and I know mostly nothing of Sinon internals (and culture) so I don't have the resources to contribute at the moment.

Thanks a lot for your help!
Fran

Figured it out. It took a while since there were so many layers to dig through, that I forgot about the main thing: Sinon 1.x does not work well with module bundlers. Sinon 2 does.

Fix for you project:

rm -r node_modules/sinon
npm i sinon@next --save-dev

Results:

$ npm-exec jest
Using Jest CLI v14.1.0, jasmine2
Running 1 test suite...
Im the Date object
function
[Function: Date]
 PASS  src/__tests__/test1.js (0.065s)
  Date
    ✓ it should be defined! (6ms)

1 test passed (1 total in 1 test suite, run time 0.443s)

Thanks a lot for your help. I will link this issue from the original Jest issue just for future reference.

@fatso83 Still running into this issue.
Using Node v6.3.1, Jest v15.1.1, sinon v2.0.0-pre.3, enzyme ^2.4.1.

Getting
TypeError: Cannot read property 'now' of undefined whenever I try to import sinon from 'sinon'.

@giedrius-timinskis then you are running into a different issue. Please file a new issue and include sufficient info on how to reproduce this. Best: a public test repo with just enough code to trigger the error.

@giedrius-timinskis: your test case is incomplete and not reproducible. Node 6 does not support the syntax import sinon from 'sinon', so you are doing something else. Please file an issue with enough information to reproduce it, preferably with a link to a minimal repo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ljian3377 picture ljian3377  Â·  3Comments

akdor1154 picture akdor1154  Â·  4Comments

fearphage picture fearphage  Â·  3Comments

sudhirbits picture sudhirbits  Â·  4Comments

optimatex picture optimatex  Â·  4Comments