-
Notifications
You must be signed in to change notification settings - Fork 21
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
PLATUI-3224 update jest #403
Conversation
|
||
const { toHaveNoViolations } = require('jest-axe'); | ||
|
||
expect.extend(toHaveNoViolations); |
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.
didn't seem to be needed
"browser": true, | ||
"context": true, | ||
"puppeteerConfig": true, | ||
"jestPuppeteer": true |
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.
stops ide warnings about globals
'**/*browser.test.js', | ||
], | ||
}, | ||
], |
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.
previously the environment of a test was configured by comments at the top of test files, there were some limits of what custom options you could specify that way so instead I've opted to rename tests with different suffixes. This matches the approach that govuk-frontend uses too https://github.com/alphagov/govuk-frontend/blob/main/jest.config.mjs
}, | ||
], | ||
|
||
modulePathIgnorePatterns: ['<rootDir>/package'], |
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.
stops us getting a warning from jest haste map
window.clock = sinon.useFakeTimers(${JSON.stringify(config)}) | ||
} | ||
`); | ||
} |
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.
we were using sinon before - but a part of it only, and to do that we had to compile the extracted part from the browser, I discovered that if we just use the whole sinon library, it's npm package has a compiled js file ready to use in the browser, so no more need for that extra build step in our tests
req.respond({ contentType: 'text/html', body }); | ||
page.setRequestInterception(false); | ||
}); | ||
await page.goto('http://localhost:3000/'); // if ever flaky, waitUntil networkidle0 (js loaded) |
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.
some of the tidying up needed is to replace anywhere I've used hardcoded port with the port from config
expect(BackLinkHelper.prototype.init).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
}); |
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.
this replaces src/all.test.js it's the same thing renamed and refactored but changed enough so that it's not picked up as a rename
"testRunner": "jest-jasmine2", | ||
"globalSetup": "./lib/puppeteer/setup.js", | ||
"globalTeardown": "./lib/puppeteer/teardown.js" | ||
} |
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.
now configured in jest.config.mjs
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 think something is up with these currency component snapshots- doesn't seem to be actually rendering the component anymore - can't remember if that's because of changes to currency input which need to be reflected in tests (do we need them anymore? future tech debt ticket to be created)
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 have left some tests here that were checking things like argument validation and message overrides - but moved the behavioural stuff into browser tests
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.
this is where I extracted lots of timeout dialog tests into
await session.close(); | ||
}); | ||
|
||
// TODO because currently it would actually log you out straight away, this is an edge case |
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.
might be an edge case that in practice can't happen (I don't know if session activity could be delayed like this, so left this as a skipped test to show how it currently behaves)
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.
another one where it already existed but renamed and refactored enough that it thinks it's new file
const configPaths = require('../../../config/paths.json'); | ||
|
||
describe('/components/user-research-banner', () => { | ||
let page; | ||
const url = `http://localhost:${configPaths.ports.test}/components/user-research-banner/default/preview`; | ||
const url = `http://localhost:${configPaths.ports.app}/components/user-research-banner/default/preview`; |
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 didn't think we needed a separate test port - we can just reuse the app one - and the jest can skip spinning up it's own dev server if ones already running
@@ -28,10 +17,6 @@ describe('/components/user-research-banner', () => { | |||
await page.deleteCookie({ name: 'mdtpurr' }); | |||
}); | |||
|
|||
afterAll(async () => { | |||
await page.close(); | |||
}); |
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.
in the browser-test jest-setup it resets the page before each test now to save stuff like this being duplicated around
2a69040
to
ab96a08
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.
wanted a new example for some tests with specific timeout length, could have refactored it to not use the examples and just render html into the test - which would be better but 🤷♂️
await clockTick(page, s * 1000); | ||
} | ||
|
||
const preloadGovukFonts = fs.readdirSync( |
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.
Is this so that the test is not slowed / flaky due to slow font loading?
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.
yup, for some of the timeout dialog tests there isn't any text rendered using the gov fonts on page load, only when the dialog triggers, and I guess css fonts are deferred by default so that's when the request is made to load them. Some of the timeout dialog tests intercept timeout / sign-out request to simulate a slow response and it was triggering on the fonts, so I copied over the preload code from the example app that's used for visual regression tests where we were getting some screenshots without fonts
<link rel="preload" href="/assets/govuk/fonts/{{font}}" as="font" type="font/woff2" crossorigin> |
await expect(page.url()).toMatch('/signed-out-early'); | ||
}); | ||
|
||
// TODO at the moment it doesn't, should we change test or implementation? |
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.
This is an interesting one, worth capturing somewhere?
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.
yup, could probably be a tech debt ticket, I'll create one and link it here
currently browser tests are disabled moving to use jest-puppeteer add a browser test as an example refactor timeout dialog tests fix after webjar build test
cafa2d0
to
d0c7bcd
Compare
d0c7bcd
to
c585e5d
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.
I don't think we need VRT of the examples for this - just one example with the correct attribute set is enough
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.
was actually renamed and refactored, see above file
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.
refactored the vrt examples into the browser tests, behaviour is a bit clearer like this I think, and with the examples we get extra screenshots being taken that we don't need
86ee4ae
to
a8d67fe
Compare
update jest, refactored some test config as a result, had some broken tests with the timeout dialog which were hard to unpick/isolate with jsdom so I've refactored a lot of them into the browser-based tests instead, seems like we don't need --runInBand (run tests in sequence) so they should run faster now, but we'll see if that holds up in jenkins.
most of this is moving around test coverage and trying to simplify test code so it's easier to maintain in future 🤞