When writing tests, ensure assertions can handle non-deterministic content while providing clear failure context: use targeted assertions for non-deterministic content, use specialized tools like cheerio for DOM testing, and include contextual information in diagnostic tests.
When writing tests, ensure assertions can handle non-deterministic content while providing clear failure context:
toContain()
, toInclude()
) instead of full snapshots// GOOD
// rspack returns error content that contains absolute paths which are non deterministic
await session.assertHasRedbox();
expect(redboxContent).toContain("Module not found: Can't resolve 'dns'");
expect(redboxLabel).toContain('Build Error');
// AVOID
await expect(browser).toDisplayRedbox(`
"error": {
"message": "[absolute path that will change]",
}
`);
// GOOD
const $ = await next.render$('/');
expect($('#server-value').text()).toBe('Server value: foobar');
// AVOID
const html = (await res.text()).replaceAll(/<!-- -->/g, '');
expect(html).toContain('Server value: foobar');
// GOOD
expect(diagnosedFiles).toEqual({
'page.tsx': { code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT, /*...*/ },
'layout.tsx': { code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT, /*...*/ }
});
// AVOID
for (const tsFile of tsFiles) {
const diagnostics = languageService.getSemanticDiagnostics(tsFile);
expect(diagnostics.length).toBe(1);
// No context about which file failed if assertion fails
}
Enter the URL of a public GitHub repository