Skip to content

test: run the image fixtures under Vitest instead of Karma - #12444

Merged
kurkle merged 7 commits into
chartjs:masterfrom
kurkle:migrate-tests-to-vitest-p2-fixtures-under-vitest
Nov 25, 2026
Merged

kurkle merged 7 commits into
chartjs:masterfrom
kurkle:migrate-tests-to-vitest-p2-fixtures-under-vitest

Conversation

@kurkle

@kurkle kurkle commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Piece P2 of the migrate-tests-to-vitest design (openspec/changes/migrate-tests-to-vitest/, tasks section 2).

What was missing

The 667 image fixtures ran only under Karma, from a describe('auto', jasmine.fixture.specs('<dir>')) line in each of 25 spec files. Vitest could not load them: every .js fixture is CommonJS, and Vite serves source files as ES modules. test/fixtures/scale.timeseries had no line of its own; it ran only because the line for scale.time matched its path by prefix.

What changed

  • test/specs/fixtures.spec.js globs every fixture config and PNG and registers one describe per top-level fixture directory, using createFixtures from chartjs-test-utils 1.x. A new fixture or directory runs without a spec line, and scale.timeseries gets its own group.
  • test/vitest.shared.js exports commonjsFixtures(), a Vite plugin that wraps each test/fixtures/**/*.js containing module.exports as an ES module with a default export. Fixtures stay CommonJS, including those in open pull requests. vitest.config.js uses the plugin.
  • test/index.js: jasmine.fixture.specs is now a stub. Each remaining describe('auto', ...) line in a Karma spec registers one placeholder (runs under Vitest (test/specs/fixtures.spec.js)), so each fixture has exactly one runner.
  • Update mode: pnpm run fixtures:update [-t <dir or fixture>] sets UPDATE_FIXTURES=1. The config then registers the saveFixtureImage command (createSaveFixtureImage() from chartjs-test-utils/node) and runs Chromium only. It writes only PNGs that are missing or differ, and prints each file it writes. A normal run writes nothing: git status after the full run below showed no PNG.
  • moment is pre-bundled (optimizeDeps.include):
    • Before, moment-timezone's pre-bundle carried its own copy of moment, and the excluded chartjs-adapter-moment imported the raw moment package.
    • The adapter therefore formatted dates in the machine's time zone, not in Etc/UTC. On a UTC+2 machine, scale.time/labels-date was shifted two hours (4.61% different).
    • A new test in test/specs/vitest.setup.spec.js pins this: the adapter formats in the default zone of moment-timezone. It sets the zone to Etc/GMT-5, so it fails on any machine, including CI's UTC clock, where no fixture would notice.
  • Four fixtures call the triggerMouseEvent global instead of jasmine.triggerMouseEvent:
    • controller.doughnut/event-replay.js
    • core.interaction/drawActiveElementsOnTop-false.js
    • core.interaction/nearest-partial-bar.js
    • core.interaction/nearest-point-behind-scale.js
  • scale.linear/grace/grace-10%.{js,png} → grace-10-percent.{js,png} (image unchanged). Vite puts the % raw into the import URL, and its server rejects the request ("Malformed URI sequence in request URL"), so the whole fixture spec failed to load. Vite 8.3.8, the latest release, still does this.
  • test/.eslintrc.yml declares triggerMouseEvent for fixtures/**/*.js. Without it the four fixtures above fail no-undef. This file is outside the design's file list for this piece; the design did not anticipate the lint rule.
  • test/setup.js links the library issue behind its registration workaround: Chart.register() registers nothing from an ES module namespace object #12437
  • Docs: docs/developers/contributing.md has the new image-test recipe (fixtures:update, then look at the PNG), filtering fixtures with -t, and what "debug": true does now: the test always fails and logs the actual, expected and diff images to the browser console.
  • Design: "Notes from building P2" in design.md records the rename, the lint override, the moment fix, the pixelmatch 8 decision and the OffscreenCanvas fix in chartjs-test-utils 1.1.2. Every box of section 2 is ticked in tasks.md.

Failing first

