Skip to content

Commit

Permalink
chore(ddp-sdk): ts-jest -> swc/jest (#29380)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored May 30, 2023
1 parent 9f2f718 commit d19fed6
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/api-client/__tests__/2fahandling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test('if the 2fa handler is not provided, it should throw an error', async () =>

expect(error.status).toBe(400);

const body = error.json();
const body = await error.json();

expect(body).toMatchObject({
errorType: 'totp-required',
Expand Down Expand Up @@ -128,7 +128,7 @@ test('if the 2fa handler is provided it should resolves', async () => {
expect(fn).toHaveBeenCalledTimes(1);
});

test.only('should be ask for 2fa code again if the code is wrong', async () => {
test('should be ask for 2fa code again if the code is wrong', async () => {
const fn = jest.fn();

const client = new RestClient({
Expand Down
3 changes: 3 additions & 0 deletions packages/api-client/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export default {
errorOnDeprecated: true,
testEnvironment: 'jsdom',
modulePathIgnorePatterns: ['<rootDir>/dist/'],
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
moduleNameMapper: {
'\\.css$': 'identity-obj-proxy',
},
Expand Down
5 changes: 3 additions & 2 deletions packages/api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"version": "0.0.1",
"private": true,
"devDependencies": {
"@swc/core": "^1.3.60",
"@swc/jest": "^0.2.26",
"@types/jest": "~29.5.0",
"@types/strict-uri-encode": "^2.0.0",
"eslint": "^8.29.0",
"jest": "~29.5.0",
"jest": "^29.5.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "~29.0.5",
"typescript": "~5.0.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ddp-client/__tests__/ClientStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('call procedures', () => {
expect(callback).toBeCalledWith(null, ['arg1', 'arg2']);
});

it('should be able to handle errors thrown by the method call', async () => {
it('should be able to handle errors thrown by the method call', async () => {
const callback = jest.fn();
const ws = new MinimalDDPClient(() => undefined);

Expand Down
2 changes: 1 addition & 1 deletion packages/ddp-client/__tests__/Connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ it('should queue messages if the connection is not ready', async () => {

expect(connection.queue.size).toBe(0);

await handleMethod(server, '1', 'method', ['arg1', 'arg2']);
await handleMethod(server, 'method', ['arg1', 'arg2']);
});

it('should throw an error if a reconnect is called while a connection is in progress', async () => {
Expand Down
89 changes: 81 additions & 8 deletions packages/ddp-client/__tests__/DDPSDK.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable no-debugger */
import util from 'util';

import WS from 'jest-websocket-mock';
import { WebSocket } from 'ws';

import { DDPSDK } from '../src/DDPSDK';
import { fireStreamChange, fireStreamAdded, fireStreamRemove, handleConnection, handleSubscription } from './helpers';
import { fireStreamChange, fireStreamAdded, fireStreamRemove, handleConnection, handleSubscription, handleMethod } from './helpers';

(global as any).WebSocket = (global as any).WebSocket || WebSocket;

Expand All @@ -25,7 +27,9 @@ beforeEach(async () => {
});

afterEach(() => {
return WS.clean();
server.close();
WS.clean();
jest.useRealTimers();
});

it('should handle a stream of messages', async () => {
Expand Down Expand Up @@ -108,18 +112,17 @@ it('should handle streams after reconnect', async () => {

// Fake timers are used to avoid waiting for the reconnect timeout
jest.useFakeTimers();
await server.close();
await WS.clean();
server.close();
WS.clean();

server = new WS('ws://localhost:1234');

const reconnect = new Promise((resolve) => sdk.connection.once('reconnecting', () => resolve(undefined)));
const connecting = new Promise((resolve) => sdk.connection.once('connecting', () => resolve(undefined)));
const connected = new Promise((resolve) => sdk.connection.once('connected', () => resolve(undefined)));
await handleConnection(server, server.connected, Promise.resolve(jest.advanceTimersByTimeAsync(1000)), reconnect, connecting, connected);

await jest.advanceTimersByTimeAsync(1000);
await handleConnection(server, jest.advanceTimersByTimeAsync(1000), reconnect, connecting, connected);

// the client should reconnect and resubscribe
await handleSubscription(server, result.id, streamName, streamParams);

fire(server, streamName, streamParams);
Expand Down Expand Up @@ -188,8 +191,78 @@ it('should handle an unsubscribe stream after reconnect', async () => {

expect(sdk.client.subscriptions.size).toBe(0);
jest.useRealTimers();
sdk.connection.close();
});

it('should create and connect to a stream', async () => {
await handleConnection(server, DDPSDK.createAndConnect('ws://localhost:1234'));
const promise = DDPSDK.createAndConnect('ws://localhost:1234');
await handleConnection(server, promise);
const sdk = await promise;
sdk.connection.close();
});

describe('Method call and Disconnection cases', () => {
it('should handle properly if the message was sent after disconnection', async () => {
const sdk = DDPSDK.create('ws://localhost:1234');

await handleConnection(server, sdk.connection.connect());

const [result] = await handleMethod(server, 'method', ['args1'], sdk.client.callAsync('method', 'args1'));

expect(result).toBe(1);
// Fake timers are used to avoid waiting for the reconnect timeout
jest.useFakeTimers();

server.close();
WS.clean();
server = new WS('ws://localhost:1234');

const reconnect = new Promise((resolve) => sdk.connection.once('reconnecting', () => resolve(undefined)));
const connecting = new Promise((resolve) => sdk.connection.once('connecting', () => resolve(undefined)));
const connected = new Promise((resolve) => sdk.connection.once('connected', () => resolve(undefined)));

const callResult = sdk.client.callAsync('method', 'args2');

expect(util.inspect(callResult).includes('pending')).toBe(true);

await handleConnection(server, jest.advanceTimersByTimeAsync(1000), reconnect, connecting, connected);

const [result2] = await handleMethod(server, 'method', ['args2'], callResult);

expect(util.inspect(callResult).includes('pending')).toBe(false);
expect(result2).toBe(1);
sdk.connection.close();
jest.useRealTimers();
});

it.skip('should handle properly if the message was sent before disconnection but got disconnected before receiving the response', async () => {
const sdk = DDPSDK.create('ws://localhost:1234');

await handleConnection(server, sdk.connection.connect());

const callResult = sdk.client.callAsync('method', 'args');

expect(util.inspect(callResult).includes('pending')).toBe(true);

// Fake timers are used to avoid waiting for the reconnect timeout
jest.useFakeTimers();

server.close();
WS.clean();
server = new WS('ws://localhost:1234');

const reconnect = new Promise((resolve) => sdk.connection.once('reconnecting', () => resolve(undefined)));
const connecting = new Promise((resolve) => sdk.connection.once('connecting', () => resolve(undefined)));
const connected = new Promise((resolve) => sdk.connection.once('connected', () => resolve(undefined)));

await handleConnection(server, jest.advanceTimersByTimeAsync(1000), reconnect, connecting, connected);

expect(util.inspect(callResult).includes('pending')).toBe(true);

const [result] = await handleMethod(server, 'method', ['args2'], callResult);

expect(result).toBe(1);

jest.useRealTimers();
});
});
22 changes: 18 additions & 4 deletions packages/ddp-client/__tests__/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,24 @@ export const handleConnectionAndRejects = async (server: WS, ...client: Promise<
]);
};

export const handleMethod = async (server: WS, id: string, method: string, params: string[]) => {
await server.nextMessage.then(async (message) => {
await expect(message).toBe(`{"msg":"method","method":"${method}","params":${JSON.stringify(params)},"id":"${id}"}`);
server.send(`{"msg":"result","id":"${id}","result":1}`);
const handleConnectionButNoResponse = async (server: WS, method: string, params: string[]) => {
return server.nextMessage.then(async (msg) => {
if (typeof msg !== 'string') throw new Error('Expected message to be a string');
const message = JSON.parse(msg);
await expect(message).toMatchObject({
msg: 'method',
method,
params,
});
return message;
});
};

export const handleMethod = async (server: WS, method: string, params: string[], ...client: Promise<unknown>[]) => {
const result = await handleConnectionButNoResponse(server, method, params);
return Promise.all([server.send(`{"msg":"result","id":"${result.id}","result":1}`), ...client]).then((result) => {
result.shift();
return result;
});
};

Expand Down
3 changes: 3 additions & 0 deletions packages/ddp-client/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export default {
testEnvironment: 'jsdom',
modulePathIgnorePatterns: ['<rootDir>/dist/'],
testMatch: ['**/**.spec.ts'],
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
moduleNameMapper: {
'\\.css$': 'identity-obj-proxy',
},
Expand Down
7 changes: 4 additions & 3 deletions packages/ddp-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "0.0.1",
"private": true,
"devDependencies": {
"@swc/core": "^1.3.60",
"@swc/jest": "^0.2.26",
"@types/jest": "^29.5.0",
"@types/ws": "^8",
"eslint": "^8.12.0",
"jest": "~29.5.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "~29.5.0",
"jest-websocket-mock": "^2.4.0",
"ts-jest": "~29.0.5",
"typescript": "~5.0.2",
"typescript": "~5.0.4",
"ws": "^8.13.0"
},
"peerDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions packages/gazzodown/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ export default {
errorOnDeprecated: true,
testEnvironment: 'jsdom',
modulePathIgnorePatterns: ['<rootDir>/dist/'],
transform: {
'^.+\\.(ts|tsx)$': [
'@swc/jest',
{
sourceMaps: true,
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
decorators: false,
dynamicImport: false,
},
transform: {
react: {
runtime: 'automatic',
},
},
},
},
],
},
moduleNameMapper: {
'\\.css$': 'identity-obj-proxy',
},
Expand Down
3 changes: 3 additions & 0 deletions packages/gazzodown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@storybook/manager-webpack4": "~6.5.16",
"@storybook/react": "~6.5.15",
"@storybook/testing-library": "~0.0.13",
"@swc/core": "^1.3.60",
"@swc/jest": "^0.2.26",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "~12.1.5",
"@types/babel__core": "^7.1.20",
Expand Down Expand Up @@ -49,6 +51,7 @@
"scripts": {
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx . --fix",
"testunit": "jest",
"test": "jest",
"build": "rm -rf dist && tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput",
Expand Down
Loading

0 comments on commit d19fed6

Please sign in to comment.