如何检测mocha测试是否在node.js中运行?

时间:2021-10-22 23:15:19

I want to make sure that in case the code is running in test mode, that it does not (accidentally) access the wrong database. What is the best way to detect if the code is currently running in test mode?

我想确保代码在测试模式下运行,它不会(意外地)访问错误的数据库。检测代码当前是否在测试模式下运行的最佳方法是什么?

2 个解决方案

#1


14  

As already mentioned in comment it is bad practice to build your code aware of tests. I even can't find mentioned topic on SO and even outside. However, I can think of ways to detect the fact of being launched in test. For me mocha doesn't add itself to global scope, but adds global.it. So your check may be

正如评论中已经提到的那样,构建代码意识到测试是不好的做法。我甚至无法在SO甚至外面找到提到的话题。但是,我可以想办法检测在测试中启动的事实。对我来说,mocha不会将自己添加到全局范围,但添加了global.it。所以你的支票可能是

var isInTest = typeof global.it === 'function';

I would suggest to be sure you don't false-detect to add check for global.sinon and global.chai which you most likely used in your node.js tests.

我建议你确保不要进行错误检测,以便为你最有可能在node.js测试中使用的global.sinon和global.chai添加检查。

#2


3  

Inspecting process.argv is a good approach in my experience.

根据我的经验,检查process.argv是一个很好的方法。

For instance if I console.log(process.argv) during a test I get the following:

例如,如果我在测试期间调试console.log(process.argv),我会得到以下内容:

[
  'node',
  '/usr/local/bin/gulp',
  'test',
  '--file',
  'getSSAI.test.unit.js',
  '--bail',
  '--watch'
]

From which you can see that gulp is being used. Using yargs makes interpretting this a whole lot easier.

从中您可以看到正在使用gulp。使用yargs可以更轻松地解释这一点。

I strongly agree with Kirill and in general that code shouldn't be aware of the fact that it's being tested (in your case perhaps you could pass in your db binding / connection via a constructor?), for things like logging I can see why you might want to detect this.

我非常同意Kirill,并且一般来说代码不应该意识到它正在被测试(在你的情况下你可以通过构造函数传递你的数据库绑定/连接吗?),对于像日志这样的东西,我可以看到为什么你可能想要发现这个。

#1


14  

As already mentioned in comment it is bad practice to build your code aware of tests. I even can't find mentioned topic on SO and even outside. However, I can think of ways to detect the fact of being launched in test. For me mocha doesn't add itself to global scope, but adds global.it. So your check may be

正如评论中已经提到的那样,构建代码意识到测试是不好的做法。我甚至无法在SO甚至外面找到提到的话题。但是,我可以想办法检测在测试中启动的事实。对我来说,mocha不会将自己添加到全局范围,但添加了global.it。所以你的支票可能是

var isInTest = typeof global.it === 'function';

I would suggest to be sure you don't false-detect to add check for global.sinon and global.chai which you most likely used in your node.js tests.

我建议你确保不要进行错误检测,以便为你最有可能在node.js测试中使用的global.sinon和global.chai添加检查。

#2


3  

Inspecting process.argv is a good approach in my experience.

根据我的经验,检查process.argv是一个很好的方法。

For instance if I console.log(process.argv) during a test I get the following:

例如,如果我在测试期间调试console.log(process.argv),我会得到以下内容:

[
  'node',
  '/usr/local/bin/gulp',
  'test',
  '--file',
  'getSSAI.test.unit.js',
  '--bail',
  '--watch'
]

From which you can see that gulp is being used. Using yargs makes interpretting this a whole lot easier.

从中您可以看到正在使用gulp。使用yargs可以更轻松地解释这一点。

I strongly agree with Kirill and in general that code shouldn't be aware of the fact that it's being tested (in your case perhaps you could pass in your db binding / connection via a constructor?), for things like logging I can see why you might want to detect this.

我非常同意Kirill,并且一般来说代码不应该意识到它正在被测试(在你的情况下你可以通过构造函数传递你的数据库绑定/连接吗?),对于像日志这样的东西,我可以看到为什么你可能想要发现这个。