I am creating a docker image/container for testing purpose from my productive build application (nodeJS app). Now I want to do some e2e testing using mocha/chai and nightmareJS. So I have created a very basic test file.
My problem is now how to test for the running application. So I want to 'load' the application like
- goto http://localhost
- check if login form is existing
- do login
- check if login was successful
I don't know how to do this in my docker image / e2e.js-file...
This is how I'm creating the docker image:
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"]
And this is how my basic e2e.js file looks like:
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
})
})
I'm not really sure by looking at your Dockerfile
, but by looking at your comment
Override the command, to run the test instead of the application
You'll still need to launch the website in itself after having installed it's dependencies, and then mocha. Since docker is only intended to run one process at a time you might want to look at supervisord, but you can also put your website as a background process and then launch your test.
Using an entrypoint to a bash script instead of your CMD
:
ENTRYPOINT ["./bin/run.sh"]
And the script in itself would be something like this:
#!/bin/bash
npm start & mocha e2e.js --reporter spec
As for the test in itself, you can do something like this to test your login flow. It's only an example, you might need to tweak a few things, like the elements selectors and some other stuff but it's still a good starting point.
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)
})
})