从单个grunt命令运行app和模拟测试的Gruntfile(Gruntfile to run app and mock test from single grunt command)

编程入门 行业动态 更新时间:2024-10-28 02:32:24
从单个grunt命令运行app和模拟测试的Gruntfile(Gruntfile to run app and mock test from single grunt command)

我有一个Node.js Express REST API应用程序。 好。

我有一个Mocha / Chai / Supertest模拟测试上面的API应用程序。 好。

但我必须启动应用程序,然后独立运行模拟测试。

如何运行启动API应用程序的单个grunt命令,让它启动并运行,然后运行模拟测试?

或者我是否需要在某种测试模式下运行API应用程序(通过env var)并且只测试逻辑以某种方式调用模拟测试?

我可以尝试一些事情并获得一些工作,但有什么好办法? (避免过度使用短语'最佳实践'。)

I have a Node.js Express REST API app that works. Good.

I have a Mocha/Chai/Supertest mock that tests the API app above. Good.

But I have to start the app and then independently run the mock test.

How can I run a single grunt command that starts the API app, let's it get up and going, and then runs the mock test?

Or do I need to run the API app in some kind of test mode (via env var) and have test-only logic somehow invoke the mock test?

I can try some things and get something to work, but what is the good way? (Avoiding overused phrase 'best practice'.)

最满意答案

你可以用grunt-express-server和grunt-mocha-test来做到这一点,你必须像下面那样设置你的任务:

grunt.registerTask('test', ['express:test', 'mochaTest']);

这将使用您为测试环境设置的配置运行您的快速服务器,然后在运行grunt test时运行mocha。

由于您使用的是supertest,我认为您正在进行功能测试,这意味着您将使用相同的数据库进行开发和测试(如果您没有嘲笑某些内容)。 这可能会耗费时间,并且由于数据不佳而导致测试失败。 使用两个不同的环境可确保在运行测试时数据的状态。

如果您不想手动执行此操作,仍然可以使用grunt watch插件重新启动文件更改测试。

希望这可以帮助

You can do that with grunt-express-server and grunt-mocha-test, you will juste have to setup your task like below :

grunt.registerTask('test', ['express:test', 'mochaTest']);

This will run your express server with the config you have set for the test environement then run mocha when you run grunt test.

Since you are using supertest I suppose you are doing functionnal testing which means that you will be using the same database for developement and testing (if you are not mocking something). That can be time loosing and make your test fail because of bad data. Using two different environement makes sure of the state of your data when you are running the test.

You can still use grunt watch plugins to relaunch your test on file change if you don't want to have to do it manually.

Hope this helps

更多推荐

本文发布于:2023-08-03 07:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   测试   Gruntfile   grunt   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!