NodeJS:如何做docker应用程序的nightmareJS e2etesting

我正在创build一个Docker镜像/容器,用于从我的高效构build应用程序(nodeJS应用程序)进行testing。 现在我想用摩卡/柴噩梦做一些e2etesting。 所以我创build了一个非常基本的testing文件。

我的问题是现在如何testing正在运行的应用程序。 所以我想“加载”应用程序

- goto http://localhost - check if login form is existing - do login - check if login was successful 

我不知道如何在我的docker image / e2e.js文件中做到这一点…

这是我如何创builddocker图像:

Dockerfile

 # Use the production image as base image FROM productive_build:latest # Copy the test files COPY e2e.js / # Override the NODE_ENV environment variable to 'dev', in order to get required test packages ENV NODE_ENV dev # 1. Get test packages; AND # 2. Install our test framework - mocha RUN (cd programs/server && npm install) RUN npm install -g mocha RUN npm install chai nightmare # Override the command, to run the test instead of the application CMD ["mocha", "e2e.js", "--reporter", "spec"] 

这就是我的基本e2e.js文件的样子:

e2e.js

 var Nightmare = require('nightmare'), expect = require('chai').expect describe('test', function () { it('should always be true', function () { var nightmare = Nightmare() nightmare.end() expect(true).to.be.true }) }) 

我不确定通过查看您的Dockerfile ,但通过查看您的评论

覆盖该命令,运行testing而不是应用程序

在安装依赖关系之后,您仍然需要自行启动网站,然后再进行摩卡。 由于docker只是一次运行一个进程,你可能想看看supervisord ,但你也可以把你的网站作为后台进程,然后启动你的testing。

使用一个入口点而不是你的CMD的bash脚本:

 ENTRYPOINT ["./bin/run.sh"] 

脚本本身就是这样的:

 #!/bin/bash npm start & mocha e2e.js --reporter spec 

至于testing本身,你可以做这样的事情来testing你的loginstream量。 这只是一个例子,你可能需要调整一些东西,比如元素select器和其他一些东西,但它仍然是一个很好的起点。

 var Nightmare = require('nightmare') var expect = require('chai').expect describe('test login', function () { it('should login and display my username', function (done) { var nightmare = Nightmare({ show: false }) nightmare .goto('http://localhost:8000') // your local url with the correct port .type('#login', 'your login') // change value of the login input .type('#password', 'your password') .click('#submit') // click the submit button .wait('#my-username') // wait for this element to appear (page loaded) .evaluate(function () { // query something once logged on the page return document.querySelector('#my-username').innerText }) .end() .then(function (result) { // the result will be the thing you grabbed from the window in the evaluate expect(result).to.equal('user3142695') done() }) .catch(done) }) }) 

限定词:我不知道nightmarejs或所有的依赖

你需要一个node.js的监控工具。 nodemon是一个不错的select,Docker有一个关于使用nodemonVisual Studio Code来debuggingnode.js应用程序的教程。

这里的教程: https : //github.com/docker/labs/blob/master/developer-tools/nodejs-debugging/VSCode-README.md