其实手动配置babel环境并不难,记录下步骤:
1、首先npm init创建一个nodejs项目
2、全局安装babel-cli处理工具:npm i babel-cli -g
3、cd到项目下安装babel依赖:npm i babel-preset-es2015 babel-preset-stage-3 --save-dev,这俩包主要是处理es6转码需要使用的
4、配置.babelrc文件:
{ "presets": [ "es2015", "stage-3" ], "plugins": [] }
5、编写我们的测试代码:
var sleep = function(time) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(`the program stopped ${time}ms`); }, time); }); } var start = async function() { console.log("start"); var sleeptime = await sleep(3000); console.log(sleeptime); console.log("end"); } start();
6、cmd窗口执行:babel-node index.js,说明下:babel-node命令会可以理解为开启了一个新的node环境,该环境下es6代码被支持,当然,你也可以使用其它babel命令,比如:babel index.js -o index.compile.js,然后再执行node index.compile.js即可实现相同效果,关于babel详细介绍可以参考官网使用说明及参数说明。如下是输出打印:
start the program stopped 3000ms end
再来看一个例子,是在async函数中使用for循环调用async函数,直接贴代码了:
var sleep = function(time) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(`the program stopped ${time}ms`); }, time); }); } var start = async function() { console.log("start"); var sleeptime = await sleep(3000); console.log(sleeptime); console.log("end"); } // start(3000); var sleep2 = function(time) { return new Promise(function(resolve, reject) { setTimeout(function() { reject("error accured!"); }, time); }); } var start2 = async function() { try { console.log("start"); var sleeptime = await sleep2(3000); console.log(sleeptime); console.log("end"); } catch (e) { console.log(e); } } // start2(3000); /*async函数的上下文 */ var asyncForFunc = async function(time) { for (var i = 0; i < 10; i++) { console.log(`当前开始的是是第${i+1}次循环`); var time2 = await sleep(time); console.log(`当前是第${i+1}次输出:${time2}`); } } asyncForFunc(1000);
程序正确输出:
循环输出如果改成forEach的话就会直接报错,因为forEach函数改变了await的上下文:await必须出现在async函数中,而forEach非async函数。
额外记录一些东西,那就是nodejs中测试用例的编写:
主要使用的npm包:mocha(测试工具)、should(断言工具)、istanbul(case覆盖率测试工具),如下是待测试的代码:
var fibonacci = function(n) { if (typeof n != "number") { throw new Error("n should be a number"); } if (n < 0) { throw new Error("n should >= 0"); } if (n <= 1) { return n; } if (n > 10) { throw new Error("n should <= 10"); } return fibonacci(n - 1) + fibonacci(n - 2); } if (require.main == module) { //如果是直接执行main,则进入此处 //如果是被其他js引入,则此处不会执行 var n = Number(process.argv[2]); console.log(`fibonacci(${n}) is ${fibonacci(n)}`); } exports.fibonacci = fibonacci;
接下来我们编写测试用例,新建文件夹test,并main.test.js文件:
var main = require("../main"); var should = require("should"); describe("test/main.test.js", function() { it("should equal 55 when n === 10", function() { // done(); //done可以传一个err参数,err不为空时直接中断后续操作,空时不中断 main.fibonacci(10).should.equal(55); }); it("should equal 0 when n === 0", function() { main.fibonacci(0).should.equal(0); }); it("should equal 1 when n === 1", function() { main.fibonacci(1).should.equal(1); }); it("should throw when n > 10", function() { (function() { main.fibonacci(11); }).should.throw("n should <= 10"); }); it("should throw when n < 0", function() { (function() { main.fibonacci(-1); }).should.throw("n should >= 0"); }); it("should throw when n isnt number", function() { (function() { main.fibonacci('hehe'); }).should.throw("n should be a number"); }); });
然后再在根目录输入命令mocha进行测试,可能会有如下输出,分别表示测试不通过与通过示例:
测试不通过:
测试通过:
使用istanbul主要是进行代码覆盖率测试,详细介绍可以参考阮老师的文章:
在当前项目根目录下执行命令:istanbul cover _mocha 即可,看下输出如下:
这里罗列出了语句覆盖率测试、分支覆盖率测试、函数覆盖率以及行覆盖率测试的结果,over。。。