Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Detect CommonJS usage by exports variable only #318

Closed
wants to merge 1 commit into from

3 participants

@chrisberkhout

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

@chrisberkhout chrisberkhout Detect CommonJS usage by exports variable only (because PhantomJS use…
…s a CommonJS interface, but also has a window variable defined).
d5f60dc
@infews
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.

@chrisberkhout

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?

@chrisberkhout

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

@chrisberkhout

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

@akre54

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).

@infews
Owner

@akre54 are you using 2.0rc5?

@akre54

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

@akre54

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

@infews
Owner

Per above. Closing. Thanks @akre54.

@infews 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. @chrisberkhout

    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
View
2  lib/jasmine-core/jasmine.js
@@ -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.
View
2  src/core/base.js
@@ -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.