我需要在集成测试之前创建一些文件:
before(function(done) {
fs.mkdir('tmp');
fs.mkdir('tmp/css');
fs.mkdir('tmp/js');
fs.mkdir('tmp/lib');
fs.open('tmp/index.htm', 'w');
fs.open('tmp/some.js', 'w');
fs.open('tmp/some.css', 'w');
fs.open('tmp/css/main.css', 'w');
fs.open('tmp/css/sub.css', 'w');
fs.open('tmp/js/main.js', 'w');
fs.open('tmp/lib/jquery.js', 'w');
done();
});
问题是,我得到:
fs: missing callback Error: ENOENT, mkdir 'tmp/css'
fs: missing callback Error: ENOENT, mkdir 'tmp/js'
fs: missing callback Error: ENOENT, mkdir 'tmp/lib'
fs: missing callback Error: ENOENT, open 'tmp/css/main.css'
fs: missing callback Error: ENOENT, open 'tmp/css/sub.css'
fs: missing callback Error: ENOENT, open 'tmp/js/main.js'
fs: missing callback Error: ENOENT, open 'tmp/lib/jquery.js'
我认为问题在于,它尝试在创建目录之前同步打开文件。我可以使用fs.mkdirSync
并向每个人传递一个回调,但是我遇到了done();
. 如果我想让它以正确的方式完成,这段代码应该是什么样子?