11var Fiber = Npm . require ( "fibers" ) ;
22
33setupGlobals = function ( mocha ) {
4- //basically a direct copy from meteor/packages/meteor/dynamics_nodejs.js
5- //except the wrapped function has an argument (mocha distinguishes
6- //asynchronous tests from synchronous ones by the "length" of the
7- //function passed into it, before, etc.)
8- var moddedBindEnvironment = function ( func , onException , _this ) {
9- Meteor . _nodeCodeMustBeInFiber ( ) ;
10-
11- var boundValues = _ . clone ( Fiber . current . _meteor_dynamics || [ ] ) ;
12-
13- if ( ! onException || typeof ( onException ) === 'string' ) {
14- var description = onException || "callback of async function" ;
15- onException = function ( error ) {
16- Meteor . _debug (
17- "Exception in " + description + ":" ,
18- error && error . stack || error
19- ) ;
20- } ;
21- }
22-
23- //IMPORTANT note the callback variable present here, for
24- //Metoer.bindEnvironment this is ` return function (/* arguments */) {`
25- return function ( callback ) {
26- var args = _ . toArray ( arguments ) ;
27- // If _this is not provided, then function 'this' will be used,
28- // which is the mocha suite context in 'it', 'beforeEach', etc.
29- _this = _this || this ;
30-
31- var runWithEnvironment = function ( ) {
32- var savedValues = Fiber . current . _meteor_dynamics ;
33- try {
34- // Need to clone boundValues in case two fibers invoke this
35- // function at the same time
36- Fiber . current . _meteor_dynamics = _ . clone ( boundValues ) ;
37- var ret = func . apply ( _this , args ) ;
38- } catch ( e ) {
39- onException ( e ) ;
40- } finally {
41- Fiber . current . _meteor_dynamics = savedValues ;
42- }
43- return ret ;
44- } ;
45-
46- if ( Fiber . current )
47- return runWithEnvironment ( ) ;
48- Fiber ( runWithEnvironment ) . run ( ) ;
49- } ;
50- } ;
51-
52-
534
545 var mochaExports = { } ;
556 mocha . suite . emit ( "pre-require" , mochaExports , undefined , mocha ) ;
56- //console.log(mochaExports);
577
588 // 1. patch up it and hooks functions so it plays nice w/ fibers
599 // 2. trick to allow binding the suite instance as `this` value
@@ -74,36 +24,39 @@ setupGlobals = function(mocha){
7424 //fiber, but it was unclear to me how that could be done without
7525 //forking mocha itself.
7626
77- var wrappedFunc = function ( callback ) {
7827
79- if ( func . length == 0 ) {
80- func . call ( this ) ;
81- callback ( ) ;
82- }
83- else {
84- func . call ( this , callback ) ;
28+ var wrappedFunction = function ( done ) {
29+ var self = this ;
30+ var run = function ( ) {
31+ try {
32+ if ( func . length == 0 ) {
33+ func . call ( self ) ;
34+ done ( ) ;
35+ }
36+ else {
37+ func . call ( self , done ) ;
38+ }
39+ } catch ( error ) {
40+ done ( error ) ;
41+ }
42+ } ;
43+
44+ if ( Fiber . current ) {
45+ return run ( ) ;
8546 }
47+ Fiber ( run ) . run ( ) ;
8648 } ;
8749
88- var boundWrappedFunction = moddedBindEnvironment ( wrappedFunc , function ( err ) {
89- throw err ;
90- } ) ;
91-
92- // Show original function source code
93- boundWrappedFunction . toString = function ( ) { return func . toString ( ) } ;
94- return boundWrappedFunction
50+ // Show original's function source code
51+ wrappedFunction . toString = function ( ) { return func . toString ( ) } ;
52+ return wrappedFunction ;
9553 } ;
9654
97- global . describe = function ( name , func ) {
98- return mochaExports . describe ( name , moddedBindEnvironment ( func , function ( err ) { throw err ; } ) , this ) ;
99- } ;
55+ global . describe = mochaExports . describe ;
10056 global . describe . skip = mochaExports . describe . skip ;
101- global . describe . only = function ( name , func ) {
102- mochaExports . describe . only ( name , moddedBindEnvironment ( func , function ( err ) { throw err ; } ) , this ) ;
103- } ;
57+ global . describe . only = mochaExports . describe . only ;
10458
10559 global [ 'it' ] = function ( name , func ) {
106-
10760 // You can create pending tests without a callback
10861 // http://mochajs.org/#pending-tests
10962 // i.e pending test
@@ -123,4 +76,4 @@ setupGlobals = function(mocha){
12376 mochaExports [ testFunctionName ] ( wrapRunnable ( func ) ) ;
12477 }
12578 } ) ;
126- } ;
79+ } ;
0 commit comments