本文介绍了如何让Mocha加载一个定义全局钩子或实用程序的helper.js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 test / helper.js 的文件,用于在我的Node.js应用程序上运行Mocha测试。我的测试结构如下所示:

I have a file named test/helper.js that I use to run Mocha tests on my Node.js apps. My tests structure looks like:

test/
test/helper.js    # global before/after
test/api/sometest.spec.js
test/models/somemodel.spec.js
... more here

必须加载文件 helper.js ,因为它包含我的测试套件的全局挂钩。当我运行Mocha来执行这样的整个测试套件时:

The file helper.js has to be loaded because it contains global hooks for my test suite. When I run Mocha to execute the whole test suite like this:

mocha --recursive test/

在我的测试和我的<$ c之前加载 helper.js 文件$ c>在之前按预期执行钩子。

the helper.js file is loaded before my tests and my before hook gets executed as expected.

但是,当我只运行一个特定的测试时,帮助器。测试前未加载js 。我就是这样运行的:

However, when I run just one specific test, helper.js is not loaded before the test. This is how I run it:

mocha test/api/sometest.spec.js

在调用之前没有全局,甚至没有 console.log ('我在这里');

那么我怎样才能让Mocha 总是加载我的 helper.js file?

So how can I get Mocha to always load my helper.js file?

推荐答案

Mocha没有任何特殊的概念名为 helper.js 的文件,它将在其他文件之前加载。

Mocha does not have any notion of a special file named helper.js that it would load before other files.

当你运行 mocha --recursive 时,你想要做的是什么,因为Mocha的顺序发生来加载你的文件。因为 helper.js 比其他文件高一级,所以首先加载它。当您为Mocha指定单个文件时,Mocha只会加载此文件,并且如您所发现的那样,您的 helper.js 文件未加载到所有。

What you are trying to do works when you run mocha --recursive because of the order in which Mocha happens to load your files. Because helper.js is one level higher than the other files, it is loaded first. When you specify an individual file to Mocha, then Mocha just loads this file and, as you discovered, your helper.js file is not loaded at all.

所以你要做的是加载一个文件,使它设置顶级(全局)钩子(例如之前之后的等)。选项:

So what you want to do is load a file such that it will set top level ("global") hooks (e.g. before, after, etc.). Options:


  1. 您可以使用并按照您想要的顺序提供文件。

  1. You could use Mocha programmatically and feed it the files in the order you want.

在列出任何其他文件之前,您可以强制自己始终在命令行 上指定帮助文件。 (我不会这样做,但有可能。)

You could force yourself to always specify your helper file on the command line first before you list any other file. (I would not do this, but it is possible.)

另一种选择是组织你的套房,就像我在。基本上,您有一个顶级文件,可以将套件的其余部分加载到其中。使用此方法,您将失去在单个文件上运行Mocha的功能,但您可以使用 - grep 来选择正在运行的内容。

Another option would be to organize your suite like I've detailed in this answer. Basically, you have one "top level" file that loads the rest of the suite into it. With this method you'd lose the ability of running Mocha on individual files, but you could use --grep to select what is being run.

无法使用 -r 选项。它在运行套件之前加载一个模块,但不幸的是,加载的模块无法访问Mocha为测试提供的任何测试接口,因此无法设置挂钩。

You cannot use the -r option. It loads a module before running the suite but, unfortunately, the loaded module does not have access to any of the testing interface that Mocha makes available to your tests so it cannot set hooks.

这篇关于如何让Mocha加载一个定义全局钩子或实用程序的helper.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:20