When refactoring JavaScript, I hope to introduce an easy-to-use testing framework to ensure the smooth progress of the refactoring. In the future, I can continue to ensure the correctness of JavaScript logic through test code.
JsUnit (http://sourceforge.net/projects/jsunit/, http://www.jsunit.net/)
JsUnit is an independent JavaScript unit testing framework, similar to JUnit, with no difficulty in getting started, including traditional setUp Like tearDown, the assert method provided is similar to JUnit, with additional JavaScript-specific methods such as assertNaN and assertUndefined. The test page must introduce the js file jsUnitCore.js in
.
Test suite support: addTestPage and addTestSuite are provided;
Test log support: including three log levels: warn, info and debug. Front-end coding is not like back-end code. It is not suitable to use too many logs in formal code. Besides, the log is only FF. Only support next time.
A thousand words are not enough but one example:
Js code
//Module JS
function testWithMainProcess() {
assertEquals("Web play url", "##http://...##", webOnlinePlay());
}
The project code is full of Ajax calls. To do unit testing, it seems that piling is inevitable. There are many Mock tools, such as QMock suitable for JQuery:
Js code
var mockJquery = new Mock();
mockJquery
.expects(1)
.method('ajax')
.withArguments({
url: ' http://xxx,
success: Function,
dataType: "jsonp"
})
.callFunctionWith({ feed : { entry : "data response" }});
This pile is just a fake ajax mock Jason returns: [feed:[entry:"data response"]], take a look, the use is almost the same as EasyMock that I have been exposed to before.
Students who are interested in JavaScript testing frameworks can also learn about some other testing frameworks, such as JSpec.
It is recommended that the unit test code be placed in the module's package: test.html. Even under ideal conditions, when the module is released separately, it will be accompanied by reliable front-end code of test cases.
Which JavaScript code should I start with?
1. Functional code. Such code ensures good independence, does not require any staking, and has low testing costs. If you do not understand the meaning of functional code, please refer to "Functional Programming".
2. Complex logic.
Trying TDD? It is not recommended to be used within our team. Front-end TDD requires higher skills and higher requirements on human factors. If one day TDD of the background Java code is completed, there will be no essential difference if it is replaced by JavaScript code.
If the effect is appropriate, why can’t JavaScript UT be integrated into ICP-CI as part of continuous integration?