-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
53 lines (44 loc) · 1.6 KB
/
index.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
// index.test.js
const mockStream = {
pipe: jest.fn().mockReturnThis(),
on: jest.fn((event, callback) => {
if (event === 'end') callback();
return mockStream;
}),
};
jest.mock('ytdl-core', () => jest.fn(() => mockStream));
jest.mock('fluent-ffmpeg', () => {
const mockFfmpeg = {
on: jest.fn((event, callback) => {
if (event === 'end') callback();
return mockFfmpeg;
}),
screenshots: jest.fn().mockReturnThis(),
};
return jest.fn(() => mockFfmpeg);
});
const takeScreenshot = require('./index');
test('should successfully take a screenshot', async () => {
const videoUrl = 'https://www.youtube.com/watch?v=example';
const time = '00:01:00';
const outputPath = './';
const filename = 'screenshot.png';
await expect(takeScreenshot(videoUrl, time, outputPath, filename)).resolves.toBeUndefined();
});
test('should handle errors from fluent-ffmpeg', async () => {
require('fluent-ffmpeg').mockImplementationOnce(() => {
const errorMockFfmpeg = {
on: jest.fn((event, callback) => {
if (event === 'error') callback(new Error('fluent-ffmpeg error'));
return errorMockFfmpeg;
}),
screenshots: jest.fn().mockReturnThis(),
};
return errorMockFfmpeg;
});
const videoUrl = 'https://www.youtube.com/watch?v=example';
const time = '00:01:00';
const outputPath = './';
const filename = 'screenshot.png';
await expect(takeScreenshot(videoUrl, time, outputPath, filename)).rejects.toThrow('fluent-ffmpeg error');
});