-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.js
48 lines (40 loc) · 1.38 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
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {fileTypeFromBuffer} from 'file-type';
import test from 'ava';
import Vinyl from 'vinyl';
import {vinylFile} from 'vinyl-file';
import {pEvent} from 'p-event';
import webp from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('converts images to WebP', async t => {
const file = await vinylFile(path.join(__dirname, 'fixture.jpg'));
const stream = webp();
const promise = pEvent(stream, 'data');
stream.end(file);
const data = await promise;
const {mime} = await fileTypeFromBuffer(file.contents);
t.is(mime, 'image/webp');
t.is(data.path, path.join(__dirname, 'fixture.webp'));
});
test('should not convert unsupported files', async t => {
const stream = webp();
const promise = pEvent(stream, 'data');
stream.end(new Vinyl({
path: path.join(__dirname, 'fixture.jpg'),
contents: Buffer.from('contents'),
}));
const data = await promise;
t.is(data.contents.toString(), 'contents');
});
test('emits a plugin error when the image is corrupt', async t => {
const fileName = path.join(__dirname, 'fixture-corrupt.jpg');
const file = await vinylFile(fileName);
const stream = webp();
const promise = pEvent(stream, 'error');
stream.end(file);
const error = await promise;
t.is(error.plugin, 'gulp-webp');
t.is(error.fileName, fileName);
});