Skip to content

Commit

Permalink
Migrate Node tests to TypeScript (versatica#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc authored and piranna committed Feb 8, 2023
1 parent ebc97e9 commit c84da04
Show file tree
Hide file tree
Showing 27 changed files with 693 additions and 800 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Tell GitHub to ignore some folders when detecting the project language.
art/* linguist-vendored=true
doc/* linguist-vendored=true
node/test/* linguist-vendored=true
rust/benches/* linguist-vendored=true
rust/examples/* linguist-vendored=true
rust/examples-frontend/* linguist-vendored=true
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/mediasoup-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
restore-keys: |
${{ matrix.ci.os }}-node-
- run: npm install
- run: npm ci
- run: npm run lint:node
if: runner.os == 'Linux'
- run: npm run test:node
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

## Others.
/coverage
/.cache
/NO_GIT
*.swp
*.swo
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### NEXT

* Node: Migrate tests to TypeScript (PR #958).
* Node: Remove compiled JavaScript from repository and compile TypeScript code on NPM `prepare` script on demand when installed via git (PR #954).
* `Worker`: Add `RTC::Shared` singleton for RTC entities (PR #953).

Expand Down
11 changes: 8 additions & 3 deletions node/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const os = require('os');

const isWindows = os.platform() === 'win32';

const eslintConfig =
{
env :
Expand All @@ -15,7 +19,8 @@ const eslintConfig =
{
impliedStrict : true
},
lib : [ 'es2018' ]
lib : [ 'es2018' ],
project : 'node/tsconfig.json'
},
globals :
{
Expand Down Expand Up @@ -59,7 +64,7 @@ const eslintConfig =
}
],
'keyword-spacing' : 2,
'linebreak-style' : [ 2, 'unix' ],
'linebreak-style' : [ 2, isWindows ? 'windows' : 'unix' ],
'lines-around-comment' : [ 2,
{
allowBlockStart : true,
Expand Down Expand Up @@ -222,7 +227,7 @@ eslintConfig.overrides.push(

eslintConfig.overrides.push(
{
files : [ '*.js' ],
files : [ '*.ts' ],
env : {
...eslintConfig.env,
'jest/globals' : true
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const { toBeType } = require('jest-tobetype');
const mediasoup = require('../lib/');
const { createWorker } = mediasoup;
import * as mediasoup from '../';

expect.extend({ toBeType });
const { createWorker } = mediasoup;

let worker;
let router;
let activeSpeakerObserver;
let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let activeSpeakerObserver: mediasoup.types.ActiveSpeakerObserver;

const mediaCodecs =
const mediaCodecs: mediasoup.types.RtpCodecCapability[] =
[
{
kind : 'audio',
Expand Down Expand Up @@ -41,7 +39,7 @@ test('router.createActiveSpeakerObserver() succeeds', async () =>

expect(onObserverNewRtpObserver).toHaveBeenCalledTimes(1);
expect(onObserverNewRtpObserver).toHaveBeenCalledWith(activeSpeakerObserver);
expect(activeSpeakerObserver.id).toBeType('string');
expect(typeof activeSpeakerObserver.id).toBe('string');
expect(activeSpeakerObserver.closed).toBe(false);
expect(activeSpeakerObserver.paused).toBe(false);
expect(activeSpeakerObserver.appData).toEqual({});
Expand All @@ -56,10 +54,12 @@ test('router.createActiveSpeakerObserver() succeeds', async () =>

test('router.createActiveSpeakerObserver() with wrong arguments rejects with TypeError', async () =>
{
// @ts-ignore
await expect(router.createActiveSpeakerObserver({ interval: false }))
.rejects
.toThrow(TypeError);

// @ts-ignore
await expect(router.createActiveSpeakerObserver({ appData: 'NOT-AN-OBJECT' }))
.rejects
.toThrow(TypeError);
Expand Down Expand Up @@ -102,7 +102,7 @@ test('ActiveSpeakerObserver emits "routerclose" if Router is closed', async () =
const router2 = await worker.createRouter({ mediaCodecs });
const activeSpeakerObserver2 = await router2.createAudioLevelObserver();

await new Promise((resolve) =>
await new Promise<void>((resolve) =>
{
activeSpeakerObserver2.on('routerclose', resolve);
router2.close();
Expand All @@ -113,7 +113,7 @@ test('ActiveSpeakerObserver emits "routerclose" if Router is closed', async () =

test('ActiveSpeakerObserver emits "routerclose" if Worker is closed', async () =>
{
await new Promise((resolve) =>
await new Promise<void>((resolve) =>
{
activeSpeakerObserver.on('routerclose', resolve);
worker.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { toBeType } = require('jest-tobetype');
const mediasoup = require('../lib/');
import * as mediasoup from '../';
const { createWorker } = mediasoup;

expect.extend({ toBeType });
let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let audioLevelObserver: mediasoup.types.AudioLevelObserver;

let worker;
let router;
let audioLevelObserver;

const mediaCodecs =
const mediaCodecs: mediasoup.types.RtpCodecCapability[] =
[
{
kind : 'audio',
Expand Down Expand Up @@ -41,7 +38,7 @@ test('router.createAudioLevelObserver() succeeds', async () =>

expect(onObserverNewRtpObserver).toHaveBeenCalledTimes(1);
expect(onObserverNewRtpObserver).toHaveBeenCalledWith(audioLevelObserver);
expect(audioLevelObserver.id).toBeType('string');
expect(typeof audioLevelObserver.id).toBe('string');
expect(audioLevelObserver.closed).toBe(false);
expect(audioLevelObserver.paused).toBe(false);
expect(audioLevelObserver.appData).toEqual({});
Expand All @@ -64,14 +61,17 @@ test('router.createAudioLevelObserver() with wrong arguments rejects with TypeEr
.rejects
.toThrow(TypeError);

// @ts-ignore
await expect(router.createAudioLevelObserver({ threshold: 'foo' }))
.rejects
.toThrow(TypeError);

// @ts-ignore
await expect(router.createAudioLevelObserver({ interval: false }))
.rejects
.toThrow(TypeError);

// @ts-ignore
await expect(router.createAudioLevelObserver({ appData: 'NOT-AN-OBJECT' }))
.rejects
.toThrow(TypeError);
Expand Down Expand Up @@ -114,7 +114,7 @@ test('AudioLevelObserver emits "routerclose" if Router is closed', async () =>
const router2 = await worker.createRouter({ mediaCodecs });
const audioLevelObserver2 = await router2.createAudioLevelObserver();

await new Promise((resolve) =>
await new Promise<void>((resolve) =>
{
audioLevelObserver2.on('routerclose', resolve);
router2.close();
Expand All @@ -125,7 +125,7 @@ test('AudioLevelObserver emits "routerclose" if Router is closed', async () =>

test('AudioLevelObserver emits "routerclose" if Worker is closed', async () =>
{
await new Promise((resolve) =>
await new Promise<void>((resolve) =>
{
audioLevelObserver.on('routerclose', resolve);
worker.close();
Expand Down
Loading

0 comments on commit c84da04

Please sign in to comment.