Skip to content

Commit

Permalink
jest.mocked instead of ts-jest/mocked due to jestjs/jest#12089
Browse files Browse the repository at this point in the history
  • Loading branch information
boonya committed Jan 24, 2022
1 parent 185b2af commit 348e9a5
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project should be documented in this file.

- [Issue #195](https://github.com/boonya/rtsp-video-recorder/issues/195) acknowledged, investigated and fixed
- Dev dependencies updated
- `jest.mocked` instead of `ts-jest/mocked` due to https://github.com/facebook/jest/pull/12089

## [2.0.2-beta.1] - Bugfix & update

Expand Down
3 changes: 1 addition & 2 deletions test/events/error.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents, RecorderError } from '../../src/recorder';
Expand All @@ -10,7 +9,7 @@ let fakeProcess: ChildProcessWithoutNullStreams;
let eventHandler: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
eventHandler = jest.fn().mockName('onError');
});
Expand Down
3 changes: 1 addition & 2 deletions test/events/file_created.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -10,7 +9,7 @@ let fakeProcess: ChildProcessWithoutNullStreams;
let eventHandler: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
eventHandler = jest.fn().mockName('onFileCreated');
});
Expand Down
3 changes: 1 addition & 2 deletions test/events/progress.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -11,7 +10,7 @@ let fakeProcess: ChildProcessWithoutNullStreams;
let eventHandler: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
eventHandler = jest.fn().mockName('onProgress');
});
Expand Down
19 changes: 8 additions & 11 deletions test/events/space_full.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import dirSize from '../../src/helpers/space';
import Recorder, { RecorderEvents, RecorderError } from '../../src/recorder';
// import transformDirSizeThreshold from '../../src/helpers/sizeThreshold';

jest.mock('../../src/validators');
jest.mock('../../src/helpers/space');
// jest.mock('../../src/helpers/sizeThreshold');

let fakeProcess: ChildProcessWithoutNullStreams;
let onSpaceFull: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
onSpaceFull = jest.fn().mockName('onSpaceFull');
});

