Skip to content

Commit

Permalink
chore: testing setup for image shrink
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Feb 16, 2024
1 parent a7a08b6 commit ed20998
Show file tree
Hide file tree
Showing 8 changed files with 2,524 additions and 112 deletions.
2,570 changes: 2,467 additions & 103 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions packages/image-shrink/jest.config.js

This file was deleted.

10 changes: 6 additions & 4 deletions packages/image-shrink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"prepack": "cp ../../LICENSE ./LICENSE",
"clean": "rimraf dist",
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
"test": "npx playwright install-deps chromium && vitest --run",
"prebuild": "npm run clean",
"build": "npm run build:types && npm run build:compile",
"build:types": "dts-bundle-generator --project tsconfig.dts.json -o dist/index.d.ts src/index.ts",
Expand All @@ -43,7 +43,9 @@
"signature"
],
"devDependencies": {
"ts-node": "^10.8.1"
},
"dependencies": {}
"@vitest/browser": "^1.2.2",
"playwright": "^1.41.2",
"ts-node": "^10.8.1",
"vitest": "^1.2.2"
}
}
11 changes: 11 additions & 0 deletions packages/image-shrink/src/test/helpers/blobToImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const blobToImage = (blob: Blob) => {
return new Promise<HTMLImageElement>((resolve) => {
const url = URL.createObjectURL(blob)
const img = new Image()
img.onload = () => {
URL.revokeObjectURL(url)
resolve(img)
}
img.src = url
})
}
5 changes: 5 additions & 0 deletions packages/image-shrink/src/test/helpers/fileFromUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const fileFromUrl = async (url: string) => {
const response = await fetch(url)
const buffer = await response.arrayBuffer()
return new File([buffer], 'some.jpeg', { type: 'image/jpeg' })
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions packages/image-shrink/src/utils/shrinkFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference types="vite/client" />
import { expect, describe, it } from 'vitest'
import { shrinkFile } from './shrinkFile'
import imageUrl from '../test/samples/2000x2000.jpeg'
import { fileFromUrl } from '../test/helpers/fileFromUrl'
import { blobToImage } from '../test/helpers/blobToImage'

describe('shrinkFile', () => {
it('should shrink the image', async () => {
const file = await fileFromUrl(imageUrl)
const blob = await shrinkFile(file, {
size: 100,
quality: 0.5
})
expect(blob.size).toBeLessThan(file.size)
expect(blob.size).toBeLessThan(1000)

const img = await blobToImage(blob)
expect(img.width).toBe(10)
expect(img.height).toBe(10)
})
})
13 changes: 13 additions & 0 deletions packages/image-shrink/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
test: {
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true
}
}
})

0 comments on commit ed20998

Please sign in to comment.