Skip to content

Commit

Permalink
chore(jest): use jest-jasmine2 test runner instead
Browse files Browse the repository at this point in the history
At least until jestjs/jest#10529 lands.
  • Loading branch information
enisdenjo committed Jun 11, 2021
1 parent c8a3735 commit d321473
Show file tree
Hide file tree
Showing 5 changed files with 440 additions and 505 deletions.
1 change: 1 addition & 0 deletions jest.config.js
@@ -1,5 +1,6 @@
module.exports = {
testEnvironment: 'node',
testRunner: 'jest-jasmine2',
moduleFileExtensions: ['ts', 'js'],
testRegex: '/tests/.+.ts$',
testPathIgnorePatterns: ['/node_modules/', '/fixtures/', '/utils/'],
Expand Down
187 changes: 86 additions & 101 deletions src/tests/client.ts
Expand Up @@ -11,7 +11,7 @@ import {
stringifyMessage,
SubscribePayload,
} from '../common';
import { startWSTServer as startTServer, waitForDone } from './utils';
import { startWSTServer as startTServer } from './utils';

// simulate browser environment for easier client testing
beforeEach(() => {
Expand Down Expand Up @@ -167,31 +167,28 @@ it('should not accept invalid WebSocket implementations', async () => {
).toThrow();
});

it(
'should recieve optional connection ack payload in event handler',
waitForDone(async (done) => {
const { url } = await startTServer({
onConnect: () => ({ itsa: 'me' }),
});
it('should recieve optional connection ack payload in event handler', async (done) => {
const { url } = await startTServer({
onConnect: () => ({ itsa: 'me' }),
});

createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
on: {
connected: (_socket, payload) => {
try {
expect(payload).toEqual({ itsa: 'me' });
} catch (err) {
fail(err);
}
done();
},
createClient({
url,
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
on: {
connected: (_socket, payload) => {
try {
expect(payload).toEqual({ itsa: 'me' });
} catch (err) {
fail(err);
}
done();
},
});
}),
);
},
});
});

it('should close with error message during connecting issues', async () => {
const { url } = await startTServer();
Expand Down Expand Up @@ -379,32 +376,29 @@ it('should use a custom JSON message reviver function', async () => {
});
});

it(
'should use a custom JSON message replacer function',
waitForDone(async (done) => {
const { url, waitForClient } = await startTServer();
it('should use a custom JSON message replacer function', async (done) => {
const { url, waitForClient } = await startTServer();

createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
jsonMessageReplacer: (key, value) => {
if (key === 'type') {
return 'CONNECTION_INIT';
}
return value;
},
});
createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
jsonMessageReplacer: (key, value) => {
if (key === 'type') {
return 'CONNECTION_INIT';
}
return value;
},
});

await waitForClient((client) => {
client.onMessage((data) => {
expect(data).toBe('{"type":"CONNECTION_INIT"}');
done();
});
await waitForClient((client) => {
client.onMessage((data) => {
expect(data).toBe('{"type":"CONNECTION_INIT"}');
done();
});
}),
);
});
});

describe('ping/pong', () => {
it('should respond with a pong to a ping', async () => {
Expand Down Expand Up @@ -520,29 +514,26 @@ describe('ping/pong', () => {
}, 20);
});

it(
'should ping the server after the keepAlive timeout',
waitForDone(async (done) => {
const { url, waitForConnect, waitForClient } = await startTServer();
it('should ping the server after the keepAlive timeout', async (done) => {
const { url, waitForConnect, waitForClient } = await startTServer();

createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
keepAlive: 20,
});
createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
keepAlive: 20,
});

await waitForConnect();
await waitForConnect();

await waitForClient((client) => {
client.onMessage((data) => {
expect(data).toBe('{"type":"ping"}');
done();
});
await waitForClient((client) => {
client.onMessage((data) => {
expect(data).toBe('{"type":"ping"}');
done();
});
}),
);
});
});
});

describe('query operation', () => {
Expand Down Expand Up @@ -951,26 +942,23 @@ describe('lazy', () => {
await server.waitForClientClose();
});

it(
'should report errors to the `onNonLazyError` callback',
waitForDone(async (done) => {
const { url, ...server } = await startTServer();
it('should report errors to the `onNonLazyError` callback', async (done) => {
const { url, ...server } = await startTServer();

createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: (err) => {
expect((err as CloseEvent).code).toBe(1005);
done();
},
});
createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: (err) => {
expect((err as CloseEvent).code).toBe(1005);
done();
},
});

await server.waitForClient((client) => {
client.close();
});
}),
);
await server.waitForClient((client) => {
client.close();
});
});

it('should not close connection if a subscription is disposed multiple times', async () => {
const { url, ...server } = await startTServer();
Expand Down Expand Up @@ -1543,26 +1531,23 @@ describe('events', () => {
});
});

it(
'should emit closed event when disposing',
waitForDone(async (done) => {
const { url, waitForClient } = await startTServer();
it('should emit closed event when disposing', async (done) => {
const { url, waitForClient } = await startTServer();

const client = createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
on: {
closed: () => done(),
},
});
const client = createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
on: {
closed: () => done(),
},
});

await waitForClient();
await waitForClient();

client.dispose();
}),
);
client.dispose();
});

it('should emit the websocket connection error', (done) => {
expect.assertions(3);
Expand Down

0 comments on commit d321473

Please sign in to comment.