test('should not evaluate space if "threshold" is undefined', async () => {
mocked(dirSize).mockReturnValue(Infinity);
jest.mocked(dirSize).mockReturnValue(Infinity);

new Recorder(URI, DESTINATION)
.on(RecorderEvents.SPACE_FULL, onSpaceFull)
Expand All @@ -36,7 +33,7 @@ test('should not evaluate space if "threshold" is undefined', async () => {
});

test('should evaluate space but not rise an event if "used" is less than the "threshold"', async () => {
mocked(dirSize).mockReturnValue(300);
jest.mocked(dirSize).mockReturnValue(300);
const onStopped = jest.fn().mockName('onStopped');

new Recorder(URI, DESTINATION, { dirSizeThreshold: 500 })
Expand All @@ -56,7 +53,7 @@ test('should evaluate space but not rise an event if "used" is less than the "th
});

test('should evaluate space on start and rise an event if "used" is close to the "threshold"', async () => {
mocked(dirSize).mockReturnValue(496);
jest.mocked(dirSize).mockReturnValue(496);
const onStopped = jest.fn().mockName('onStopped');

new Recorder(URI, DESTINATION, { dirSizeThreshold: 500 })
Expand All @@ -78,7 +75,7 @@ test('should evaluate space on start and rise an event if "used" is close to the
});

test('should evaluate space on start and rise an event if "used" is bigger than the "threshold"', async () => {
mocked(dirSize).mockReturnValue(600);
jest.mocked(dirSize).mockReturnValue(600);
const onStopped = jest.fn().mockName('onStopped');

new Recorder(URI, DESTINATION, { dirSizeThreshold: 500 })
Expand All @@ -100,7 +97,7 @@ test('should evaluate space on start and rise an event if "used" is bigger than
});

test('should evaluate space twice and rise an event if "used" became bigger than the "threshold" at progress', async () => {
mocked(dirSize).mockReturnValueOnce(200);
jest.mocked(dirSize).mockReturnValueOnce(200);
const onStop = jest.fn().mockName('onStop');

new Recorder(URI, DESTINATION, { dirSizeThreshold: 500 })
Expand All @@ -111,7 +108,7 @@ test('should evaluate space twice and rise an event if "used" became bigger than
// We have to wait next tick
await Promise.resolve(true);

mocked(dirSize).mockReturnValueOnce(600);
jest.mocked(dirSize).mockReturnValueOnce(600);

fakeProcess.stderr.emit('data', Buffer.from('Opening \'segment.mp4\' for writing', 'utf8'));

Expand All @@ -126,7 +123,7 @@ test('should evaluate space twice and rise an event if "used" became bigger than
});

test('should return RecorderError - space evaluation failed', async () => {
mocked(dirSize).mockImplementation(() => {
jest.mocked(dirSize).mockImplementation(() => {
throw new Error('space evaluation failed');
});
const onError = jest.fn().mockName('onError');
Expand Down
3 changes: 1 addition & 2 deletions test/events/start.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -8,7 +7,7 @@ jest.mock('../../src/validators');
let onStart: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
mockSpawnProcess();
onStart = jest.fn().mockName('onStart');
});
Expand Down
5 changes: 2 additions & 3 deletions test/events/started.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -12,10 +11,10 @@ let fakeProcess: ChildProcessWithoutNullStreams;
let eventHandler: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
eventHandler = jest.fn().mockName('onStarted');
mocked(dirSize).mockReturnValue(0);
jest.mocked(dirSize).mockReturnValue(0);
});

const FFMPEG_MESSAGE = `[libx264 @ 0x148816200] 264 - core 163 r3060 5db6aa6 - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=15 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Expand Down
3 changes: 1 addition & 2 deletions test/events/stop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -9,7 +8,7 @@ let onStop: () => void;
let onStopped: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
mockSpawnProcess();
onStop = jest.fn().mockName('onStop');
onStopped = jest.fn().mockName('onStopped');
Expand Down
3 changes: 1 addition & 2 deletions test/events/stopped.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from '../test.helpers';
import Recorder, { RecorderEvents } from '../../src/recorder';
Expand All @@ -10,7 +9,7 @@ let fakeProcess: ChildProcessWithoutNullStreams;
let eventHandler: () => void;

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
fakeProcess = mockSpawnProcess();
eventHandler = jest.fn().mockName('onStopped');
});
Expand Down
15 changes: 5 additions & 10 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'ts-jest/utils';
import directoryExists from '../src/helpers/directoryExists';
import dirSize from '../src/helpers/space';
import fs from 'fs';
Expand All @@ -10,24 +9,21 @@ jest.mock('path');

describe('directoryExists', () => {
test('exists', () => {
// @ts-ignore
mocked(fs).lstatSync.mockReturnValue({isDirectory: () => true});
jest.mocked(fs).lstatSync.mockReturnValue({isDirectory: () => true});

expect(directoryExists('path')).toBeTruthy();
});

test('does not exist', () => {
// @ts-ignore
mocked(fs).lstatSync.mockImplementation(() => {
jest.mocked(fs).lstatSync.mockImplementation(() => {
throw new Error('no such file or directory');
});

expect(directoryExists('path')).toBeFalsy();
});

test('not a directory', () => {
// @ts-ignore
mocked(fs).lstatSync.mockReturnValue({isDirectory: () => false});
jest.mocked(fs).lstatSync.mockReturnValue({isDirectory: () => false});

expect(() => directoryExists('path')).toThrowError('path exists but it is not a directory.');
});
Expand All @@ -44,9 +40,8 @@ test('transformSegmentTime', () => {
});

test('should return directory size in bytes', () => {
mocked(fs).readdirSync.mockReturnValue(new Array(3).fill(0));
// @ts-ignore
mocked(fs).statSync.mockReturnValue({isDirectory: () => false, size: 3});
jest.mocked(fs).readdirSync.mockReturnValue(new Array(3).fill(0));
jest.mocked(fs).statSync.mockReturnValue({isDirectory: () => false, size: 3});

const size = dirSize('');

Expand Down
3 changes: 1 addition & 2 deletions test/process.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'ts-jest/utils';
import {mockSpawnProcess, URI, DESTINATION} from './test.helpers';
import Recorder from '../src/recorder';
import { verifyAllOptions } from '../src/validators';
Expand All @@ -7,7 +6,7 @@ import { Options } from '../src/types';
jest.mock('../src/validators');

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
});

it('Spawn arguments with no additional options defined', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/recorder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { mocked } from 'ts-jest/utils';
import { verifyAllOptions } from '../src/validators';
import {mockSpawnProcess, URI, DESTINATION} from './test.helpers';
import Recorder, { RecorderValidationError } from '../src/recorder';

jest.mock('../src/validators');

beforeEach(() => {
mocked(verifyAllOptions).mockReturnValue([]);
jest.mocked(verifyAllOptions).mockReturnValue([]);
mockSpawnProcess();
});

test('should throw RecorderValidationError if validation failed', () => {
mocked(verifyAllOptions).mockReturnValue([
jest.mocked(verifyAllOptions).mockReturnValue([
'Any validation error message',
'One more validation error message',
]);
Expand Down
3 changes: 1 addition & 2 deletions test/test.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventEmitter } from 'events';
import { mocked } from 'ts-jest/utils';
import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
import path from 'path';

Expand All @@ -26,7 +25,7 @@ export function mockSpawnProcess (options: MockSpawnProcessOptions = {}) {
return true;
};

mocked(spawn).mockImplementation((...args) => {
jest.mocked(spawn).mockImplementation((...args) => {
onSpawn(...args);
return proc;
});
Expand Down

0 comments on commit 348e9a5

Please sign in to comment.