testing:摩卡和节点不同

首先,虽然我是一个有经验的程序员,但我对节点相对比较陌生,而且对于摩卡来说是非常新的,所以我的问题可能就是我! 在这种情况下,我也乐意发现这一点!

我正在写的一些代码使用了一个npm模块,其中包含对fs.exists的调用。 当我通过节点vs运行vs mocha运行fs.exists运行testing时得到了不同的结果; 在我看来,我应该得到同样的答案。 该文件确实存在,所以在两种情况下结果都应该是true,但是在通过节点调用时为true,而在通过mocha调用时为false。 那么,它不能正常工作。

我知道fs.exists已被弃用,但是在我使用的npm模块中,所以除非我修改模块,这就是我正在使用的模块。

不同的是我如何调用摩卡。

根据一些网站的build议,我编辑了我的package.json文件,在“脚本”部分包含了下面的语句:“test”:“mocha test”,并且把testing放在这个“test”目录下。 这是通过npmtesting调用的。

遇到问题后,我也安装了npm install -g mocha。

我的testing文件是testVersion.js

结果是,当我通过节点npm test和mocha testVersion.js调用fs.exists时调用以下三种方法之一时,我得到了不同的答案; 我期待'真': – node testVersion.js返回true – 它find文件。 – mocha testVersion.js返回true – 它find文件。 – npmtesting返回false – 它没有find该文件。

我怀疑摩卡或节点的不同版本正在被调用,但没有足够的经验可以自行确定。

所以我的问题是:为什么我得到不同的结果?

这是我的代码testVersion.js

var expect = require('chai').expect; var assert = expect.assert; var fs = require("fs"); var isInTest = typeof global.it === 'function'; //exe via mocha or node console.log('isInTest mode: ', isInTest); if(!isInTest) { console.log('NOT TEST MODE: invoking fs.exists'); fs.exists("testVersion.js", function(info) { console.log('NOT TEST MODE: fs.exists callback RAN.'); console.log('NOT TEST MODE: fs.exists: should be true and is: ', info); }) } if(isInTest) { describe("Test Mocha and fs.exists issue", function() { it("Test that file self exists.", function() { console.log('TEST MODE: invoking fs.exists'); expect(fs.exists("testVersion.js", function(result) { console.log('TEST MODE: fs.exists callback RAN.'); console.log('TEST MODE: fs.exists: should be true, and is: ', result); return true; })).to.equal(true); }); }); }; 

经过大量的testing工作,我相信我的问题是因为与fs.exists关联的asynchronous调用。 无论如何,下面是为我工作,我想logging它,以防别人帮助其他人。

 var assert = require('assert'); var fs = require('fs'); fs.existsSync("bbq.js", function(result) { console.log('False: fs.exists(bbq.js) says: ', result); }) fs.existsSync("test2.js", function(result) { console.log('True: fs.exists(test2.js) says: ', result); }) describe('Testing Synch fs.existsSync() ===', function() { describe('False: fs.exists(bbq.js)', function() { it("This assertion should pass, as we are asserting false on file that doesn't exist.", function() { assert.equal(false, fs.existsSync(__dirname + "/bbq.js", function(result) { return result;})) }); }); describe('Testing Synch fs.existsSync(test2.js method A)', function() { it("This assertions should pass as we are asserting true on file that does exist", function() { assert.equal(true, fs.existsSync(__dirname + "/test2.js", function(result) { return result; })); }); }); describe('Testing Synch fs.existsSync(test2.js method B)', function() { it("This assertions should pass as we are are asserting true on file that does exist using callback", function() { fs.existsSync(__dirname + "/test2.js", function(result) { assert.equal(true, result); }); }); }); }); //////////////////// describe('Asynch test of fs.exists() === some results are UNDEFINED because of async', function() { describe('False: fs.exists(bbq.js)', function() { it("This assertion should pass as we are expecting undefined result due to async.", function() { assert.equal(undefined, fs.exists(__dirname + "/bbq.js", function(result) { return result;})) }); }); describe('True: fs.exists(test2.js method A)', function() { it("This assertion should pass as we are expecting undefined result due to async.", function() { assert.equal(undefined, fs.exists(__dirname + "/test2.js", function(result) { return result; })); }); }); describe('True: fs.exists(test2.js method B)', function() { it("This equal assertion passes, because of use of callback waits for response.", function() { fs.exists(__dirname + "/test2.js", function(result) { assert.equal(true, result); }); }); }); });