Skip to content

Commit

Permalink
Use vitest for testing
Browse files Browse the repository at this point in the history
Faster, typescript support out of the box.
  • Loading branch information
ralfstx committed Feb 18, 2024
1 parent e6c6927 commit 512c016
Show file tree
Hide file tree
Showing 32 changed files with 1,337 additions and 62 deletions.
1,321 changes: 1,298 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"build": "tsc",
"lint": "eslint '{src,test}/**/*.{js,ts}' --max-warnings 0 --format visualstudio && prettier --check .",
"test": "jest"
"test": "vitest run test"
},
"dependencies": {
"@pdf-lib/fontkit": "^1.1.1",
Expand All @@ -40,7 +40,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^12.0.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^1.3.0"
}
}
2 changes: 1 addition & 1 deletion src/api/make-pdf.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'node:crypto';

import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { makePdf } from './make-pdf.js';

Expand Down
2 changes: 1 addition & 1 deletion src/binary-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { parseBinaryData } from './binary-data.js';

Expand Down
2 changes: 1 addition & 1 deletion src/box.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { parseEdges, parseLength, subtractEdges } from './box.js';

Expand Down
8 changes: 4 additions & 4 deletions src/font-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
import fontkit from '@pdf-lib/fontkit';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { createFontLoader, createFontStore, FontLoader } from './font-loader.js';
import { Font, FontDef, FontSelector } from './fonts.js';
Expand Down Expand Up @@ -136,16 +136,16 @@ describe('font-loader', () => {
beforeEach(() => {
testFont = fakeFont('Test');
fontLoader = {
loadFont: jest.fn(async (selector: FontSelector) => {
loadFont: vi.fn(async (selector: FontSelector) => {
if (selector.fontFamily === 'Test') return testFont;
throw new Error('No such font defined');
}) as any,
};
jest.spyOn(fontkit, 'create').mockReturnValue({ fake: true } as any);
vi.spyOn(fontkit, 'create').mockReturnValue({ fake: true } as any);
});

afterEach(() => {
jest.restoreAllMocks();
vi.restoreAllMocks();
});

it('rejects if font could not be loaded', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/fonts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { readFonts, weightToNumber } from './fonts.js';

Expand Down
4 changes: 2 additions & 2 deletions src/image-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

import { beforeAll, beforeEach, describe, expect, it, jest } from '@jest/globals';
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { createImageLoader, createImageStore, ImageLoader } from './image-loader.js';
import { ImageSelector } from './images.js';
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('image-loader', () => {

beforeEach(() => {
imageLoader = {
loadImage: jest.fn(async (selector: ImageSelector) => {
loadImage: vi.fn(async (selector: ImageSelector) => {
if (selector.name === 'liberty') return { data: libertyJpg };
if (selector.name === 'torus') return { data: torusPng };
throw new Error('No such image');
Expand Down
2 changes: 1 addition & 1 deletion src/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { Image, readImages, registerImage } from './images.js';
import { fakePDFDocument, mkData } from './test/test-utils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/images/jpeg.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { isJpeg, readJpegInfo } from './jpeg.js';

Expand Down
2 changes: 1 addition & 1 deletion src/images/png.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { isPng, readPngInfo } from './png.js';

Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout-columns.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
Expand Down
4 changes: 2 additions & 2 deletions src/layout/layout-image.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { Box } from '../box.js';
import { ImageStore } from '../image-loader.js';
Expand All @@ -15,7 +15,7 @@ describe('layout-image', () => {

beforeEach(() => {
const imageStore = {
selectImage: jest.fn(async (selector: ImageSelector) => {
selectImage: vi.fn(async (selector: ImageSelector) => {
const match = /^img-(\d+)-(\d+)$/.exec(selector.name);
if (match) {
return fakeImage(selector.name, Number(match[1]), Number(match[2]));
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout-rows.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { paperSizes } from '../api/sizes.js';
import { Box } from '../box.js';
Expand Down
2 changes: 1 addition & 1 deletion src/page.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { PDFPage } from 'pdf-lib';
import { beforeEach, describe, expect, it } from 'vitest';

import { Font } from './fonts.js';
import { addPageFont, getExtGraphicsState, Page } from './page.js';
Expand Down
2 changes: 1 addition & 1 deletion src/print-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { printValue } from './print-value.js';

Expand Down
2 changes: 1 addition & 1 deletion src/read-block.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import {
readBlock,
Expand Down
2 changes: 1 addition & 1 deletion src/read-colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { readColor } from './read-color.js';

Expand Down
2 changes: 1 addition & 1 deletion src/read-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { PageInfo, readDocumentDefinition } from './read-document.js';

Expand Down
2 changes: 1 addition & 1 deletion src/read-graphics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';
import { describe, expect, it } from 'vitest';

import { readShape } from './read-graphics.js';
import { p } from './test/test-utils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/read-page-size.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { paperSizes } from './api/sizes.js';
import { applyOrientation, parseOrientation, readPageSize } from './read-page-size.js';
Expand Down
2 changes: 1 addition & 1 deletion src/render/render-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'node:crypto';

import { describe, expect, it } from '@jest/globals';
import { PDFDict, PDFDocument, PDFHexString, PDFName, PDFStream, PDFString } from 'pdf-lib';
import { describe, expect, it } from 'vitest';

import { renderDocument } from './render-document.js';

Expand Down
2 changes: 1 addition & 1 deletion src/render/render-graphics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';
import { beforeEach, describe, expect, it } from 'vitest';

import { Size } from '../box.js';
import { CircleObject, LineObject, PathObject, PolylineObject, RectObject } from '../frame.js';
Expand Down
2 changes: 1 addition & 1 deletion src/render/render-image.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { Size } from '../box.js';
import { ImageObject } from '../frame.js';
Expand Down
4 changes: 2 additions & 2 deletions src/render/render-page.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { PDFArray, PDFDict, PDFDocument, PDFName, PDFPage, PDFRef } from 'pdf-lib';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { Size } from '../box.js';
import { Font } from '../fonts.js';
Expand All @@ -20,7 +20,7 @@ describe('render-page', () => {

beforeEach(() => {
size = { width: 300, height: 400 };
pdfDoc = { addPage: jest.fn().mockReturnValue(pdfPage) } as unknown as PDFDocument;
pdfDoc = { addPage: vi.fn().mockReturnValue(pdfPage) } as unknown as PDFDocument;
});

it('renders content', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/render/render-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from 'vitest';

import { Size } from '../box.js';
import { Font } from '../fonts.js';
Expand Down
2 changes: 1 addition & 1 deletion src/svg-paths.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { parseSvgPath, svgPathToPdfOps, tokenizeSvgPath } from './svg-paths.js';

Expand Down
2 changes: 1 addition & 1 deletion src/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';
import { beforeEach, describe, expect, it } from 'vitest';

import { FontStore } from './font-loader.js';
import { Font } from './fonts.js';
Expand Down
2 changes: 1 addition & 1 deletion src/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import {
dynamic,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import { round } from './utils.js';

Expand Down

0 comments on commit 512c016

Please sign in to comment.