Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Detect CommonJS usage by exports variable only #318

Closed
wants to merge 1 commit into from

3 participants

Chris Berkhout Davis W. Frank Adam Krebs
Chris Berkhout

Because PhantomJS uses a CommonJS interface, but also has a window variable defined.

Chris Berkhout chrisberkhout Detect CommonJS usage by exports variable only (because PhantomJS use…
…s a CommonJS interface, but also has a window variable defined).
d5f60dc
Davis W. Frank
Owner

Is there some docs somewhere about CommonJS for us to follow to detect this? This code seems to change every so often in pull requests and I'd like to solve this problem once and for all.

Chris Berkhout

Hi Davis,

This is quite brief but seems to be the authoritative spec:
http://www.commonjs.org/specs/modules/1.0/

The "Module Context" section mentions require and exports, but not window. The linked executable specs also don't mention window.

Adding a check on require (that it's a function) might be a more complete check of the spec-defined conditions for CommonJS modules, but I'd prefer to just check for exports, because that's all Jasmine needs when exporting itself.

If someone has browser-based code that defines a global exports that can't be used to export a module, I don't have a problem with that breaking.

What do you think?

Chris Berkhout

Later version of the same spec (same conclusion):
http://wiki.commonjs.org/wiki/Modules/1.1.1

Chris Berkhout

This slide of a presentation linked to by the spec suggests that a CommonJS-module environment may provide a window variable:
https://docs.google.com/presentation/d/1KlPxgocvzpTq44KTF4xZ6TnzGRsy628hP5he7bXVB3I/edit#slide=id.i73

Adam Krebs

Just ran into this myself. Can we get some dev love on this? The only requirement of commonjs is a free exports object (or optionally a module with an exports property).

Davis W. Frank
Owner

@akre54 are you using 2.0rc5?

Adam Krebs

@infews I'm using 1.3.1 via grunt-contrib-jasmine. Let me check with latest.

Adam Krebs

Just upgraded. Perfect, thanks. Maybe close this issue so others like me don't think it's still outstanding?

Davis W. Frank
Owner

Per above. Closing. Thanks @akre54.

Davis W. Frank infews closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2013
  1. Chris Berkhout

    Detect CommonJS usage by exports variable only (because PhantomJS use…

    chrisberkhout authored
    …s a CommonJS interface, but also has a window variable defined).
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +1 −1  lib/jasmine-core/jasmine.js
  2. +1 −1  src/core/base.js
2  lib/jasmine-core/jasmine.js
View
@@ -1,4 +1,4 @@
-var isCommonJS = typeof window == "undefined" && typeof exports == "object";
+var isCommonJS = typeof exports == "object";
/**
* Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework.
2  src/core/base.js
View
@@ -1,4 +1,4 @@
-var isCommonJS = typeof window == "undefined" && typeof exports == "object";
+var isCommonJS = typeof exports == "object";
/**
* Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework.
Something went wrong with that request. Please try again.