Skip to content

Commit c783928

Browse files
committed
Added nodejs support (finally) along with node-qunit test/runner.js
1 parent db5f5b7 commit c783928

10 files changed

Lines changed: 61 additions & 11 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ Alternatively:
2222
* Minified version provided in state-machine.min.js
2323
* No 3rd party library is required
2424
* Demo can be found in /index.html
25-
* QUnit tests can be found in /test/index.html
25+
* QUnit (browser) tests can be found in /test/index.html
26+
* QUnit (headless) tests can be run with "node test/runner.js" (after installing node-qunit with "npm install")
2627

2728
Usage
2829
=====
2930

30-
Include `state-machine.min.js` in your application.
31+
Include `state-machine.js` in your web application, or, for nodejs `require("state-machine.js")`.
3132

3233
In its simplest form, create a standalone state machine using:
3334

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "state-machine",
3+
"description": "A simple finite state machine library",
4+
"homepage": "https://github.com/jakesgordon/javascript-state-machine",
5+
"keywords": ["state machine", "server", "client"],
6+
"author": "Jake Gordon <jake@codeincomplete.com>",
7+
"repository": {"type": "git", "url": "git://github.com/jakesgordon/javascript-state-machine.git"},
8+
"main": "state-machine.js",
9+
"devDependencies": [ "qunit" ],
10+
"version": "2.3.0"
11+
}

state-machine.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
*/
99

10-
(function (window) {
10+
(function () {
1111

1212
var StateMachine = {
1313

@@ -191,12 +191,27 @@
191191

192192
//===========================================================================
193193

194-
if ("function" === typeof define) {
194+
//======
195+
// NODE
196+
//======
197+
if (typeof exports !== 'undefined') {
198+
if (typeof module !== 'undefined' && module.exports) {
199+
exports = module.exports = StateMachine;
200+
}
201+
exports.StateMachine = StateMachine;
202+
}
203+
//============
204+
// AMD/REQUIRE
205+
//============
206+
else if (typeof define === 'function') {
195207
define(function(require) { return StateMachine; });
196208
}
197-
else {
209+
//========
210+
// BROWSER
211+
//========
212+
else if (window) {
198213
window.StateMachine = StateMachine;
199214
}
200215

201-
}(this));
216+
}());
202217

test/runner.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// To run tests via nodejs you must have nodejs and npm installed
3+
//
4+
// > npm install # to install node-qunit
5+
// > node test/runner
6+
//
7+
8+
var runner = require("qunit");
9+
10+
runner.run({
11+
12+
code: "./state-machine.js",
13+
14+
tests: [
15+
"test/test_basics.js",
16+
"test/test_advanced.js",
17+
"test/test_classes.js",
18+
"test/test_async.js",
19+
"test/test_initialize.js"
20+
]
21+
22+
});

test/test_advanced.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-----------------------------------------------------------------------------
22

3-
module("advanced");
3+
QUnit.module("advanced");
44

55
//-----------------------------------------------------------------------------
66

test/test_async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-----------------------------------------------------------------------------
22

3-
module("async");
3+
QUnit.module("async");
44

55
//-----------------------------------------------------------------------------
66

test/test_basics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-----------------------------------------------------------------------------
22

3-
module("basic");
3+
QUnit.module("basic");
44

55
//-----------------------------------------------------------------------------
66

test/test_classes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-----------------------------------------------------------------------------
22

3-
module("classes");
3+
QUnit.module("classes");
44

55
//-----------------------------------------------------------------------------
66

test/test_initialize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-----------------------------------------------------------------------------
22

3-
module("special initialization options", {
3+
QUnit.module("special initialization options", {
44

55
setup: function() {
66
this.called = [];

0 commit comments

Comments
 (0)