我现在正在使用 mocha 进行 javascript 单元测试。
我有几个测试文件,每个文件都有一个before
and beforeEach
,但它们完全一样。
我如何为他们所有人(或其中一些人)提供一个全球before
性的?beforeEach
我现在正在使用 mocha 进行 javascript 单元测试。
我有几个测试文件,每个文件都有一个before
and beforeEach
,但它们完全一样。
我如何为他们所有人(或其中一些人)提供一个全球before
性的?beforeEach
在 test 文件夹的根目录中,创建一个全局测试助手test/helper.js
,其中包含你的 before 和 beforeEach
// globals
global.assert = require('assert');
// setup
before();
beforeEach();
// teardown
after();
afterEach();
来自mocha 文档……</p>
根级挂钩
您还可以选择任何文件并添加“根”级挂钩。例如,在所有 describe() 块之外添加 beforeEach()。这将导致对 beforeEach() 的回调在任何测试用例之前运行,无论它位于哪个文件中(这是因为 Mocha 有一个隐含的 describe() 块,称为“根套件
describe()
首先收集所有常规套件,然后再运行,这可以保证首先调用它。
'use strict'
let run = false
beforeEach(function() {
if ( run === true ) return
console.log('GLOBAL ############################')
run = true
});
如果您想在每次测试之前看到它每次运行,请删除运行标志。
我命名了这个文件test/_beforeAll.test.js
。它不需要在任何地方导入/需要,但文件名中的.test.
(resp. .spec.
) 很重要,以便您的测试运行者将其拾取……</p>
mocha.opts
\o/如果有东西,你真的只想在运行测试之前设置一次(不管是哪个......),这mocha.opts
是一个令人惊讶的优雅选择!– 只需将 a 添加require
到您的文件中(是的,即使它对 mocha 贡献不大,而是对您的测试设置贡献不大)。它会在之前可靠地运行一次:
(在这个例子中,我检测到,如果单个测试或多个测试即将运行。在前一种情况下,我输出 every log.info()
,而在完全运行时,我将详细程度降低为 error+warn...)
更新:
如果有人知道一种方法,可以访问即将运行的 mocha 套件的一些基本属性once.js
,我很想知道并在这里添加。(即我的suiteMode
-detection 很糟糕,如果有另一种检测方法,要运行多少测试......)
在单独的文件中声明 a before
or (我使用)并要求它。beforeEach
spec_helper.coffee
spec_helper.coffee
afterEach (done) ->
async.parallel [
(cb) -> Listing.remove {}, cb
(cb) -> Server.remove {}, cb
], ->
done()
test_something.coffee
require './spec_helper'
mochaHooks
Mocha 8 上的根钩子插件最小示例
该机制目前记录在:https ://mochajs.org/#root-hook-plugins
它不适用于before
,仅适用于beforeEach
,因为before
不在以下可用挂钩列表中:https ://mochajs.org/#available-root-hooks
这是一个演示:
测试/global.js
// Root hook.
exports.mochaHooks = {
beforeEach(done) {
console.log('mochaHooks.beforeEach');
done();
},
};
// Bonus: global fixture, runs once before everything.
exports.mochaGlobalSetup = async function() {
console.log('mochaGlobalSetup');
};
测试/mytest.js
var assert = require('assert');
describe('describe0', function() {
// Only runs before the current describe.
before(async () => {
console.error('before describe 0');
});
beforeEach(async () => {
console.error('beforeEach describe 0');
});
it('it 0 0', function() {
assert.equal(0, 0);
});
it('it 0 1', function() {
assert.equal(0, 0);
});
describe('describe 0 0', function() {
before(async () => {
console.error('before describe 0 0');
});
beforeEach(async () => {
console.error('beforeEach describe 0 0');
});
it('it 0 0 0', function() {
assert.equal(0, 0);
});
it('it 0 0 1', function() {
assert.equal(0, 0);
});
});
describe('describe 0 1', function() {
before(async () => {
console.error('before describe 0 1');
});
beforeEach(async () => {
console.error('beforeEach describe 0 1');
});
it('it 0 1 0', function() {
assert.equal(0, 0);
});
it('it 0 1 1', function() {
assert.equal(0, 0);
});
});
});
然后使用以下命令启用该文件--require
:
npx mocha --require test/global.js test/
结果:
mochaGlobalSetup
describe0
before describe 0
mochaHooks.beforeEach
beforeEach describe 0
✓ it 0 0
mochaHooks.beforeEach
beforeEach describe 0
✓ it 0 1
describe 0 0
before describe 0 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 0
✓ it 0 0 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 0
✓ it 0 0 1
describe 0 1
before describe 0 1
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 1
✓ it 0 1 0
mochaHooks.beforeEach
beforeEach describe 0
beforeEach describe 0 1
✓ it 0 1 1
6 passing (6ms)
所以我们看到 global 钩子在每个 local 之前运行beforeEach
。
因为before
我找不到比定义一个助手并从每个地方调用它更好的解决方案before
:如何让 Mocha 加载一个定义全局挂钩或实用程序的 helper.js 文件?
在 mocha 8.3.2、Node v14.16.0 上测试。
当我需要“模拟”依赖项之一使用的全局变量时,我遇到了类似的问题。
我为此使用了 .mocharc.js,因为在设置“mocha”环境时,该 JS 文件中的代码会被执行一次。
示例 .mocharc.js:
global.usedVariable = "someDefinedValue";
/** other code to be executed when mocha env setup **/
module.exports = {};
这对我有用,但是这样做看起来很“肮脏”。如果您知道该代码的更好位置,请发表评论:)
使用模块可以更轻松地为您的测试套件进行全局设置/拆卸。这是一个使用 RequireJS(AMD 模块)的示例:
首先,让我们使用全局设置/拆卸来定义一个测试环境:
// test-env.js
define('test-env', [], function() {
// One can store globals, which will be available within the
// whole test suite.
var my_global = true;
before(function() {
// global setup
});
return after(function() {
// global teardown
});
});
在我们的 JS 运行器中(包含在 mocha 的 HTML 运行器中,以及其他库和测试文件中,作为<script type="text/javascript">…</script>
.,或者更好,作为外部 JS 文件):
require([
// this is the important thing: require the test-env dependency first
'test-env',
// then, require the specs
'some-test-file'
], function() {
mocha.run();
});
some-test-file.js
可以这样实现:
// some-test-file.js
define(['unit-under-test'], function(UnitUnderTest) {
return describe('Some unit under test', function() {
before(function() {
// locally "global" setup
});
beforeEach(function() {
});
afterEach(function() {
});
after(function() {
// locally "global" teardown
});
it('exists', function() {
// let's specify the unit under test
});
});
});