-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing Mocha with Jest #640
Conversation
1e6d8c3
to
d35733a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
govuk-prototype-kit/.travis.yml
Lines 6 to 7 in 48ad1c3
before_install: | |
- npm install -g mocha |
__tests__/spec/sanity-checks.js
Outdated
|
||
test('should send with a well formed response for the index page', async () => { | ||
const response = await request(app).get('/') | ||
expect(response.statusCode).toBe(200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we aim for one expectation per test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good shout, this has now been addressed 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really sensible and tidy, nice work 👍
(Agree with Ollie's points)
73b34ca
to
584c511
Compare
584c511
to
b5fbb5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good now. 🎉
After running jest-codemon
I was able to get rid of the gulp task and just call it straight from `npm test`.
This listener which looks out for file changes using BrowserSync doesn't need to be part of `server.js` as it's only needed for npm start. We added a new file called `listen-on-port.js` - this needs to be separate from `server.js` for Jest to work correctly. More detail here: http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/#2-Separate-your-app-and-sever
523ca90
to
0ccbc4a
Compare
Co-authored with @hannalaakso
We wanted to ensure that testing frameworks were consistent between the various repos that we supported so this removes Mocha and moved the tests over to Jest.
We experienced a lot of issues replacing mocha because server.js had a lot of complicated code and it wasn’t clear what was happening. This commit starts to address this issue by splitting the server and the app into separate files (server.js and app.js) As mentioned in this blog http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/. There is no need to add listeners to the app for testing and seemed to be a best practice.
Another issue we had, At one point the tests were passing but Jest was not exiting and instead threw this error and kept running until we manually killed the process.
The issue here was that we had nunjucks (middleware) configured incorrectly. It was setup to continuously watch for file changes in the view (even in production). We resolved this by adding some logic which dynamically created an object for nunchucks configuration. This object conditionally set the watch flag to true depending on whether it was running in development.
While cleaning up mocha we were able to get rid of the gulp task and just call it straight from
npm test
.Edit: We added a new file called
listen-on-port.js
which adds a listener for the server - this needs to be separate fromserver.js
for Jest to work correctly. We now haveserver.js
,start.js
andlisten-on-port.js
- we can improve the naming once we do more major refactoring of the code, we didn't want to rename existing files as this could be considered a breaking change.Note: it's probably helpful to review this commit by commit.
To do:
https://trello.com/c/OYcXcTFd/1559-rewrite-existing-smoke-tests-using-jest-pair