I'm working on creating a unit testing framework and I'm trying to figure out how the output is possible of the following section of code without the use of global variables:
describe("Test Title", function () {
it("should return a string", function () {
/* Assertions here */
});
});
//OUTPUT: "PASS: Test Title should return a string"
Could someone explain how the it
method call somehow manages to get one of its parameters up to describe
?
If that's not clear, what I'm trying to say is I would like to know how a variable can move through callbacks. If I understand what's happening, a method call to an outside function within a callback somehow gets an argument to another outside function.
You're probably thinking that it
executes the callback passed to it. It doesn't. It just registers the callback as a test.
The simplest implementation is for describe to run tests:
var tests = [];
function describe(description,fn) {
fn();
for (var i=0;i<tests.length;i++) {
try {
tests[i].fn();
console.log('FAIL:' + description + ' ' + tests[i].description);
}
catch (e) {
console.log('FAIL:' + description + ' ' + tests[i].description);
}
}
}
function it(description,fn) {
tests.push({
description: description,
fn: fn
});
}
However, from glancing at the Mocha code it seems that even describe
doesn't really run the code, only register the test suite for another function to process:
function describe(description,fn) {
fn();
testSuites.push({
description: description,
tests: tests.slice(0)
});
}
But the logic is the same. Create a data structure with all the values and then process them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.