Skip to content

Commit d97d89e

Browse files
committed
sdk-core: fix unit tests
1 parent 7f2983a commit d97d89e

File tree

4 files changed

+81
-106
lines changed

4 files changed

+81
-106
lines changed

packages/sdk-core/src/modules/database/BacktraceDatabase.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ export class BacktraceDatabase implements BacktraceModule {
375375
}
376376

377377
private async setupDatabaseAutoSend() {
378+
if (!this._enabled) {
379+
return;
380+
}
381+
378382
if (this._options?.autoSend === false) {
379383
return;
380384
}

packages/sdk-core/tests/database/databaseSetupTests.spec.ts

Lines changed: 64 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import crypto from 'crypto';
22
import { nextTick } from 'process';
33
import { promisify } from 'util';
44
import { AttachmentBacktraceDatabaseRecord, BacktraceData, ReportBacktraceDatabaseRecord } from '../../src/index.js';
5-
import { RequestBacktraceReportSubmission } from '../../src/model/http/BacktraceReportSubmission.js';
65
import { BacktraceDatabase } from '../../src/modules/database/BacktraceDatabase.js';
76
import { BacktraceDatabaseContext } from '../../src/modules/database/BacktraceDatabaseContext.js';
8-
import { TEST_SUBMISSION_URL } from '../mocks/BacktraceTestClient.js';
9-
import { testHttpClient } from '../mocks/testHttpClient.js';
7+
import { BacktraceTestClient } from '../mocks/BacktraceTestClient.js';
108
import { getTestStorageProvider } from '../mocks/testStorageProvider.js';
119

1210
function randomReportRecord(): ReportBacktraceDatabaseRecord {
@@ -35,16 +33,7 @@ function randomAttachmentRecord(): AttachmentBacktraceDatabaseRecord {
3533
describe('Database setup tests', () => {
3634
it('The database should be disabled by default', () => {
3735
const testStorageProvider = getTestStorageProvider();
38-
const database = new BacktraceDatabase(
39-
undefined,
40-
testStorageProvider,
41-
new RequestBacktraceReportSubmission(
42-
{
43-
url: TEST_SUBMISSION_URL,
44-
},
45-
testHttpClient,
46-
),
47-
);
36+
const database = new BacktraceDatabase(undefined, testStorageProvider);
4837

4938
expect(database.enabled).toBeFalsy();
5039
});
@@ -56,37 +45,30 @@ describe('Database setup tests', () => {
5645
autoSend: false,
5746
},
5847
testStorageProvider,
59-
new RequestBacktraceReportSubmission(
60-
{
61-
url: TEST_SUBMISSION_URL,
62-
},
63-
testHttpClient,
64-
),
6548
);
66-
67-
const databaseStartResult = database.initialize();
68-
69-
expect(databaseStartResult).toBeTruthy();
70-
expect(database.enabled).toBeTruthy();
49+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
50+
try {
51+
const databaseStartResult = database.initialize();
52+
53+
expect(databaseStartResult).toBeTruthy();
54+
expect(database.enabled).toBeTruthy();
55+
} finally {
56+
client.dispose();
57+
}
7158
});
7259

7360
it('Should not enable the database if the enable option is set to false', () => {
7461
const testStorageProvider = getTestStorageProvider();
75-
const database = new BacktraceDatabase(
76-
{ enable: false },
77-
testStorageProvider,
78-
new RequestBacktraceReportSubmission(
79-
{
80-
url: TEST_SUBMISSION_URL,
81-
},
82-
testHttpClient,
83-
),
84-
);
85-
86-
const databaseStartResult = database.initialize();
87-
88-
expect(databaseStartResult).toBeFalsy();
89-
expect(database.enabled).toBeFalsy();
62+
const database = new BacktraceDatabase({ enable: false }, testStorageProvider);
63+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
64+
try {
65+
const databaseStartResult = database.initialize();
66+
67+
expect(databaseStartResult).toBeFalsy();
68+
expect(database.enabled).toBeFalsy();
69+
} finally {
70+
client.dispose();
71+
}
9072
});
9173

9274
it('Should not enable the database if the storage is not prepared', () => {
@@ -97,12 +79,6 @@ describe('Database setup tests', () => {
9779
path: '/path/to/fake/dir',
9880
},
9981
testStorageProvider,
100-
new RequestBacktraceReportSubmission(
101-
{
102-
url: TEST_SUBMISSION_URL,
103-
},
104-
testHttpClient,
105-
),
10682
);
10783
jest.spyOn(testStorageProvider, 'start').mockReturnValue(false);
10884

@@ -114,35 +90,22 @@ describe('Database setup tests', () => {
11490

11591
it('Should be disabled after disposing database', () => {
11692
const testStorageProvider = getTestStorageProvider();
117-
const database = new BacktraceDatabase(
118-
undefined,
119-
testStorageProvider,
120-
new RequestBacktraceReportSubmission(
121-
{
122-
url: TEST_SUBMISSION_URL,
123-
},
124-
testHttpClient,
125-
),
126-
);
93+
const database = new BacktraceDatabase(undefined, testStorageProvider);
94+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
12795

128-
database.initialize();
129-
database.dispose();
96+
try {
97+
database.initialize();
98+
database.dispose();
13099

131-
expect(database.enabled).toBeFalsy();
100+
expect(database.enabled).toBeFalsy();
101+
} finally {
102+
client.dispose();
103+
}
132104
});
133105

134106
it('Should not add a record to disabled database', () => {
135107
const testStorageProvider = getTestStorageProvider();
136-
const database = new BacktraceDatabase(
137-
undefined,
138-
testStorageProvider,
139-
new RequestBacktraceReportSubmission(
140-
{
141-
url: TEST_SUBMISSION_URL,
142-
},
143-
testHttpClient,
144-
),
145-
);
108+
const database = new BacktraceDatabase(undefined, testStorageProvider);
146109

147110
const result = database.add({} as BacktraceData, []);
148111
expect(result).toBeFalsy();
@@ -160,23 +123,22 @@ describe('Database setup tests', () => {
160123
autoSend: false,
161124
},
162125
testStorageProvider,
163-
new RequestBacktraceReportSubmission(
164-
{
165-
url: TEST_SUBMISSION_URL,
166-
},
167-
testHttpClient,
168-
),
169126
);
127+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
170128

171-
const databaseStartResult = database.initialize();
129+
try {
130+
const databaseStartResult = database.initialize();
172131

173-
const nextTickAsync = promisify(nextTick);
174-
await nextTickAsync();
132+
const nextTickAsync = promisify(nextTick);
133+
await nextTickAsync();
175134

176-
expect(databaseStartResult).toBeTruthy();
177-
expect(database.enabled).toBeTruthy();
135+
expect(databaseStartResult).toBeTruthy();
136+
expect(database.enabled).toBeTruthy();
178137

179-
expect(contextLoad).toHaveBeenCalledWith(reports);
138+
expect(contextLoad).toHaveBeenCalledWith(reports);
139+
} finally {
140+
client.dispose();
141+
}
180142
});
181143

182144
it('should add no more than maximumNumberOfRecords reports from storage on initialize', async () => {
@@ -193,23 +155,22 @@ describe('Database setup tests', () => {
193155
maximumNumberOfRecords: 2,
194156
},
195157
testStorageProvider,
196-
new RequestBacktraceReportSubmission(
197-
{
198-
url: TEST_SUBMISSION_URL,
199-
},
200-
testHttpClient,
201-
),
202158
);
159+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
203160

204-
const databaseStartResult = database.initialize();
161+
try {
162+
const databaseStartResult = database.initialize();
205163

206-
const nextTickAsync = promisify(nextTick);
207-
await nextTickAsync();
164+
const nextTickAsync = promisify(nextTick);
165+
await nextTickAsync();
208166

209-
expect(databaseStartResult).toBeTruthy();
210-
expect(database.enabled).toBeTruthy();
167+
expect(databaseStartResult).toBeTruthy();
168+
expect(database.enabled).toBeTruthy();
211169

212-
expect(contextLoad).toHaveBeenCalledWith(expected);
170+
expect(contextLoad).toHaveBeenCalledWith(expected);
171+
} finally {
172+
client.dispose();
173+
}
213174
});
214175

215176
it('should limit report records by maximumNumberOfRecords and attachment records by maximumNumberOfAttachmentRecords', async () => {
@@ -241,22 +202,21 @@ describe('Database setup tests', () => {
241202
maximumNumberOfAttachmentRecords: 3,
242203
},
243204
testStorageProvider,
244-
new RequestBacktraceReportSubmission(
245-
{
246-
url: TEST_SUBMISSION_URL,
247-
},
248-
testHttpClient,
249-
),
250205
);
206+
const client = BacktraceTestClient.buildFakeClient(undefined, undefined, undefined, undefined, [database]);
251207

252-
const databaseStartResult = database.initialize();
208+
try {
209+
const databaseStartResult = database.initialize();
253210

254-
const nextTickAsync = promisify(nextTick);
255-
await nextTickAsync();
211+
const nextTickAsync = promisify(nextTick);
212+
await nextTickAsync();
256213

257-
expect(databaseStartResult).toBeTruthy();
258-
expect(database.enabled).toBeTruthy();
214+
expect(databaseStartResult).toBeTruthy();
215+
expect(database.enabled).toBeTruthy();
259216

260-
expect(contextLoad).toHaveBeenCalledWith(expected);
217+
expect(contextLoad).toHaveBeenCalledWith(expected);
218+
} finally {
219+
client.dispose();
220+
}
261221
});
262222
});

packages/sdk-core/tests/mocks/BacktraceTestClient.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BacktraceConfiguration,
55
BacktraceCoreClient,
66
BacktraceDatabaseStorageProvider,
7+
BacktraceModule,
78
BacktraceRequestHandler,
89
FileSystem,
910
} from '../../src/index.js';
@@ -22,6 +23,7 @@ export class BacktraceTestClient extends BacktraceCoreClient {
2223
attributeProviders: BacktraceAttributeProvider[] = [],
2324
attachments: BacktraceAttachment[] = [],
2425
fileSystem?: FileSystem,
26+
modules?: BacktraceModule[],
2527
) {
2628
super({
2729
options: {
@@ -42,6 +44,7 @@ export class BacktraceTestClient extends BacktraceCoreClient {
4244
requestHandler: handler,
4345
attributeProviders,
4446
fileSystem,
47+
modules,
4548
});
4649
this.requestHandler = handler;
4750
}
@@ -51,6 +54,7 @@ export class BacktraceTestClient extends BacktraceCoreClient {
5154
attributeProviders: BacktraceAttributeProvider[] = [],
5255
attachments: BacktraceAttachment[] = [],
5356
fileSystem?: FileSystem,
57+
modules?: BacktraceModule[],
5458
) {
5559
attributeProviders.push({
5660
type: 'scoped',
@@ -61,7 +65,14 @@ export class BacktraceTestClient extends BacktraceCoreClient {
6165
};
6266
},
6367
});
64-
const instance = new BacktraceTestClient(options, testHttpClient, attributeProviders, attachments, fileSystem);
68+
const instance = new BacktraceTestClient(
69+
options,
70+
testHttpClient,
71+
attributeProviders,
72+
attachments,
73+
fileSystem,
74+
modules,
75+
);
6576
instance.initialize();
6677
return instance;
6778
}

packages/sdk-core/tests/mocks/testHttpClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { BacktraceReportSubmissionResult, BacktraceRequestHandler } from '../../
22

33
export const testHttpClient: BacktraceRequestHandler = {
44
post: jest.fn().mockResolvedValue(Promise.resolve(BacktraceReportSubmissionResult.Ok('Ok'))),
5-
postError: jest.fn().mockResolvedValue(Promise.resolve()),
5+
postError: jest.fn().mockResolvedValue(Promise.resolve(BacktraceReportSubmissionResult.Ok('Ok'))),
66
};

0 commit comments

Comments
 (0)