Skip to content

Commit

Permalink
Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Jul 30, 2024
1 parent b4581e6 commit bce17ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"start:shellyDevice": "node dist/shellyDevice.js startShelly",
"start:shelly": "node dist/shelly.js shelly",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:verbose": "node --experimental-vm-modules node_modules/jest/bin/jest.js--verbose",
"test:verbose": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"test:platform": "node --experimental-vm-modules node_modules/jest/bin/jest.js index.test.ts platform.test.ts --coverage",
Expand Down
23 changes: 23 additions & 0 deletions src/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { parseBasicAuthenticateHeader, parseDigestAuthenticateHeader, createBasicShellyAuth, createDigestShellyAuth, getGen1BodyOptions, getGen2BodyOptions } from './auth';
import { jest } from '@jest/globals';

describe('Authenticathe utility test', () => {
let consoleLogSpy: jest.SpiedFunction<typeof console.log>;

beforeAll(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation((message?: any, ...optionalParams: any[]) => {
// console.error(`Mocked console.log: ${message}`, optionalParams);
});
});

beforeEach(() => {
//
});

afterEach(() => {
//
});

afterAll(() => {
consoleLogSpy.mockRestore();
});

test('parseBasicAuthenticateHeader', () => {
expect(parseBasicAuthenticateHeader('Basic realm="Shelly')).toStrictEqual({
realm: 'Shelly',
Expand Down
3 changes: 1 addition & 2 deletions src/mcastServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Muticast server and client test', () => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation((message?: any, ...optionalParams: any[]) => {
// console.error(`Mocked console.log: ${message}`, optionalParams);
});
consoleLogSpy.mockRestore();
// consoleLogSpy.mockRestore();
});

beforeEach(() => {
Expand All @@ -26,7 +26,6 @@ describe('Muticast server and client test', () => {

afterAll(() => {
consoleLogSpy.mockRestore();
//
});

test('Create the Multicast class', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/mdnsScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { jest } from '@jest/globals';
import path from 'path';
import { readFileSync } from 'fs';
import { ResponsePacket } from 'multicast-dns';
import { getIpv4InterfaceAddress } from 'matterbridge/utils';

async function loadResponse(shellyId: string) {
const responseFile = path.join('src', 'mock', `${shellyId}.mdns.json`);
Expand Down Expand Up @@ -50,6 +51,7 @@ describe('Shellies MdnsScanner test', () => {
afterAll(() => {
mdns.stop();
mdns.off('discovered', discoveredDeviceListener);
consoleLogSpy.mockRestore();
});

test('Constructor', () => {
Expand All @@ -74,6 +76,17 @@ describe('Shellies MdnsScanner test', () => {
}, 4000);
}, 10000);

test('Start discover with interface', (done) => {
mdns.start(3000, getIpv4InterfaceAddress(), 'udp4', true);
expect(mdns.isScanning).toBeTruthy();
setTimeout(() => {
mdns.stop();
done();
expect(mdns.isScanning).toBeFalsy();
// expect(discoveredDeviceListener).toHaveBeenCalled();
}, 4000);
}, 10000);

// eslint-disable-next-line jest/no-commented-out-tests
/*
test('Discover shellyplus1pm-441793D69718', (done) => {
Expand Down

0 comments on commit bce17ad

Please sign in to comment.