Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Address the issue of setup/teardown functions.

  • Loading branch information...
commit f52589c8b6844fabaf74cb724ee373621ef1d89b 1 parent 64ac5a6
authored August 09, 2010
26  README.md
Source Rendered
@@ -117,7 +117,31 @@ can be run with the following command:
117 117
 
118 118
     node examples/readme.js
119 119
 
120  
-Additionally, the you can look at the files in the `test` directory for more
  120
+Because all tests are just functions, writing setup or teardown functions is as
  121
+simple as writing a wrapper function which takes a test and returns a new test:
  122
+
  123
+    function setup(testFunc) {
  124
+      return function newTestFunc(test) {
  125
+        var extra1 = 1;
  126
+        var extra2 = 2;
  127
+        testFunc(test, extra1, extra2);
  128
+      }
  129
+    }
  130
+
  131
+    suite['wrapped test'] = setup(function(test, one, two) {
  132
+      test.equal(1, one);
  133
+      test.equal(2, two);
  134
+      test.finish();
  135
+    });
  136
+
  137
+**node-async-testing** comes with a convenience function for wrapping all tests
  138
+in a suite:
  139
+
  140
+    require('async_testing').wrapTests(suite, setup);
  141
+
  142
+See `test/test-wrap_tests` for more details.
  143
+
  144
+Additionally, the you can look at any of the files in the `test` directory for
121 145
 examples.
122 146
 
123 147
 Running test suites
19  examples/readme.js
... ...
@@ -1,4 +1,5 @@
1 1
 // This file contains all the examples mentioned in the readme
  2
+var async_testing = require('../lib/async_testing');
2 3
 
3 4
 exports['asynchronousTest'] = function(test) {
4 5
   setTimeout(function() {
@@ -33,6 +34,22 @@ exports['test catch sync error'] = function(test) {
33 34
   throw e;
34 35
 };
35 36
 
  37
+exports['wrapped test'] = function(test, one, two) {
  38
+  test.equal(1, one);
  39
+  test.equal(2, two);
  40
+  test.finish();
  41
+};
  42
+
  43
+function setup(testFunc) {
  44
+  return function newTestFunc(test) {
  45
+    var extra1 = 1;
  46
+    var extra2 = 2;
  47
+    testFunc(test, extra1, extra2);
  48
+  }
  49
+}
  50
+
  51
+async_testing.wrapTests(exports, setup);
  52
+
36 53
 if (module == require.main) {
37  
-  require('../async_testing').run(__filename, process.ARGV);
  54
+  async_testing.run(__filename, process.ARGV);
38 55
 }
7  lib/async_testing.js
@@ -611,3 +611,10 @@ exports.runSuites = function(list, options) {
611 611
     exports.runSuite(suite, itemOpts);
612 612
   }
613 613
 }
  614
+
  615
+// convenience function for wrapping tests in a suite for setup or teardown
  616
+exports.wrapTests = function(obj, wrapper) {
  617
+  for(var key in obj) {
  618
+    obj[key] = wrapper(obj[key]);
  619
+  }
  620
+}
1  test/test-async_assertions.js
@@ -18,6 +18,7 @@ exports['test success -- numAssertionsExpected'] = function(test) {
18 18
       test.finish();
19 19
     }, 500);
20 20
 };
  21
+// test that the num assertions error doesn't override an assertion error
21 22
 exports['test fail -- numAssertionsExpected'] = function(test) {
22 23
   test.numAssertions = 1;
23 24
   setTimeout(function() {
1  test/test-sync_assertions.js
@@ -12,6 +12,7 @@ exports['test success -- numAssertionsExpected'] = function(test) {
12 12
   test.ok(true, 'This should be true');
13 13
   test.finish();
14 14
 };
  15
+// test that the num assertions error doesn't override an assertion error
15 16
 exports['test fail -- numAssertionsExpected'] = function(test) {
16 17
   test.numAssertions = 1;
17 18
   test.ok(false);
80  test/test-wrap_tests.js
... ...
@@ -0,0 +1,80 @@
  1
+var async_testing = require('../lib/async_testing');
  2
+
  3
+function wrapTestsWrapper(func) {
  4
+  return function(test) {
  5
+    test.ok(true, 'make sure we get here');
  6
+    func(test);
  7
+  }
  8
+}
  9
+
  10
+exports['test wrapTests'] = function(test) {
  11
+  test.numAssertions = 1;
  12
+  test.finish();
  13
+}
  14
+
  15
+async_testing.wrapTests(exports, wrapTestsWrapper);
  16
+
  17
+var extra1 = {}
  18
+  , extra2 = {}
  19
+  ;
  20
+
  21
+function setupWrapper(func) {
  22
+  return function(test) {
  23
+    func(test, extra1, extra2);
  24
+  }
  25
+}
  26
+
  27
+exports['test setup'] = setupWrapper(function(test, one, two) {
  28
+  test.strictEqual(one, extra1);
  29
+  test.strictEqual(two, extra2);
  30
+  test.finish();
  31
+});
  32
+
  33
+function errorWrapper(func) {
  34
+  return function(test) {
  35
+    throw new Error();
  36
+  }
  37
+}
  38
+
  39
+// just to make sure errors still work the same way...
  40
+exports['test wrapper errors'] = errorWrapper(function(test) {
  41
+  test.finish();
  42
+});
  43
+
  44
+// writing a teardown function is a bit more complicated because we have to
  45
+// account for the asynchronous nature of Node.  Basically, when a test
  46
+// finishes it calls `finish`, so we hijack that method, wait for it to be
  47
+// called, and then when it is, run our teardown code.  Finally, we have to
  48
+// call the original finish to make the sure the test is finished.
  49
+function teardownWrapper(func) {
  50
+  return function(test) {
  51
+    var finish = test.finish;
  52
+
  53
+    test.finish = function() {
  54
+      // teardown code goes here
  55
+      test.ok(true, 'make sure we get here');
  56
+
  57
+      // finish the test
  58
+      finish();
  59
+    }
  60
+
  61
+    func(test);
  62
+  }
  63
+}
  64
+
  65
+exports['test teardown'] = teardownWrapper(function(test) {
  66
+  test.numAssertions = 1;
  67
+  test.finish();
  68
+});
  69
+
  70
+exports['test teardown async'] = teardownWrapper(function(test) {
  71
+  test.numAssertions = 1;
  72
+
  73
+  setTimeout(function() {
  74
+      test.finish();
  75
+    }, 500);
  76
+});
  77
+
  78
+if (module == require.main) {
  79
+  async_testing.run(__filename, process.ARGV);
  80
+}

0 notes on commit f52589c

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