vitest run --browser.headless --project chromium test/specs/fixtures.spec.js with only the spec added (first commit):

Malformed URI sequence in request URL: /test/fixtures/scale.linear/grace/grace-10%.js
 FAIL  |chromium| test/specs/fixtures.spec.js
Caused by: TypeError: Failed to fetch dynamically imported module
      Tests  no tests

With that pair excluded, the CommonJS form still stopped the whole file:

Caused by: SyntaxError: The requested module '/test/fixtures/controller.bar/aligned-pixels.js' does not provide an export named 'default'
      Tests  no tests

The new setup test without the moment pre-bundle, on a UTC+2 machine:

AssertionError: expected '14' to be '17'

Counts

Chromium / Chrome Firefox
Karma tests before 1744 (667 fixtures) 1744 (667 fixtures)
Karma tests after 1102 (0 fixtures, 25 placeholders) 1102 (0 fixtures, 25 placeholders)
Vitest fixture tests 667 667
Vitest tests in total 682 682
  • 667 fixture configs on disk (527 .js, 140 .json), each with its PNG.
  • The 667 fixture names Vitest registers in each browser are exactly the set Karma ran, with no fixture missing or extra. Karma named them /base/test/fixtures/<path>.js; Vitest names them <path>.

Image results

The verdict on the images comes from this pull request's CI run on Ubuntu. The local results below come from macOS 26.6 and are for comparison only. On that machine Karma itself fails some fixtures on master.

Karma on master, locally:

Fixture Chrome Firefox
scale.radialLinear/ticks-below-zero 0.11% pass
scale.timeseries/data-timestamps 0.45% fail
scale.time/ticks-round 0.11% fail
plugin.filler/line/drawTimeFillFalse/beforeDatasetDraw 0.46% fail
plugin.filler/line/drawTimeFillFalse/beforeDraw 0.46% fail
plugin.filler/line/drawTimeFillFalse/beforeDatasetsDraw 0.46% fail
plugin.filler/line/above-below-vertical-linechart 0.32% fail

Vitest on this branch, locally, before the three fixture updates below (Chromium 10 failures, Firefox 11):

Fixture Chromium Firefox Fails under local Karma Text (spriteText)
controller.doughnut/doughnut-circumference-over-2pi 1306px / 0.5% 1306px / 0.5% no no
controller.doughnut/single-slice-opacity pass 71px / 0.11% no no
controller.line/point-style-offscreen-canvas not compared not compared no no
plugin.filler/line/above-below-vertical-linechart 817px / 0.31% 301px / 0.11% yes no
plugin.filler/line/drawTimeFillFalse/beforeDatasetDraw 1207px / 0.46% 1116px / 0.43% yes no
plugin.filler/line/drawTimeFillFalse/beforeDatasetsDraw 1207px / 0.46% 1116px / 0.43% yes no
plugin.filler/line/drawTimeFillFalse/beforeDraw 1207px / 0.46% 1116px / 0.43% yes no
scale.linear/min-max-skip/no-collision pass 44px / 0.11% no yes
scale.radialLinear/ticks-below-zero 289px / 0.11% pass yes (Chrome) no
scale.time/labels-strings 425px / 0.21% 472px / 0.24% no yes
scale.time/ticks-round 205px / 0.16% 289px / 0.22% yes yes
scale.timeseries/data-timestamps 1180px / 0.45% 1302px / 0.5% yes yes

On Ubuntu CI, three fixtures failed. Karma passes them, and the cause is the comparer, not the rendering. chartjs-test-utils 1.x compares with pixelmatch 8, which measures colour difference in OKLab where pixelmatch 5 (Karma's) used YIQ, so a fixture's threshold now admits a different difference. On the same Vitest render, pixelmatch 5 finds 0 differing pixels in doughnut-circumference-over-2pi and pixelmatch 8 finds 1306. Source and built bundle render that config identically, and headed browsers give the same result.

Pixelmatch 8 stays, and the references are updated where needed:

Fixture CI result Change
controller.doughnut/doughnut-circumference-over-2pi 1306px / 0.5% in both browsers Reference regenerated from Chromium with pnpm run fixtures:update. The new image differs from the old one only in the border where the 400° arc starts and overlaps itself.
controller.doughnut/single-slice-opacity Firefox only, 71px against a limit of 65 (256x256) tolerance 0.001 (default) → 0.0012 (limit 78). Its Chromium render already matches, so regenerating writes nothing.
scale.linear/min-max-skip/no-collision Firefox only, 41px against a limit of 40 (200x200; 44 locally) tolerance 0.001 (default) → 0.0012 (limit 48). Same reason.

All three now pass locally in Chromium and Firefox. The other local failures above also fail under local Karma, or are text-bearing local noise; CI on Ubuntu does not show them.

OffscreenCanvas fixture

controller.line/point-style-offscreen-canvas failed in both browsers with "Input value is not a valid image source.": chartjs-test-utils 1.1.1 accepted only a CanvasRenderingContext2D in its image matcher, and a chart drawn on an OffscreenCanvas has an OffscreenCanvasRenderingContext2D. This pull request takes chartjs-test-utils ^1.1.2 (chartjs/chartjs-test-utils#45), which accepts it; the fixture now passes against its existing reference image in Chromium and Firefox. The chartjs-test-utils-karma alias stays on 0.4.

Try it

pnpm exec playwright install chromium firefox   # once
pnpm run test-ci-vitest fixtures                # every fixture, both browsers
pnpm run dev:vitest fixtures -t "controller\.bar/"  # one directory, Chromium, watch mode
pnpm run fixtures:update -t "scale\.time/labels-date$"   # rewrites the PNG only if it differs

@netlify

netlify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit 0be7e99
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b06f79eb3ea22eea9ca1b89
😎 Deploy Preview https://deploy-preview-12444--chartjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Size Change: 0 B

Total Size: 363 kB

ℹ️ View Unchanged
Filename Size
dist/chart.cjs 81.9 kB
dist/chart.js 82.2 kB
dist/chart.umd.js 72.4 kB
dist/chart.umd.min.js 72.4 kB
dist/chunks/helpers.dataset.cjs 25.8 kB
dist/chunks/helpers.dataset.js 25.6 kB
dist/helpers.cjs 1.52 kB
dist/helpers.js 1.33 kB

compressed-size-action

The CommonJS fixtures do not load under Vitest yet.
Load the CommonJS fixtures through a Vite transform in
test/vitest.shared.js, and stub jasmine.fixture.specs in test/index.js
so that each fixture has one runner. Add `pnpm run fixtures:update`,
which writes missing or changed reference images from Chromium.

Pre-bundle moment: otherwise the date adapter used its own copy and
formatted dates in the machine's time zone instead of Etc/UTC.

Rename the grace-10% fixture, whose name Vite cannot serve, and call
the triggerMouseEvent global in the four fixtures that used Jasmine's.
`-t scale.time` also runs scale.timeseries. Show the slash that selects
one directory and the `$` that selects one fixture.
Jasmine warned three times per placeholder that it had no expectations.
The design assumed only the blending mattered; the fix goes to
chartjs-test-utils.
Regenerate doughnut-circumference-over-2pi from Chromium. Raise the
tolerance of single-slice-opacity and min-max-skip/no-collision from
0.001 to 0.0012: they differ only in Firefox, where they measure just
over 0.1%, and their Chromium render already matches.
1.1.1 rejected the OffscreenCanvasRenderingContext2D of a chart drawn
on an OffscreenCanvas, so controller.line/point-style-offscreen-canvas
failed before any comparison. 1.1.2 accepts it, and the fixture passes
against its existing reference image in Chromium and Firefox.
@kurkle
kurkle force-pushed the migrate-tests-to-vitest-p2-fixtures-under-vitest branch from be237e8 to 0be7e99 Compare November 25, 2026 14:58
@kurkle
kurkle marked this pull request as ready for review November 25, 2026 15:10
@kurkle

kurkle commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@etimberg the karma coverage will be red for these till all is converted, acceptable?

@etimberg

Copy link
Copy Markdown
Member

@etimberg the karma coverage will be red for these till all is converted, acceptable?

yup, all good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants