Skip to content

Commit

Permalink
Merge pull request #224 from ably-labs/test-typing
Browse files Browse the repository at this point in the history
test: force typescript checks on tests
  • Loading branch information
AndyTWF authored Jun 26, 2024
2 parents 01b1102 + e6ea6bd commit c4b4b1a
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ jobs:
node-version: 20
- run: npm ci
- run: npm run lint
test-typescript:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/setup-node@v1
with:
node-version: 20
- run: npm ci
- run: npm run test:typescript
test:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test:watch": "vitest watch",
"build": "vite build",
"prepare": "npm run build",
"test:typescript": "tsc --project tsconfig.test.json",
"demo:reload": "npm run build && cd demo && npm i file:../",
"docs": "typedoc",
"modulereport": "tsc --noEmit --esModuleInterop scripts/moduleReport.ts && esr scripts/moduleReport.ts"
Expand Down
9 changes: 8 additions & 1 deletion test/Messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Ably from 'ably';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { ChatApi } from '../src/ChatApi.js';
import { normaliseClientOptions } from '../src/config.js';
import { MessageEvents } from '../src/events.js';
import { DefaultRoom } from '../src/Room.js';
import { randomRoomId } from './helper/identifier.js';
Expand Down Expand Up @@ -71,7 +72,13 @@ describe('Messages', () => {
createdAt: timestamp,
});

const room = new DefaultRoom('coffee-room-chat', realtime, chatApi, { typingTimeoutMs: 300 }, makeTestLogger());
const room = new DefaultRoom(
'coffee-room-chat',
realtime,
chatApi,
normaliseClientOptions({ typingTimeoutMs: 300 }),
makeTestLogger(),
);
const messagePromise = room.messages.send('hello there');

const message = await messagePromise;
Expand Down
2 changes: 1 addition & 1 deletion test/Presence.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('UserPresence', { timeout: 10000 }, () => {
realtimeClient: Realtime,
event: PresenceAction | PresenceAction[],
realtimeChannelName: string,
expectationFn: (member: object) => void,
expectationFn: (member: Ably.PresenceMessage) => void,
) {
return new Promise<void>((resolve) => {
const presence = realtimeClient.channels.get(realtimeChannelName).presence;
Expand Down
20 changes: 10 additions & 10 deletions test/SubscriptionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const waitForPresenceEvent = (
};

// Wait for the channel to change state to the expected state
const waitForChannelStateChange = (channel, expectedState) => {
const waitForChannelStateChange = (channel: Ably.RealtimeChannel, expectedState: Ably.ChannelState) => {
return new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
if (channel.state === expectedState) {
Expand All @@ -67,7 +67,7 @@ const waitForChannelStateChange = (channel, expectedState) => {

// Assert that a channel does not enter the expected state
// during the interval
const assertNoChannelStateChange = (channel, expectedState) => {
const assertNoChannelStateChange = (channel: Ably.RealtimeChannel, expectedState: Ably.ChannelState) => {
return new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
if (channel.state === expectedState) {
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('subscription manager', { timeout: 15000 }, () => {
const { channel, publishChannel, subscriptionManager } = context;

const receivedMessages: Ably.Message[] = [];
const listener = (message) => {
const listener = (message: Ably.Message) => {
receivedMessages.push(message);
};
await subscriptionManager.subscribe(['test-event'], listener);
Expand All @@ -116,7 +116,7 @@ describe('subscription manager', { timeout: 15000 }, () => {
const { channel, publishChannel, subscriptionManager } = context;

const receivedMessages: Ably.Message[] = [];
const listener = (message) => {
const listener = (message: Ably.Message) => {
receivedMessages.push(message);
};
await subscriptionManager.subscribe(listener);
Expand All @@ -138,7 +138,7 @@ describe('subscription manager', { timeout: 15000 }, () => {
const { channel, publishChannel, subscriptionManager } = context;

const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
await subscriptionManager.presenceSubscribe(listener);
Expand All @@ -156,7 +156,7 @@ describe('subscription manager', { timeout: 15000 }, () => {
const { channel, publishChannel, subscriptionManager } = context;

const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
await subscriptionManager.presenceSubscribe('update', listener);
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('subscription manager', { timeout: 15000 }, () => {
});
it<TestContext>('should emit an enter event with supplied data when entering presence', async (context) => {
const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
// subscribe to presence events
Expand All @@ -297,7 +297,7 @@ describe('subscription manager', { timeout: 15000 }, () => {

it<TestContext>('should emit an event enter when joining for the first time', async (context) => {
const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
// subscribe to presence events
Expand All @@ -311,7 +311,7 @@ describe('subscription manager', { timeout: 15000 }, () => {

it<TestContext>('should emit an update event if already entered presence', async (context) => {
const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
// Join presence first
Expand Down Expand Up @@ -347,7 +347,7 @@ describe('subscription manager', { timeout: 15000 }, () => {

it<TestContext>('should emit a leave event with supplied data when leaving presence', async (context) => {
const receivedMessages: Ably.PresenceMessage[] = [];
const listener = (message) => {
const listener = (message: Ably.PresenceMessage) => {
receivedMessages.push(message);
};
// subscribe to presence events
Expand Down
11 changes: 8 additions & 3 deletions test/Typing.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as Ably from 'ably';
import { beforeEach, describe, expect, it } from 'vitest';

import { normaliseClientOptions } from '../src/config.js';
import { Room } from '../src/Room.js';
import { DefaultRooms, Rooms } from '../src/Rooms.js';
import { TypingEvent } from '../src/Typing.js';
Expand Down Expand Up @@ -40,7 +41,11 @@ describe('Typing', () => {
beforeEach<TestContext>((context) => {
context.realtime = ablyRealtimeClient();
context.roomId = randomRoomId();
context.chat = new DefaultRooms(context.realtime, { typingTimeoutMs: 300 }, makeTestLogger());
context.chat = new DefaultRooms(
context.realtime,
normaliseClientOptions({ typingTimeoutMs: 300 }),
makeTestLogger(),
);
context.clientId = context.realtime.auth.clientId;
context.chatRoom = context.chat.get(context.roomId);
});
Expand Down Expand Up @@ -104,13 +109,13 @@ describe('Typing', () => {
const clientId1 = randomClientId();
const client1 = new DefaultRooms(
ablyRealtimeClient({ clientId: clientId1 }),
{ typingTimeoutMs: 1000 },
normaliseClientOptions({ typingTimeoutMs: 1000 }),
makeTestLogger(),
);
const clientId2 = randomClientId();
const client2 = new DefaultRooms(
ablyRealtimeClient({ clientId: clientId2 }),
{ typingTimeoutMs: 1000 },
normaliseClientOptions({ typingTimeoutMs: 1000 }),
makeTestLogger(),
);

Expand Down
26 changes: 23 additions & 3 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { normaliseClientOptions } from '../src/config.js';
import { LogContext, LogLevel, makeLogger } from '../src/logger.js';
import { LogContext, Logger, LogLevel, makeLogger } from '../src/logger.js';

const defaultLogContext = { contextKey: 'contextValue' };

Expand Down Expand Up @@ -38,7 +38,7 @@ describe('logger', () => {
});

const logger = makeLogger(options);
logger[logLevel]('test', logContext);
callMethodForLevel(logger, logLevel, logContext);
reject('Expected logHandler to be called');
}),
);
Expand Down Expand Up @@ -71,8 +71,28 @@ describe('logger', () => {
});

const logger = makeLogger(options);
logger[logLevel]('test');
callMethodForLevel(logger, logLevel);
done();
}),
);
});

const callMethodForLevel = (log: Logger, level: Omit<LogLevel, 'silent'>, context?: object | undefined) => {
switch (level) {
case LogLevel.trace:
log.trace('test', context);
break;
case LogLevel.debug:
log.debug('test', context);
break;
case LogLevel.info:
log.info('test', context);
break;
case LogLevel.warn:
log.warn('test', context);
break;
case LogLevel.error:
log.error('test', context);
break;
}
};
33 changes: 33 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"include": [
"test/**/*.spec.ts",
"test/**/*.test.ts"
],
"exclude": [
"test/utils/**/*.ts"
],
"compilerOptions": {
"noEmit": true,
"target": "es6",
"sourceMap": true,
"strict": true,
"alwaysStrict": true,
"noImplicitThis": true,
"esModuleInterop": true,
"declaration": true,
"moduleResolution": "node",
"skipLibCheck": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"allowImportingTsExtensions": true,
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
],
"types": [
"node"
]
}
}

0 comments on commit c4b4b1a

Please sign in to comment.