Skip to content
Browse files

Merge pull request #10 from walling/master

Support running tests on server-side
  • Loading branch information...
2 parents 054f368 + fc7b75f commit ad8a519ee87c40a9d3b1cb34fe06c8157f5c592e @slevithan slevithan committed Mar 16, 2012
Showing with 42 additions and 8 deletions.
  1. +2 −1 .npmignore
  2. +8 −0 README.md
  3. +7 −1 package.json
  4. +25 −6 tests/tests.js
View
3 .npmignore
@@ -1,3 +1,4 @@
src/
-tests/
+tests/index.html
+tests/qunit/
tools/
View
8 README.md
@@ -156,6 +156,14 @@ XRegExp.matchRecursive(str, '<', '>', 'gy');
More details: [Addons: Match Recursive](http://xregexp.com/plugins/#matchRecursive).
+## How to run server-side tests
+
+~~~bash
+npm install -g qunit # needed to run the tests
+npm test # in the root directory
+~~~
+
+
## Changelog
* Historical changes: [Version history](http://xregexp.com/history/).
View
8 package.json
@@ -10,5 +10,11 @@
}],
"description": "Extended JavaScript regular expressions",
"keywords": ["regex", "regexp"],
- "main": "xregexp-all.js"
+ "main": "xregexp-all.js",
+ "scripts": {
+ "test": "qunit -c xregexp:./xregexp-all.js -t ./tests/tests.js"
+ },
+ "devDependencies": {
+ "qunit": "0.2.x"
+ }
}
View
31 tests/tests.js
@@ -1,3 +1,17 @@
+// In Node.JS module is defined as some object, which makes the QUnit module
+// definitions fail. So we redefine it to be a function that does nothing. This
+// does nothing in the browser, where QUnit already defined the module function.
+if (typeof module !== "function") {
+ var module = function(name) {};
+}
+
+// When using node-qunit on the command line, the module is imported as is and
+// we need to point at the XRegExp class inside the module. This does nothing
+// in the browser, where XRegExp is already loaded in the global scope.
+if (typeof XRegExp === "undefined" && typeof xregexp !== "undefined") {
+ var XRegExp = xregexp.XRegExp;
+}
+
//-------------------------------------------------------------------
module("API");
//-------------------------------------------------------------------
@@ -220,12 +234,17 @@ test("XRegExp.isRegExp", function () {
tamperedRegex.constructor = {};
ok(XRegExp.isRegExp(tamperedRegex), "RegExp with assigned Object constructor is RegExp");
- var iframe = document.createElement("iframe");
- iframe.width = iframe.height = iframe.border = 0; //iframe.style.display = "none";
- document.body.appendChild(iframe);
- frames[frames.length - 1].document.write("<script>var regex = /x/;<\/script>");
- ok(XRegExp.isRegExp(iframe.contentWindow.regex), "RegExp constructed in another frame is RegExp");
- iframe.parentNode.removeChild(iframe);
+ // Use feature detection: Check whether document exists and only run the last
+ // part of the test in this case. This is to ensure the test is run only in
+ // the browser and not in server-side environments without a DOM.
+ if (typeof document !== "undefined") {
+ var iframe = document.createElement("iframe");
+ iframe.width = iframe.height = iframe.border = 0; //iframe.style.display = "none";
+ document.body.appendChild(iframe);
+ frames[frames.length - 1].document.write("<script>var regex = /x/;<\/script>");
+ ok(XRegExp.isRegExp(iframe.contentWindow.regex), "RegExp constructed in another frame is RegExp");
+ iframe.parentNode.removeChild(iframe);
+ }
});
test("XRegExp.matchChain", function () {

0 comments on commit ad8a519

Please sign in to comment.
Something went wrong with that request. Please try again.