dhxmvp
Version:
A complete boilerplate for building online, offline and syncable MVP Single Page Applications using DHTMLX.
255 lines (215 loc) • 6.59 kB
JavaScript
function Test( settings ) {
extend( this, settings );
this.assert = new Assert( this );
this.assertions = [];
this.testNumber = ++Test.count;
}
Test.count = 0;
Test.prototype = {
setup: function() {
if (
// Emit moduleStart when we're switching from one module to another
this.module !== config.previousModule ||
// They could be equal (both undefined) but if the previousModule property doesn't
// yet exist it means this is the first test in a suite that isn't wrapped in a
// module, in which case we'll just emit a moduleStart event for 'undefined'.
// Without this, reporters can get testStart before moduleStart which is a problem.
!hasOwn.call( config, "previousModule" )
) {
if ( hasOwn.call( config, "previousModule" ) ) {
runLoggingCallbacks( "moduleDone", {
name: config.previousModule,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
total: config.moduleStats.all
});
}
config.previousModule = this.module;
config.moduleStats = { all: 0, bad: 0 };
runLoggingCallbacks( "moduleStart", {
name: this.module
});
}
config.current = this;
this.testEnvironment = extend({
setup: function() {},
teardown: function() {}
}, this.moduleTestEnvironment );
this.started = now();
runLoggingCallbacks( "testStart", {
name: this.testName,
module: this.module,
testNumber: this.testNumber
});
if ( !config.pollution ) {
saveGlobal();
}
if ( config.notrycatch ) {
this.testEnvironment.setup.call( this.testEnvironment, this.assert );
return;
}
try {
this.testEnvironment.setup.call( this.testEnvironment, this.assert );
} catch ( e ) {
this.pushFailure( "Setup failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
}
},
run: function() {
config.current = this;
if ( this.async ) {
QUnit.stop();
}
this.callbackStarted = now();
if ( config.notrycatch ) {
this.callback.call( this.testEnvironment, this.assert );
this.callbackRuntime = now() - this.callbackStarted;
return;
}
try {
this.callback.call( this.testEnvironment, this.assert );
this.callbackRuntime = now() - this.callbackStarted;
} catch ( e ) {
this.callbackRuntime = now() - this.callbackStarted;
this.pushFailure( "Died on test #" + ( this.assertions.length + 1 ) + " " + this.stack + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
// else next test will carry the responsibility
saveGlobal();
// Restart the tests if they're blocking
if ( config.blocking ) {
QUnit.start();
}
}
},
teardown: function() {
config.current = this;
if ( config.notrycatch ) {
if ( typeof this.callbackRuntime === "undefined" ) {
this.callbackRuntime = now() - this.callbackStarted;
}
this.testEnvironment.teardown.call( this.testEnvironment, this.assert );
return;
} else {
try {
this.testEnvironment.teardown.call( this.testEnvironment, this.assert );
} catch ( e ) {
this.pushFailure( "Teardown failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
}
}
checkPollution();
},
finish: function() {
config.current = this;
if ( config.requireExpects && this.expected === null ) {
this.pushFailure( "Expected number of assertions to be defined, but expect() was not called.", this.stack );
} else if ( this.expected !== null && this.expected !== this.assertions.length ) {
this.pushFailure( "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run", this.stack );
} else if ( this.expected === null && !this.assertions.length ) {
this.pushFailure( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.", this.stack );
}
var i,
bad = 0;
this.runtime = now() - this.started;
config.stats.all += this.assertions.length;
config.moduleStats.all += this.assertions.length;
for ( i = 0; i < this.assertions.length; i++ ) {
if ( !this.assertions[ i ].result ) {
bad++;
config.stats.bad++;
config.moduleStats.bad++;
}
}
runLoggingCallbacks( "testDone", {
name: this.testName,
module: this.module,
failed: bad,
passed: this.assertions.length - bad,
total: this.assertions.length,
runtime: this.runtime,
// HTML Reporter use
assertions: this.assertions,
testNumber: this.testNumber,
// DEPRECATED: this property will be removed in 2.0.0, use runtime instead
duration: this.runtime
});
config.current = undefined;
},
queue: function() {
var bad,
test = this;
function run() {
// each of these can by async
synchronize(function() {
test.setup();
});
synchronize(function() {
test.run();
});
synchronize(function() {
test.teardown();
});
synchronize(function() {
test.finish();
});
}
// `bad` initialized at top of scope
// defer when previous test run passed, if storage is available
bad = QUnit.config.reorder && defined.sessionStorage &&
+sessionStorage.getItem( "qunit-test-" + this.module + "-" + this.testName );
if ( bad ) {
run();
} else {
synchronize( run, true );
}
},
push: function( result, actual, expected, message ) {
var source,
details = {
module: this.module,
name: this.testName,
result: result,
message: message,
actual: actual,
expected: expected,
testNumber: this.testNumber
};
if ( !result ) {
source = sourceFromStacktrace();
if ( source ) {
details.source = source;
}
}
runLoggingCallbacks( "log", details );
this.assertions.push({
result: !!result,
message: message
});
},
pushFailure: function( message, source, actual ) {
if ( !this instanceof Test ) {
throw new Error( "pushFailure() assertion outside test context, was " + sourceFromStacktrace( 2 ) );
}
var details = {
module: this.module,
name: this.testName,
result: false,
message: message || "error",
actual: actual || null,
testNumber: this.testNumber
};
if ( source ) {
details.source = source;
}
runLoggingCallbacks( "log", details );
this.assertions.push({
result: false,
message: message
});
}
};
QUnit.pushFailure = function() {
if ( !QUnit.config.current ) {
throw new Error( "pushFailure() assertion outside test context, in " + sourceFromStacktrace( 2 ) );
}
// Gets current test obj
var currentTest = QUnit.config.current.assert.test;
return currentTest.pushFailure.apply( currentTest, arguments );
};