forked from imagemin/imagemin-jpeg-recompress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·56 lines (43 loc) · 1.64 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const isJpg = require('is-jpg');
const isProgressive = require('is-progressive');
const test = require('ava');
const {ExifImage} = require('exif');
const m = require('.');
const readFile = promisify(fs.readFile);
const exifImageAsync = promisify(ExifImage);
test('optimize a JPG', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);
t.true(data.length < buf.length);
t.true(isJpg(data));
t.true(isProgressive.buffer(data));
});
test('progressive option', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m({progressive: false})(buf);
t.false(isProgressive.buffer(data));
});
test('strip option', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m({strip: false})(buf);
t.is((await exifImageAsync(data)).image.Software, 'imagemin-jpeg-recompress');
});
test('strip metadata by default', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);
const error = await t.throwsAsync(() => exifImageAsync(data));
t.is(error.message, 'No Exif segment found in the given image.');
});
test('skip optimizing a non-JPG file', async t => {
const buf = await readFile(__filename);
const data = await m()(buf);
t.deepEqual(data, buf);
});
test('throw error when a JPG is corrupt', async t => {
const buf = await readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
const error = await t.throwsAsync(() => m()(buf));
t.regex(error.message, /Corrupt JPEG data/);
});