node.js - 如何让 grunt.js 启动 express 应用程序进行测试

标签 node.js unit-testing gruntjs

我目前的情况是,我使用 grunt 制作我的 express 应用程序的生产版本(最小化并合并所有 js/css,将所有文件复制到位),然后我必须运行一个设置环境变量的脚本(我的应用程序只在测试模式下运行时提供测试工具),创建一个空的 Mongo 测试数据库,然后在应用程序目录上调用 npm start,然后我必须手动从 Chrome 或 Phantom 运行测试,这是我想要的要做的是让 grunt 设置环境变量并运行服务器,运行测试,然后停止服务器(如果将来一切都成功的话,也可以部署它会很好)。但是,当我尝试以咕噜声运行该应用程序时,它会在完成后立即停止。

我如何用 grunt 启动应用程序,等待它启动然后运行测试?

最佳答案

如果你勾选grunt-express这是一个通过 grunt 进行快速网络服务器任务的插件。

express-keepalive

Note that this server only runs as long as grunt is running. Once grunt's tasks have completed, the web server stops. This behavior can be changed by appending a express-keepalive task at the end of your task list like so

grunt.registerTask('myServer', ['express', 'express-keepalive']);
Now when you run grunt myServer, your express server will be kept alive until you manually terminate it.

Such feature can also be enabled ad-hoc by running the task like grunt express express-keepalive.

This design gives you the flexibility to use grunt-express in conjunction with another task that is run immediately afterwards, like the grunt-contrib-qunit plugin qunit task. If we force express task to be always async, such use case can no longer happen.

来自指南 grunt-contrib-qunit 包用于在 headless PhantomJS 实例中运行 QUnit 单元测试。还要注意最后一行,如果您强制 express 始终异步,那将毫无用处。

npm link对于 grunt-express

关于node.js - 如何让 grunt.js 启动 express 应用程序进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15857585/

相关文章:

node.js - 有没有像PHP Smarty一样支持模板继承功能的nodejs模板引擎?

node.js - Nodejs 集群和 expressjs session

css - Angular - 用户界面网格 - 字体

javascript - grunt 将散列附加到 dist/files

javascript - 如何在版本更改时重新加载资源(HTML/CSS/JS)

node.js - Nodejs - 依赖项(非常基本的) - Express -> 它是 NodeJS 或插件的一部分

mysql - 序列化查询以获取不在关联中的记录

php - 单元测试调用具体子方法的抽象类方法

c# - 如何让AutoFixture从多个TypeRelay中随机选择?

java - 单元测试冰面