Mocha没有't在after()函数中调用rmdir
Mocha doesn't call rmdir in after() function
我试图在测试前创建一些目录树,并最终将其删除:
require('should');
var fs = require('fs');
var rmdir = require('rmdir');
describe('FsWatcher', function() {
before(function(done) {
console.log("before");
fs.mkdir('tmp');
fs.mkdir('tmp/css');
fs.mkdir('tmp/js');
fs.mkdir('tmp/lib');
fs.open('tmp/index.htm', 'w');
// (...)
console.log("after_before");
done();
});
describe('single file', function() {
it('should succed', function(done) {
(1).should.equal(1);
done();
});
});
after(function() {
console.log("after");
rmdir('tmp', function() {
console.log("rmdir!");
});
console.log("after_end");
});
});
console.log
s的顺序正常:
mocha `find tests -name '*.js'`
before
after_before
․after
after_end
✔ 1 test complete (6 ms)
但它不调用rmdir((。相同的rmdir在before()
:结束时工作
describe('FsWatcher', function() {
before(function(done) {
console.log("before");
fs.mkdir('tmp');
fs.mkdir('tmp/css');
fs.mkdir('tmp/js');
fs.mkdir('tmp/lib');
fs.open('tmp/index.htm', 'w');
// (...)
console.log("after_before");
rmdir('tmp', function(){
console.log("rmdir!");
});
done();
});
});
控制台:
mocha `find tests -name '*.js'`
before
after_before
rmdir!
after
after_end
为什么它在after()
中不起作用?
测试完成得太快。您需要使用after的异步版本。
after(function(done) {
console.log("after");
rmdir('tmp', function() {
console.log("rmdir!");
done();
});
console.log("after_end");
});
哪个应该输出:
after
after_end
rmdir!
当您从before()
调用它时,它似乎能工作,是因为还有很多其他事情发生,所以rmdir会被调用,但稍后当节点返回到事件循环时。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- Mocha没有't在after()函数中调用rmdir