Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(image-utils): replace jest with vitest #5711

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/image-utils/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin
build
coverage
jest.config.js
vitest.config.mts
13 changes: 0 additions & 13 deletions libs/image-utils/jest.config.js

This file was deleted.

14 changes: 5 additions & 9 deletions libs/image-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"lint": "pnpm type-check && eslint .",
"lint:fix": "pnpm type-check && eslint . --fix",
"test": "is-ci test:ci test:watch",
"test:ci": "jest --coverage --ci --reporters=default --reporters=jest-junit --maxWorkers=6",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch"
"test:ci": "vitest run --coverage",
"test:coverage": "vitest --coverage",
"test:watch": "vitest"
},
"packageManager": "[email protected]",
"dependencies": {
Expand All @@ -32,23 +32,19 @@
"tmp": "^0.2.1"
},
"devDependencies": {
"@jest/types": "^29.6.1",
"@types/debug": "4.1.8",
"@types/jest": "^29.5.3",
"@types/jest-image-snapshot": "^6.4.0",
"@types/node": "20.16.0",
"@types/pdfjs-dist": "2.1.3",
"@types/pixelmatch": "^5.2.6",
"@types/tmp": "0.2.4",
"@vitest/coverage-istanbul": "^2.1.8",
"esbuild": "0.21.2",
"esbuild-runner": "2.2.2",
"eslint-plugin-vx": "workspace:*",
"fast-check": "2.23.2",
"is-ci-cli": "2.2.0",
"jest": "^29.6.2",
"jest-junit": "^16.0.0",
"jest-watch-typeahead": "^2.2.2",
"lint-staged": "11.0.0",
"ts-jest": "29.1.1"
"vitest": "^2.1.8"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect, test } from 'vitest';
import { Rect } from '@votingworks/types';
import { createImageData, ImageData } from 'canvas';
import fc from 'fast-check';
import { arbitraryImageData, arbitraryRect } from '../test/arbitraries';
import { crop } from './crop';
import { int } from './types';
import { RGBA_CHANNEL_COUNT } from '.';
import { arbitraryImageData, arbitraryRect } from '../test/arbitraries.mjs';
import { crop } from './crop.js';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the imports changed to have an extension? And why are the files changed to .mts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed test-utils-vitest to use type: module in package.json because the files would be transpiled to CommonJS and we'd get a require('vitest') in the resulting build. Here is the file you get if you require('vitest'):

// vitest/index.cjs
throw new Error(
  'Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.'
  + '\n\nIf you are using "import" in your source code, then it\'s possible it was bundled into require() automatically by your bundler. '
  + 'In that case, do not bundle CommonJS output since it will never work with Vitest, or use dynamic import() which is available in all CommonJS modules.',
)

So now test-utils-vitest/build/index.js is ESM. If you then depend on test-utils-vitest and try to import it in a CJS test file, you get this:

Error [ERR_REQUIRE_ESM]: require() of ES Module /media/parallels/WorkingFiles/code/vxsuite/libs/test-utils-vitest/build/index.js not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.

Since I don't want to change the packages themselves to be ESM yet, I change the test files to be treated as ESM by renaming them to .mts. This makes everything work out. Once we're using ESM, though, we need to import relative files including their target extension. So we add the .js extension to the imports from the .mts files.

import { int } from './types.js';
import { RGBA_CHANNEL_COUNT } from './index.js';

/**
* A slow-but-accurate implementation of `crop` to compare against.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect, test } from 'vitest';
import { Buffer } from 'node:buffer';
import { ImageData, createImageData } from 'canvas';
import fc from 'fast-check';
import { writeFile } from 'node:fs/promises';
import { fileSync } from 'tmp';
import { randomFillSync } from 'node:crypto';
import { MaybePromise } from '@votingworks/basics';
import { arbitraryImageData } from '../test/arbitraries';
import { arbitraryImageData } from '../test/arbitraries.mjs';
import {
RGBA_CHANNEL_COUNT,
encodeImageData,
Expand All @@ -17,7 +18,7 @@ import {
toDataUrl,
toImageBuffer,
writeImageData,
} from './image_data';
} from './image_data.js';

test('channels', () => {
const rgbaImage = createImageData(1, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect, test } from 'vitest';
import { sampleBallotImages } from '@votingworks/fixtures';
import { createImageData } from 'canvas';
import { basename } from 'node:path';
import { crop } from './crop';
import { crop } from './crop.js';

test('matching images', async () => {
const image = createImageData(1, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from 'vitest';
import fc from 'fast-check';
import { assertInteger, EPSILON, isCloseToZero } from './numeric';
import { assertInteger, EPSILON, isCloseToZero } from './numeric.js';

test('isCloseToZero', () => {
expect(isCloseToZero(0)).toEqual(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import { iter } from '@votingworks/basics';
import { electionGridLayoutNewHampshireHudsonFixtures } from '@votingworks/fixtures';
import { Size } from '@votingworks/types';
Expand All @@ -9,7 +10,7 @@ import {
parsePdf,
pdfToImages,
setPdfRenderWorkerSrc,
} from './pdf_to_images';
} from './pdf_to_images.js';

const pdfNotRequiringPdfjsIntermediateCanvasBuffer = readFileSync(
join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { assert } from '@votingworks/basics';
import { Rect } from '@votingworks/types';
import { createImageData, ImageData } from 'canvas';
import fc from 'fast-check';
import { int, RGBA_CHANNEL_COUNT } from '../src';
import { assertInteger } from '../src/numeric';
import { int, RGBA_CHANNEL_COUNT } from '../src/index.js';
import { assertInteger } from '../src/numeric.js';

/**
* Options for building an arbitrary image data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from 'vitest';
import fc from 'fast-check';
import { assertInteger } from '../src/numeric';
import { arbitraryImageData, arbitraryRect } from './arbitraries';
import { assertInteger } from '../src/numeric.js';
import { arbitraryImageData, arbitraryRect } from './arbitraries.mjs';

test('arbitraryImageData has sensible values', () => {
fc.assert(
Expand Down
1 change: 1 addition & 0 deletions libs/image-utils/test/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImageData } from 'canvas';
import { expect } from 'vitest';
import { toMatchImage, ToMatchImageOptions } from '../src';

declare global {
Expand Down
7 changes: 6 additions & 1 deletion libs/image-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["**/*.test.ts", "**/*.test.tsx"],
"exclude": [
"**/*.test.ts",
"**/*.test.tsx",
"**/*.test.mts",
"**/*.test.mtsx"
],
"compilerOptions": {
"noEmit": false,
"outDir": "build",
Expand Down
10 changes: 10 additions & 0 deletions libs/image-utils/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '../../vitest.config.shared.mjs';

export default defineConfig({
test: {
setupFiles: ['test/setupTests.ts'],
coverage: {
exclude: ['src/jest_pdf_snapshot.ts', 'src/cli/pdf_to_images.ts'],
},
},
});
36 changes: 12 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.