Skip to content

Commit

Permalink
test: add unit tests ensuring correct removeMetadata behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Apr 28, 2021
1 parent 0b38337 commit 2e6eb0b
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/rollup/src/__tests__/__fixtures__/with-metadata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 77 additions & 3 deletions packages/rollup/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { join } from 'path'
import { testEntry, getFiles } from './util'
import { toMatchImageSnapshot } from 'jest-image-snapshot'
import { JSDOM } from 'jsdom'
import sharp from 'sharp'

expect.extend({ toMatchImageSnapshot })
process.chdir(join(__dirname, 'fixtures'))
process.chdir(join(__dirname, '__fixtures__'))

describe('rollup-plugin-imagetools', () => {
describe('options', () => {
Expand Down Expand Up @@ -154,8 +155,81 @@ describe('rollup-plugin-imagetools', () => {
})

describe('removeMetadata', () => {
test('true removes private metadata', () => { })
test('false leaves private metadata', () => { })
test('true removes private metadata', async () => {
const bundle = await rollup({
plugins: [
testEntry(`
import Image from "./with-metadata.png?w=300"
export default Image
`),
imagetools({
removeMetadata: true
})
]
})

const files = await getFiles(bundle, '**.png') as OutputAsset[]

const metadata = await sharp(files[0].source as Buffer).metadata()

expect(metadata).not.toHaveProperty('icc')
expect(metadata).not.toHaveProperty('xmp')
})

test('false leaves private metadata', async () => {
const bundle = await rollup({
plugins: [
testEntry(`
import Image from "./with-metadata.png?w=300"
export default Image
`),
imagetools({
removeMetadata: false
})
]
})

const files = await getFiles(bundle, '**.png') as OutputAsset[]

const metadata = await sharp(files[0].source as Buffer).metadata()

expect(metadata).toHaveProperty('icc')
expect(metadata).toHaveProperty('xmp')
})
})

describe('defaultDirectives', () => {
test('object', async () => {
const p = rollup({
plugins: [
testEntry(`
import Image from "./pexels-allec-gomes-5195763.png?w=300"
export default Image
`),
imagetools({
defaultDirectives: { foo:'bar' }
})
]
})

await expect(p).resolves.toBeDefined()
})

test('function', async () => {
const p = rollup({
plugins: [
testEntry(`
import Image from "./pexels-allec-gomes-5195763.png?w=300"
export default Image
`),
imagetools({
defaultDirectives: () => ({ foo:'bar' })
})
]
})

await expect(p).resolves.toBeDefined()
})
})
})

Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/__tests__/__fixtures__/with-metadata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 87 additions & 3 deletions packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { getFiles, testEntry } from './util'
import { toMatchImageSnapshot } from 'jest-image-snapshot'
import { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
import { JSDOM } from 'jsdom'
import sharp from 'sharp'

expect.extend({ toMatchImageSnapshot })
process.chdir(join(__dirname, 'fixtures'))
process.chdir(join(__dirname, '__fixtures__'))

describe('vite-imagetools', () => {
describe('options', () => {
Expand Down Expand Up @@ -204,8 +205,91 @@ describe('vite-imagetools', () => {
})

describe('removeMetadata', () => {
test('true removes private metadata', () => { })
test('false leaves private metadata', () => { })
test('true removes private metadata', async () => {
const bundle = await build({
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./with-metadata.png?metadata"
window.__IMAGE__ = Image
`),
imagetools({
removeMetadata: true
})
]
}) as RollupOutput | RollupOutput[]

const files = getFiles(bundle, '**.png') as OutputAsset[]

const metadata = await sharp(files[0].source as Buffer).metadata()

expect(metadata).not.toHaveProperty('icc')
expect(metadata).not.toHaveProperty('xmp')
})

test('false leaves private metadata', async () => {
const bundle = await build({
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./with-metadata.png?metadata"
window.__IMAGE__ = Image
`),
imagetools({
removeMetadata: false
})
]
}) as RollupOutput | RollupOutput[]

const files = getFiles(bundle, '**.png') as OutputAsset[]

const metadata = await sharp(files[0].source as Buffer).metadata()

expect(metadata).toHaveProperty('icc')
expect(metadata).toHaveProperty('xmp')
})
})

describe('defaultDirectives', () => {
test('object', async () => {
const p = build({
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./pexels-allec-gomes-5195763.png?w=300"
export default Image
`),

imagetools({
defaultDirectives: { foo:'bar' }
})
]
})

await expect(p).resolves.toBeDefined()
})

test('function', async () => {
const p = build({
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./pexels-allec-gomes-5195763.png?w=300"
export default Image
`),

imagetools({
defaultDirectives: () => ({ foo:'bar' })
})
]
})

await expect(p).resolves.toBeDefined()
})
})
})

Expand Down

0 comments on commit 2e6eb0b

Please sign in to comment.