问题标签 [nodeunit]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
2137 浏览

node.js - 在节点单元中的测试套件之前运行设置代码

在编写自动化系统/集成测试时,通常在所有测试之前运行的第一步是“启动服务器”。由于启动服务器的成本可能很高,因此最好只做一次,而不是在每次单独测试之前。JUnit 具有执行此操作的简单功能。nodeunit中是否有等效的标准模式?还是需要用手滚动一些东西?

0 投票
0 回答
560 浏览

javascript - 从单独模块导出的 mocha 测试返回值

我有一个 REST API,我的一个测试需要由另一个 API 创建的某些资源......下面的示例演示了该模式......问题是

  1. 这是与 Mocha 一起使用的合适模式吗
  2. 如果是这样,您如何建议我将创建的用户的 ID 放入我的帐户测试用例中?
  3. 其他推荐的方法?

    /li>

这我有另一个帐户测试,它必须有一个用户设置。我不想为这些类型的依赖测试再次做这项工作

0 投票
1 回答
57 浏览

node.js - 如何让 nodeunit 报告以前的错误?

我目前正在测试响应对象中是否存在属性。但是如果它不存在,那么下一个测试会使整个事情陷入困境,因为 test.done() 永远不会被调用。

结果是……

失败:未完成的测试(或其设置/拆卸):- PostResourseGetSucceeds

要解决此问题,请确保所有测试都调用 test.done()

我怎样才能让测试仍然报告初始测试?我试过放入 if 语句,但它似乎很麻烦。

0 投票
1 回答
194 浏览

node.js - 如何使用 nodeunit 测试 grunt 任务回调?

我有一个 nodeunit 测试来测试我的自定义 grunt 任务:

根据nodeunit 文档,测试似乎设置正确,但从未执行回调,并且测试继续无限期地运行。

我知道在正确的条件下肯定会执行回调,所以这不是我任务中的错误。

0 投票
1 回答
170 浏览

javascript - 在 NodeUnit 中首先运行 init - 单独的文件

目前我正在开发 nodeunit 测试,我偶然发现了一个问题。

我有一个名为“Unit_tests”的文件夹

在这个文件夹中,我有一个文件“testInitializer.js”,其中包含 2 个方法: initterm

这些方法应该只运行一次,并且只在所有其他测试的开始和结束时运行。其他测试位于根“Unit_tests”下的单独文件夹中。

如何确保首先运行此文件。

截至目前,它主要是,但在某些情况下它没有。有没有办法webstorm确保它首先运行?

提前谢谢了!

0 投票
1 回答
1738 浏览

javascript - Node.js module unit testing - stubbing asynchronous file system calls with sinon.js

I've been struggling with this now for several hours and I'm not getting anywhere. This is a simplified version of the code I'm working with:

I have a module called 'setup' that internally reads files from the filesystem, and does some stuff. I'm attempting to write some unit tests that use stubs to return fakedata rather than read from the file system. I'm using the sandbox feature of nodeunit since the functions that I am stubbing are not exported.

index.js:

loader.js:

data.js:

test-setup.js:

Test 1 passes. Test 2 fails with: AssertionError: returned value is the fakedata from the stub

The log messages show that it has printed the data from the file rather than the fakedata. When I inspect the this.sandbox.loader function in the debugger it is correctly set to the stub.

The second issue is that I had to fake out the require and pass in the loader function with:

require : function () { return loader; },

Otherwise require couldn't find the ./loader. I tried to change directory with process.chdir before the test, but the require still failed even when the cwd was correctly set to the project directory. Obviously faking out require would only work if there is 1 module required, in the real code there are several requires. The only other way I got the ./loader to load was to modify the code to use an absolute path, and editing the code each time to run the test isn't feasible.

I've probably missed something obvious, writing unit tests for module code that loads data asynchronously must be pretty standard stuff. Is sandbox/stubbing the right approach?

Any help much appreciated.

0 投票
0 回答
150 浏览

javascript - 在 Express 单元测试中覆盖异步函数

我今天大部分时间都在努力为我的应用程序编写一些快速的单元测试。我有一个异步函数,它在 app.js 中进行一些应用程序设置,它读取大量文件,因此需要一段时间。运行应用程序不是问题,但单元测试是因为当测试运行时,设置尚未完成。

所以我想我会存根异步函数,让它调用一个重新定义的异步函数,该函数在运行回调之前不会完成单元测试设置。

问题是,当重新定义的函数运行时,它通过的应用程序实例尚未完成所有设置,即使测试将应用程序传递给设置。似乎 sinon.callsArgWith 围绕参数设置了一个闭包,以便在函数运行时,它具有具有预设置状态的参数。

我还使用 proxyquire 来确保在测试运行之间获得新的模块负载(使用 noPreserveCache)。

重新定义的函数在options参数中传递,sinon查找并运行它,我认为这是应该的,我阅读文档20次,找不到任何其他方法来运行代码(这是正确使用 sinon 的方法吗?)

express 应用程序 (v4) 具有以下结构:

万维网:

应用程序.js:

;

async_function.js:

选项.js:

测试.js:

helpers.js:

有什么我错过的明显/模糊的东西吗?或者一些我不知道的更简单的方法?

我想会有,只是在一个快速应用程序上运行一些测试似乎需要付出很多努力。

我真的很感激一些帮助,我完全没有想法。

0 投票
1 回答
547 浏览

javascript - 基于节点单元测试事件的异步代码

过去几天我已经发布了一些问题,这些问题太长了(我猜是因为我没有收到任何非神秘的反馈)。我试图把这个简短。

以下代码使用“setup-complete”事件通知 nodeunit setUp 命令运行测试。测试 1 通过,测试 2 失败

失败:未完成的测试(或其设置/拆卸):-基于事件的异步代码-test2

我的代码有错误吗?nodeunit 是测试基于事件的代码的错误选择吗?是我的做法吗?任何建议表示赞赏。谢谢

async_setup.js:

测试/test.js:

0 投票
4 回答
2295 浏览

node.js - 带有咕噜声的Nodeunit找不到模块'tap'

因此,有了本教程,我决定在我的 node.js 应用程序中添加一些单元测试。我的 gruntfile 似乎没问题,当我键入时,grunt nodeunit我唯一的测试运行得很好,但在那之后,它崩溃并出现错误Fatal error: Cannot find module 'tap'

我对这个模块一无所知,但是在谷歌搜索之后,它似乎是 nodeunit 需要的东西。事实上,它确实存在:$MY_NODE_APP/node_modules/nodeunit/node_modules/tap存在,当我启动节点$MY_NODE_APP/node_modules/nodeunitrequire('tap')在交互式控制台中输入时,我得到一些包含很多东西的对象,这给我的印象是它可以正常工作。

所以,显而易见的问题是:为什么我会收到这个错误,我该如何解决?

咕噜文件:

更新:安装tap到我的项目中,但也没有帮助。

0 投票
2 回答
1251 浏览

javascript - 如何在对象的 nodeunit deepEqual 测试中跳过 epoch 字段?

我正在使用nodeunit. 在某些时候,我有一个这样的验证:

我正在验证的对象大致如下所示:

我的测试总是失败,因为timestamp动态创建的对象(testObject在这种情况下)中的字段总是不同的;有没有办法告诉deepEqual方法跳过该单个字段?