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

Add an error when creating a clock with no Date object #205

Merged
merged 1 commit into from Sep 9, 2018

Conversation

LouisBrunner
Copy link
Contributor

Purpose

Improve sinon#1852 issue by adding an explicit error message.

Background

When using webpack and jsdom together, the global context doesn't have a Date object and lolex will fail with a really confusing error: TypeError: Cannot read property 'now' of undefined.

This problem is easily triggered when using sinon.useFakeTimers (which relies on lolex) in this setup.

Solution

This PR adds an explicit exception when no Date are present in the global context.

The message contains a link to @mroderick's comment that explains how to get around the problem. (Maybe this would be better as a wiki page?)

The check is placed in createClock (which means it's triggered by install as well) instead of right when NativeDate is initialised so it doesn't crash right away (as withGlobal is called when the module is initialised).

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree this should be put in a wiki, but we currently don't have one. Our documentation efforts are in need of a different direction, so feel free to join the discussion with inputs on how to best achieve this from the viewpoint of a contributor:
sinonjs/sinon#1651

@fatso83 fatso83 merged commit 1278eaa into sinonjs:master Sep 9, 2018
@SimenB
Copy link
Member

SimenB commented Sep 9, 2018

@LouisBrunner why do you have windowOptions in your reproduction case? That's not part of JSDOM's API, is it?

const {JSDOM} = require('jsdom');

let dom = new JSDOM('', {windowOptions: {runScripts: 'outside-only'}});

console.log(dom.window.Date); // undefined

dom = new JSDOM('', {runScripts: 'outside-only'});

console.log(dom.window.Date); // [Function: Date]

@LouisBrunner LouisBrunner deleted the throws-no-date branch September 9, 2018 12:30
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

Successfully merging this pull request may close these issues.

None yet

3 participants