Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Aug 28, 2024
1 parent f70c3dd commit 09fc1e1
Show file tree
Hide file tree
Showing 7 changed files with 1,109 additions and 1,046 deletions.
2 changes: 1 addition & 1 deletion __tests__/pubsub.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('@PubSub Service Init', () => {
});

it('should call init', () => {
expect(SubService.init).toBeCalled();
expect(SubService.init).toHaveBeenCalled();
});

it('should override closeAll method', () => {
Expand Down
41 changes: 19 additions & 22 deletions __tests__/subscription.with-custom-credentials.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PubSubService, SubscriptionService } from '@honestfoodcompany/pubsub';
import { mocked } from 'ts-jest/utils';
import { SubscriberTuple } from '../src/subscriber';
import GooglePubSubAdapter from '../src/client/googlePubSub';
import { createProject } from '../src/client/googlePubSub/project';
Expand All @@ -15,18 +14,7 @@ jest.mock('../src/client/googlePubSub/project', () => {
};
});

const mockPublish = jest.fn();
const mockSubscribe = jest.fn();
const mockGet = jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
publish: mockPublish,
},
]);
});
});

const mockConstructor = jest.fn();
const mockClose = jest.fn();
const mockRemoveListeners = jest.fn();
Expand All @@ -47,9 +35,18 @@ jest.mock('@google-cloud/pubsub', () => {
return {
subscription: mockSubscription,
subscribe: mockSubscribe(config),
topic: jest.fn(() => ({
get: mockGet,
getSubscriptions: jest.fn(() => ['dummySub']),
topic: jest.fn((topicName) => ({
get: jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
name: topicName,
publish: jest.fn(),
getSubscriptions: jest.fn(() => ['dummySub']),
},
]);
});
}),
})),
};
}),
Expand Down Expand Up @@ -91,9 +88,9 @@ describe('With Project Credentials', (): void => {
const getProject = jest.spyOn(GooglePubSubAdapter.prototype, 'getProject');
await PubSubService.getInstance().subscribe(subscription);

expect(subscribe).toBeCalled();
expect(getProject).toBeCalledWith(subscription[1].options);
expect(mocked(createProject).mock.calls[1]).toEqual([
expect(subscribe).toHaveBeenCalled();
expect(getProject).toHaveBeenCalledWith(subscription[1].options);
expect(jest.mocked(createProject).mock.calls[1]).toEqual([
'google-pubsub-project-id',
{
credentials: {
Expand All @@ -111,13 +108,13 @@ describe('With Project Credentials', (): void => {
projectId: 'google-pubsub-project-id',
},
]);
expect(mockSubscription).toBeCalledTimes(1);
expect(mockSubscription).toHaveBeenCalledTimes(1);
});

it('should close existing subscription and skip not subscribed topics', async (): Promise<void> => {
await PubSubService.getInstance().closeAll();
expect(mockSubscription).toBeCalledTimes(1);
expect(mockClose).toBeCalledTimes(1);
expect(mockRemoveListeners).toBeCalledTimes(1);
expect(mockSubscription).toHaveBeenCalledTimes(1);
expect(mockClose).toHaveBeenCalledTimes(1);
expect(mockRemoveListeners).toHaveBeenCalledTimes(1);
});
});
16 changes: 8 additions & 8 deletions __tests__/topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ describe('topics', (): void => {
const spy = jest.spyOn(PubSubService, 'getInstance');
const topic = new TestTopic();
await topic.publish({ data: 'test' });
expect(spy).toBeCalledTimes(1);
expect(mockPublish).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledTimes(1);
});

it('Expect publish to return a string with the messageId', async (): Promise<void> => {
const spy = jest.spyOn(PubSubService, 'getInstance');
const topic = new TestTopic();
const data = await topic.publish({ data: 'test' });
expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(data).toBe('testid');
expect(mockPublish).toBeCalledTimes(1);
expect(mockPublish).toHaveBeenCalledTimes(1);
});

it('Should test retry config to be the default one', async (): Promise<void> => {
const spy = jest.spyOn(PubSubService.prototype, 'publish');
const topic = new TestTopic();
const data = await topic.publish({ data: 'test' });
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.objectContaining({
_timestamp: expect.stringContaining(':'),
Expand All @@ -64,7 +64,7 @@ describe('topics', (): void => {
const topic = new TestTopicNoTimeStamp();
const data = await topic.publish({ data: 'test' });
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopicNoTimeStamp,
expect.not.objectContaining({
_timestamp: expect.stringContaining(':'),
Expand All @@ -88,7 +88,7 @@ describe('topics', (): void => {
},
);
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.any(Object),
expect.objectContaining({
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('topics', (): void => {
{ attributes: { test: 'filter' } },
);
expect(data).toBe('testid');
expect(spy).toBeCalledWith(
expect(spy).toHaveBeenCalledWith(
TestTopic,
expect.any(Object),
expect.objectContaining({
Expand Down
30 changes: 15 additions & 15 deletions __tests__/topic.with-custom-credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import TestTopicWithProjectCredentials from './pubsub/topics/test-topic.with-cus
process.env.PUBSUB_DRIVER = 'google';

const mockPublish = jest.fn();
const mockGet = jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
publish: mockPublish,
publishJSON: mockPublish,
},
]);
});
});

const mockConstructor = jest.fn();
jest.mock('@google-cloud/pubsub', () => {
Expand All @@ -28,8 +18,18 @@ jest.mock('@google-cloud/pubsub', () => {
exists: jest.fn(() => true),
})),
subscribe: jest.fn(),
topic: jest.fn(() => ({
get: mockGet,
topic: jest.fn((topicName) => ({
get: jest.fn(() => {
return new Promise((resolve) => {
resolve([
{
name: topicName,
publish: mockPublish,
publishJSON: mockPublish,
},
]);
});
}),
})),
};
}),
Expand All @@ -41,20 +41,20 @@ describe('With Custom Credentials', (): void => {
const spy = jest.spyOn(PubSubService.prototype, 'publish');
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(spy).toBeCalled();
expect(spy).toHaveBeenCalled();
});

it('should call Google Driver publish method', async (): Promise<void> => {
const spy = jest.spyOn(GooglePubSubAdapter.prototype, 'publish');
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(spy).toBeCalled();
expect(spy).toHaveBeenCalled();
});

it('should call GooglePubSub publish method', async (): Promise<void> => {
const topic = new TestTopicWithProjectCredentials();
await topic.publish({ data: 'test' });
expect(mockPublish).toBeCalled();
expect(mockPublish).toHaveBeenCalled();
});

it('should have the project defined in projects', async () => {
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ process.env.PUBSUB_ROOT_DIR = './__tests__/pubsub';
process.env.PUBSUB_HEALTH_SERVER = 'false';

const TEST_REGEX = '/__tests__/.*.test.(js|ts)?$';
/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
// FIXME: Automatically clear mock calls and instances between every test
// clearMocks: true,
Expand All @@ -13,6 +16,7 @@ module.exports = {
'src/**/*.{js,ts}',
'!**/*.{generated,interface,test,data,enums}.{ts}',
'!**/*.d.ts',
'!src/interface/**',
'!src/__tests__/**',
'!**/node_modules/**',
],
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@docusaurus/preset-classic": "^2.0.0-beta.ff31de0ff",
"@types/bluebird": "^3.5.36",
"@types/cli-table": "^0.3.0",
"@types/jest": "^27.0.2",
"@types/jest": "^29.5.12",
"@types/node": "^16",
"@types/pako": "^2",
"@types/react": "^17.0.34",
Expand All @@ -70,14 +70,14 @@
"eslint-plugin-jest": "^24.5.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.2",
"jest": "^27.2.4",
"jest": "^29.7.0",
"lint-staged": "^11.2.0",
"nodemon": "^2.0.13",
"prettier": "^2.4.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.5",
"ts-jest": "^29.1.5",
"ts-node": "^10.2.1",
"tsconfig-paths": "^3.11.0",
"typedoc": "^0.22.7",
Expand Down
Loading

0 comments on commit 09fc1e1

Please sign in to comment.