Skip to content

Commit a66a573

Browse files
committed
test: fix fetch pollution and a flaky
1 parent 66f1169 commit a66a573

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

spec/ParseQuery.Comment.spec.js

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe_only_db('mongo')('Parse.Query with comment testing', () => {
4646
});
4747

4848
it('send comment with query through REST', async () => {
49-
const comment = 'Hello Parse';
49+
const comment = `Hello Parse ${Date.now()}`;
5050
const object = new TestObject();
5151
object.set('name', 'object');
5252
await object.save();
@@ -58,23 +58,55 @@ describe_only_db('mongo')('Parse.Query with comment testing', () => {
5858
},
5959
});
6060
await request(options);
61-
const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } });
61+
62+
// Wait for profile entry to appear with retry logic
63+
let result;
64+
const maxRetries = 10;
65+
const retryDelay = 100;
66+
for (let i = 0; i < maxRetries; i++) {
67+
result = await database.collection('system.profile').findOne(
68+
{ 'command.explain.comment': comment },
69+
{ sort: { ts: -1 } }
70+
);
71+
if (result) {
72+
break;
73+
}
74+
await new Promise(resolve => setTimeout(resolve, retryDelay));
75+
}
76+
77+
expect(result).toBeDefined();
6278
expect(result.command.explain.comment).toBe(comment);
6379
});
6480

6581
it('send comment with query', async () => {
66-
const comment = 'Hello Parse';
82+
const comment = `Hello Parse ${Date.now()}`;
6783
const object = new TestObject();
6884
object.set('name', 'object');
6985
await object.save();
7086
const collection = await config.database.adapter._adaptiveCollection('TestObject');
7187
await collection._rawFind({ name: 'object' }, { comment: comment });
72-
const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } });
88+
89+
// Wait for profile entry to appear with retry logic
90+
let result;
91+
const maxRetries = 10;
92+
const retryDelay = 100;
93+
for (let i = 0; i < maxRetries; i++) {
94+
result = await database.collection('system.profile').findOne(
95+
{ 'command.comment': comment },
96+
{ sort: { ts: -1 } }
97+
);
98+
if (result) {
99+
break;
100+
}
101+
await new Promise(resolve => setTimeout(resolve, retryDelay));
102+
}
103+
104+
expect(result).toBeDefined();
73105
expect(result.command.comment).toBe(comment);
74106
});
75107

76108
it('send a comment with a count query', async () => {
77-
const comment = 'Hello Parse';
109+
const comment = `Hello Parse ${Date.now()}`;
78110
const object = new TestObject();
79111
object.set('name', 'object');
80112
await object.save();
@@ -86,12 +118,28 @@ describe_only_db('mongo')('Parse.Query with comment testing', () => {
86118
const collection = await config.database.adapter._adaptiveCollection('TestObject');
87119
const countResult = await collection.count({ name: 'object' }, { comment: comment });
88120
expect(countResult).toEqual(2);
89-
const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } });
121+
122+
// Wait for profile entry to appear with retry logic
123+
let result;
124+
const maxRetries = 10;
125+
const retryDelay = 100;
126+
for (let i = 0; i < maxRetries; i++) {
127+
result = await database.collection('system.profile').findOne(
128+
{ 'command.comment': comment },
129+
{ sort: { ts: -1 } }
130+
);
131+
if (result) {
132+
break;
133+
}
134+
await new Promise(resolve => setTimeout(resolve, retryDelay));
135+
}
136+
137+
expect(result).toBeDefined();
90138
expect(result.command.comment).toBe(comment);
91139
});
92140

93141
it('attach a comment to an aggregation', async () => {
94-
const comment = 'Hello Parse';
142+
const comment = `Hello Parse ${Date.now()}`;
95143
const object = new TestObject();
96144
object.set('name', 'object');
97145
await object.save();
@@ -100,7 +148,23 @@ describe_only_db('mongo')('Parse.Query with comment testing', () => {
100148
explain: true,
101149
comment: comment,
102150
});
103-
const result = await database.collection('system.profile').findOne({}, { sort: { ts: -1 } });
151+
152+
// Wait for profile entry to appear with retry logic
153+
let result;
154+
const maxRetries = 10;
155+
const retryDelay = 100;
156+
for (let i = 0; i < maxRetries; i++) {
157+
result = await database.collection('system.profile').findOne(
158+
{ 'command.explain.comment': comment },
159+
{ sort: { ts: -1 } }
160+
);
161+
if (result) {
162+
break;
163+
}
164+
await new Promise(resolve => setTimeout(resolve, retryDelay));
165+
}
166+
167+
expect(result).toBeDefined();
104168
expect(result.command.explain.comment).toBe(comment);
105169
});
106170
});

spec/helper.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const { SpecReporter } = require('jasmine-spec-reporter');
77
const SchemaCache = require('../lib/Adapters/Cache/SchemaCache').default;
88
const { sleep, Connections } = require('../lib/TestUtils');
99

10+
const originalFetch = global.fetch;
11+
let fetchWasMocked = false;
12+
13+
global.restoreFetch = () => {
14+
global.fetch = originalFetch;
15+
fetchWasMocked = false;
16+
}
17+
18+
1019
// Ensure localhost resolves to ipv4 address first on node v17+
1120
if (dns.setDefaultResultOrder) {
1221
dns.setDefaultResultOrder('ipv4first');
@@ -205,6 +214,7 @@ const reconfigureServer = async (changedConfiguration = {}) => {
205214
};
206215

207216
beforeAll(async () => {
217+
global.restoreFetch();
208218
await reconfigureServer();
209219
Parse.initialize('test', 'test', 'test');
210220
Parse.serverURL = serverURL;
@@ -213,6 +223,11 @@ beforeAll(async () => {
213223
});
214224

215225
global.afterEachFn = async () => {
226+
// Restore fetch to prevent mock pollution between tests (only if it was mocked)
227+
if (fetchWasMocked) {
228+
global.restoreFetch();
229+
}
230+
216231
Parse.Cloud._removeAllHooks();
217232
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient();
218233
defaults.protectedFields = { _User: { '*': ['email'] } };
@@ -251,9 +266,8 @@ global.afterEachFn = async () => {
251266
afterEach(global.afterEachFn);
252267

253268
afterAll(() => {
254-
global.displayTestStats();
255-
// restore fetch
256269
global.restoreFetch();
270+
global.displayTestStats();
257271
});
258272

259273
const TestObject = Parse.Object.extend({
@@ -389,14 +403,9 @@ function mockShortLivedAuth() {
389403
return auth;
390404
}
391405

392-
const originalFetch = global.fetch;
393-
394-
global.restoreFetch = () => {
395-
global.fetch = originalFetch;
396-
}
397-
398406
function mockFetch(mockResponses) {
399407
const spy = jasmine.createSpy('fetch');
408+
fetchWasMocked = true; // Track that fetch was mocked for cleanup
400409

401410
global.fetch = (url, options = {}) => {
402411
// Allow requests to the Parse Server to pass through WITHOUT recording in spy

0 commit comments

Comments
 (0)