next.js
8a801847 - test: refactor unclear tests to composed matchers (#68674)

Commit
1 year ago
test: refactor unclear tests to composed matchers (#68674) ### What Improve the test matcher for string matching while searching in array case, those flaky tests were giving unclear failing messages without enough context to help investigate the flakeness. ### Why Example of existing unclear flaky failure ``` pnpm test test/integration/link-ref/test/index.test.js • Invalid hrefs > production mode > should preload with child ref with React.createRef Expand output ● Invalid hrefs › production mode › should preload with child ref with React.createRef expect(received).toBe(expected) // [Object.is](http://object.is/) equality Expected: true Received: false ``` While looking at the test, assertions like `expect(found).toBe(true)` could fail without telling us what are the context, what is the original input before narrowing down as a boolean value. After changing to a composed testing matcher, we're able to see what href links are giving us the unexpected result ```ts expect(hrefs).toEqual( expect.arrayContaining([expect.stringContaining('index')]) ) ``` For example, if we're looking for a string pattern `first-1` in the link hrefs, but it only got `first`, the failure message would look like this. This will help us identify the root cause easier with more context. ``` ● Prefetching Links in viewport › production mode › should prefetch with link in viewport onload expect(received).toEqual(expected) // deep equality Expected: ArrayContaining [StringContaining "first1"] Received: ["/_next/static/chunks/pages/first-c9300691290bda9a.js"] 109 | links.map(async (link) => await link.getAttribute('href')) 110 | ) > 111 | expect(hrefs).toEqual( | ^ 112 | expect.arrayContaining([expect.stringContaining('first1')]) ```
Author
Parents
Loading