Skip to content
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

Merged
merged 5 commits into from
Nov 20, 2018
Merged

Replacing Mocha with Jest #640

merged 5 commits into from
Nov 20, 2018

Conversation

aliuk2012
Copy link
Contributor

@aliuk2012 aliuk2012 commented Nov 16, 2018

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.

Error message:
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

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 from server.js for Jest to work correctly. We now have server.js, start.js and listen-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:

  • Squash last commit once new changes are approved

https://trello.com/c/OYcXcTFd/1559-rewrite-existing-smoke-tests-using-jest-pair

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 16, 2018 16:35 Inactive
@aliuk2012 aliuk2012 added the WIP label Nov 16, 2018
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 16, 2018 17:16 Inactive
@aliuk2012 aliuk2012 removed the WIP label Nov 19, 2018
Copy link

@kr8n3r kr8n3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking change?

app.js Outdated Show resolved Hide resolved
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 19, 2018 12:05 Inactive
Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe

before_install:
- npm install -g mocha
can be cleaned up as well.

__tests__/spec/sanity-checks.js Outdated Show resolved Hide resolved

test('should send with a well formed response for the index page', async () => {
const response = await request(app).get('/')
expect(response.statusCode).toBe(200)
Copy link
Contributor

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?

Copy link
Member

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 👍

Copy link
Contributor

@NickColley NickColley left a 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)

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 19, 2018 14:06 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 19, 2018 15:09 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 19, 2018 16:04 Inactive
Copy link

@kr8n3r kr8n3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good now. 🎉

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 20, 2018 10:14 Inactive
hannalaakso and others added 4 commits November 20, 2018 14:12
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
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-prototype-kit-pr-640 November 20, 2018 14:16 Inactive
@joelanman joelanman mentioned this pull request Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants