The vi.mock('chart.js', ...) MockChart class was copy-pasted verbatim
between chart-canvas.spec.ts and overview.spec.ts. Extract it to
shared/chart-canvas/testing/mock-chart.ts and import it via
vi.hoisted(async () => import(...)) in each spec, since vi.mock's
factory is hoisted above regular imports and can't reference a
plain top-level import.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import { vi } from 'vitest';
|
|
|
|
/**
|
|
* Test double for Chart.js's `Chart` class, shared by `chart-canvas.spec.ts` and
|
|
* `overview.spec.ts`. jsdom has no canvas 2D context, so real Chart.js cannot render
|
|
* in this project's test environment — specs mock the whole `chart.js` module via
|
|
* `vi.mock('chart.js', () => ({ Chart: MockChart, registerables: [] }))` and assert
|
|
* on the Chart.js lifecycle contract (constructor args, update(), destroy()) instead.
|
|
*
|
|
* Not a `*.spec.ts` file on purpose: it exports a class rather than defining tests,
|
|
* so it must not be picked up by the test runner's `**\/*.spec.ts` include glob.
|
|
*/
|
|
export class MockChart {
|
|
static register = vi.fn();
|
|
static instances: MockChart[] = [];
|
|
|
|
data: unknown;
|
|
options: unknown;
|
|
config: { type: unknown; data: unknown; options: unknown };
|
|
destroy = vi.fn();
|
|
update = vi.fn();
|
|
|
|
constructor(
|
|
public ctx: unknown,
|
|
config: { type: unknown; data: unknown; options: unknown },
|
|
) {
|
|
this.config = config;
|
|
this.data = config.data;
|
|
this.options = config.options;
|
|
MockChart.instances.push(this);
|
|
}
|
|
}
|