-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce precision of
oklab()
arguments in test snapshots (#15210)
After the changes in #15201, our Windows CI started to fail. The problem is that lightningcss now needs to convert `oklch` colors into the `oklab` space to inline some `color-mix()` functions. The problem, though, is that this calculation seems to have rounding differences between macOS, Linux, and Windows. Since we still want to _define the default color space in `oklch`_ and _use lightningcss as a post-processor in our unit tests so we have a better coverage of the output_, this PR attempts to fix the issue by adding a custom vitest serializer. It will find usages of the `oklab()` function with arguments that have lots of decimal places (at least 6 decimal places). What it then does is simply cut off any excess decimal places to truncate the output to 5 places. E.g.: ```diff - oklab(62.7955% .224863 .125846 / .75); + oklab(62.7955% .22486 .12584 / .75); ``` ## Test Plan I updated the CI workflow file to make all three builds run in CI and observed that they are now all green again. <img width="609" alt="Screenshot 2024-11-27 at 14 54 52" src="https://github.com/user-attachments/assets/73fe6da5-30e3-4fd4-83ea-115b1f1602a6">
- Loading branch information
1 parent
6aac3cb
commit bfcc144
Showing
7 changed files
with
66 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { SnapshotSerializer } from 'vitest' | ||
|
||
// We're reducing the precision of parameters to the `oklab()` function from | ||
// our snapshots as we've observed lightningcss generating different decimal | ||
// places in the last position when run on different operating systems. | ||
const HIGH_PRECISION_OKLAB = /oklab\(\d{1,2}\.?\d{0,4}% -?\.(\d{6,8}) -?\.(\d{6,8}) \/ \.\d{1,2}\)/g | ||
|
||
export default { | ||
test(val) { | ||
return typeof val === 'string' && val.match(HIGH_PRECISION_OKLAB) !== null | ||
}, | ||
serialize(val, config, indentation, depth, refs, printer) { | ||
if (typeof val !== 'string') { | ||
throw new Error('This was already tested in the test() callback') | ||
} | ||
|
||
let replaced = val.replaceAll(HIGH_PRECISION_OKLAB, (match, first, second) => { | ||
return match.replaceAll(first, first.slice(0, 5)).replaceAll(second, second.slice(0, 5)) | ||
}) | ||
return printer(replaced, config, indentation, depth, refs) | ||
}, | ||
} satisfies SnapshotSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
const __dirname = fileURLToPath(new URL('.', import.meta.url)) | ||
|
||
export default defineConfig({ | ||
test: { | ||
snapshotSerializers: [path.resolve(__dirname, 'src/test-utils/custom-serializer.ts')], | ||
exclude: ['**/*.spec.?(c|m)[jt]s?(x)', 'integrations/**/*'], | ||
}, | ||
}) |