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

PLATUI-3224 update jest #403

Merged
merged 3 commits into from
Sep 24, 2024
Merged

PLATUI-3224 update jest #403

merged 3 commits into from
Sep 24, 2024

Conversation

oscarduignan
Copy link
Contributor

@oscarduignan oscarduignan commented Sep 13, 2024

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.

  • I think I've still got a bit of tidying up to do before this is totally ready for review.

most of this is moving around test coverage and trying to simplify test code so it's easier to maintain in future 🤞


const { toHaveNoViolations } = require('jest-axe');

expect.extend(toHaveNoViolations);
Copy link
Contributor Author

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
Copy link
Contributor Author

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',
],
},
],
Copy link
Contributor Author

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'],
Copy link
Contributor Author

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)})
}
`);
}
Copy link
Contributor Author

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)
Copy link
Contributor Author

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);
});
});
});
Copy link
Contributor Author

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"
}
Copy link
Contributor Author

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

Copy link
Contributor Author

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)

Copy link
Contributor Author

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

Copy link
Contributor Author

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
Copy link
Contributor Author

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)

Copy link
Contributor Author

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`;
Copy link
Contributor Author

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();
});
Copy link
Contributor Author

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

tasks/gulp/backstop.js Outdated Show resolved Hide resolved
Copy link
Contributor Author

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(
Copy link
Contributor

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?

Copy link
Contributor Author

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?
Copy link
Contributor

@JoPintoPaul JoPintoPaul Sep 16, 2024

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?

Copy link
Contributor Author

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
@oscarduignan oscarduignan force-pushed the jest-update branch 2 times, most recently from cafa2d0 to d0c7bcd Compare September 17, 2024 15:13
Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@oscarduignan oscarduignan marked this pull request as ready for review September 17, 2024 15:28
@oscarduignan oscarduignan merged commit f064b96 into main Sep 24, 2024
1 check passed
@oscarduignan oscarduignan deleted the jest-update branch September 24, 2024 10:31
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.

2 participants