我有一个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
更多推荐
发布评论