Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% test coverage #89

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
},
"jest": {
"coverageDirectory": "<rootDir>/../coverage",
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 0
}
},
"preset": "ts-jest",
"rootDir": "src",
"testEnvironment": "node"
Expand Down
35 changes: 12 additions & 23 deletions src/ExpoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fetch, { Headers, Response as FetchResponse } from 'node-fetch';
import assert from 'node:assert';
import { Agent } from 'node:http';
import zlib from 'node:zlib';
import { gzipSync } from 'node:zlib';
import promiseLimit from 'promise-limit';
import promiseRetry from 'promise-retry';

Expand All @@ -29,14 +29,14 @@ export class Expo {
private limitConcurrentRequests: <T>(thunk: () => Promise<T>) => Promise<T>;
private accessToken: string | undefined;
private useFcmV1: boolean | undefined;
private retryMinTimeout: number;

constructor(options: ExpoClientOptions = {}) {
constructor(options: Partial<ExpoClientOptions> = {}) {
this.httpAgent = options.httpAgent;
this.limitConcurrentRequests = promiseLimit(
options.maxConcurrentRequests != null
? options.maxConcurrentRequests
: defaultConcurrentRequestLimit,
options.maxConcurrentRequests ?? defaultConcurrentRequestLimit,
);
this.retryMinTimeout = options.retryMinTimeout ?? requestRetryMinTimeout;
this.accessToken = options.accessToken;
this.useFcmV1 = options.useFcmV1;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export class Expo {
{
retries: 2,
factor: 2,
minTimeout: requestRetryMinTimeout,
minTimeout: this.retryMinTimeout,
},
);
});
Expand Down Expand Up @@ -218,7 +218,7 @@ export class Expo {
const json = JSON.stringify(options.body);
assert(json != null, `JSON request body must not be null`);
if (options.shouldCompress(json)) {
requestBody = await gzipAsync(Buffer.from(json));
requestBody = gzipSync(Buffer.from(json));
requestHeaders.set('Content-Encoding', 'gzip');
} else {
requestBody = json;
Expand Down Expand Up @@ -333,23 +333,12 @@ export class Expo {

export default Expo;

function gzipAsync(data: Buffer): Promise<Buffer> {
return new Promise((resolve, reject) => {
zlib.gzip(data, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}

export type ExpoClientOptions = {
httpAgent?: Agent;
maxConcurrentRequests?: number;
accessToken?: string;
useFcmV1?: boolean;
httpAgent: Agent;
maxConcurrentRequests: number;
retryMinTimeout: number;
accessToken: string;
useFcmV1: boolean;
};

export type ExpoPushToken = string;
Expand Down
2 changes: 1 addition & 1 deletion src/ExpoClientValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export const defaultConcurrentRequestLimit = 6;
/**
* Minimum timeout in ms for request retries.
*/
export const requestRetryMinTimeout = process.env['NODE_ENV'] === 'test' ? 1 : 1000;
export const requestRetryMinTimeout = 1000;
4 changes: 2 additions & 2 deletions src/__tests__/ExpoClient-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('sending push notification messages', () => {
{ repeat: 3 },
);

const client = new ExpoClient();
const client = new ExpoClient({ retryMinTimeout: 1 });
const ticketPromise = client.sendPushNotificationsAsync([]);

const rejection = expect(ticketPromise).rejects;
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('sending push notification messages', () => {
)
.mock(sendApiUrl, { data: mockTickets }, { overwriteRoutes: false });

const client = new ExpoClient();
const client = new ExpoClient({ retryMinTimeout: 1 });
await expect(client.sendPushNotificationsAsync([{ to: 'a' }, { to: 'b' }])).resolves.toEqual(
mockTickets,
);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"outDir": "./build",
"rootDir": "./src"
},
"exclude": ["node_modules", "**/__mocks__", "**/__tests__", "build"]
"exclude": ["node_modules", "**/__mocks__", "**/__tests__", "build", "coverage"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"composite": true,
"noEmit": true
},
"exclude": ["build"]
"exclude": ["build", "coverage"]
}