Skip to content

Commit f537e67

Browse files
authored
test: Fix flaky tests (#10320)
1 parent 7a1b11b commit f537e67

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

spec/ParseGraphQLController.spec.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe('ParseGraphQLController', () => {
1111
let databaseController;
1212
let cacheController;
1313
let databaseUpdateArgs;
14+
let originalDbFind;
15+
let originalDbUpdate;
1416

1517
// Holds the graphQLConfig in memory instead of using the db
1618
let graphQLConfigRecord;
@@ -34,17 +36,18 @@ describe('ParseGraphQLController', () => {
3436
databaseController = parseServer.config.databaseController;
3537
cacheController = parseServer.config.cacheController;
3638

37-
const defaultFind = databaseController.find.bind(databaseController);
39+
originalDbFind = databaseController.find.bind(databaseController);
40+
originalDbUpdate = databaseController.update.bind(databaseController);
41+
3842
databaseController.find = async (className, query, ...args) => {
3943
if (className === GraphQLConfigClassName && isEqual(query, { objectId: GraphQLConfigId })) {
4044
const graphQLConfigRecord = getConfigFromDb();
4145
return graphQLConfigRecord ? [graphQLConfigRecord] : [];
4246
} else {
43-
return defaultFind(className, query, ...args);
47+
return originalDbFind(className, query, ...args);
4448
}
4549
};
4650

47-
const defaultUpdate = databaseController.update.bind(databaseController);
4851
databaseController.update = async (className, query, update, fullQueryOptions) => {
4952
databaseUpdateArgs = [className, query, update, fullQueryOptions];
5053
if (
@@ -57,13 +60,20 @@ describe('ParseGraphQLController', () => {
5760
) {
5861
setConfigOnDb(update[GraphQLConfigKey]);
5962
} else {
60-
return defaultUpdate(...databaseUpdateArgs);
63+
return originalDbUpdate(...databaseUpdateArgs);
6164
}
6265
};
6366
}
6467
databaseUpdateArgs = null;
6568
});
6669

70+
afterAll(() => {
71+
if (databaseController) {
72+
databaseController.find = originalDbFind;
73+
databaseController.update = originalDbUpdate;
74+
}
75+
});
76+
6777
describe('constructor', () => {
6878
it('should require a databaseController', () => {
6979
expect(() => new ParseGraphQLController()).toThrow(

spec/ParseGraphQLServer.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8658,6 +8658,13 @@ describe('ParseGraphQLServer', () => {
86588658
});
86598659

86608660
describe('Data Types', () => {
8661+
beforeEach(async () => {
8662+
const schema = new Parse.Schema('SomeClass');
8663+
await schema.purge().catch(() => {});
8664+
await schema.delete().catch(() => {});
8665+
await parseGraphQLServer.parseGraphQLSchema.schemaCache.clear();
8666+
});
8667+
86618668
it('should support String', async () => {
86628669
try {
86638670
const someFieldValue = 'some string';
@@ -10423,6 +10430,7 @@ describe('ParseGraphQLServer', () => {
1042310430
schema.addPointer('somePointerField', 'SomeClass');
1042410431
schema.addRelation('someRelationField', 'SomeClass');
1042510432
await schema.save();
10433+
await parseGraphQLServer.parseGraphQLSchema.schemaCache.clear();
1042610434

1042710435
const body = new FormData();
1042810436
body.append(

spec/rest.spec.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,8 @@ describe('rest create', () => {
653653
password: 'zxcv',
654654
foo: 'bar',
655655
};
656-
const now = new Date();
656+
const defaultSessionLength = 1000 * 3600 * 24 * 365;
657+
const before = Date.now();
657658

658659
rest
659660
.create(config, auth.nobody(config), '_User', user)
@@ -670,10 +671,11 @@ describe('rest create', () => {
670671
expect(r.results.length).toEqual(1);
671672

672673
const session = r.results[0];
673-
const actual = new Date(session.expiresAt.iso);
674-
const expected = new Date(now.getTime() + 1000 * 3600 * 24 * 365);
674+
const actual = new Date(session.expiresAt.iso).getTime();
675+
const after = Date.now();
675676

676-
expect(Math.abs(actual - expected) <= jasmine.DEFAULT_TIMEOUT_INTERVAL).toEqual(true);
677+
expect(actual).toBeGreaterThanOrEqual(before + defaultSessionLength);
678+
expect(actual).toBeLessThanOrEqual(after + defaultSessionLength);
677679

678680
done();
679681
});
@@ -685,9 +687,9 @@ describe('rest create', () => {
685687
password: 'zxcv',
686688
foo: 'bar',
687689
};
688-
const sessionLength = 3600, // 1 Hour ahead
689-
now = new Date(); // For reference later
690+
const sessionLength = 3600; // 1 Hour ahead
690691
config.sessionLength = sessionLength;
692+
const before = Date.now();
691693

692694
rest
693695
.create(config, auth.nobody(config), '_User', user)
@@ -704,10 +706,11 @@ describe('rest create', () => {
704706
expect(r.results.length).toEqual(1);
705707

706708
const session = r.results[0];
707-
const actual = new Date(session.expiresAt.iso);
708-
const expected = new Date(now.getTime() + sessionLength * 1000);
709+
const actual = new Date(session.expiresAt.iso).getTime();
710+
const after = Date.now();
709711

710-
expect(Math.abs(actual - expected) <= jasmine.DEFAULT_TIMEOUT_INTERVAL).toEqual(true);
712+
expect(actual).toBeGreaterThanOrEqual(before + sessionLength * 1000);
713+
expect(actual).toBeLessThanOrEqual(after + sessionLength * 1000);
711714

712715
done();
713716
})
@@ -717,38 +720,27 @@ describe('rest create', () => {
717720
});
718721
});
719722

720-
it('can create a session with no expiration', done => {
723+
it('can create a session with no expiration', async () => {
724+
await reconfigureServer({ expireInactiveSessions: false });
725+
config = Config.get('test');
726+
721727
const user = {
722728
username: 'asdf',
723729
password: 'zxcv',
724730
foo: 'bar',
725731
};
726-
config.expireInactiveSessions = false;
727-
728-
rest
729-
.create(config, auth.nobody(config), '_User', user)
730-
.then(r => {
731-
expect(Object.keys(r.response).length).toEqual(3);
732-
expect(typeof r.response.objectId).toEqual('string');
733-
expect(typeof r.response.createdAt).toEqual('string');
734-
expect(typeof r.response.sessionToken).toEqual('string');
735-
return rest.find(config, auth.master(config), '_Session', {
736-
sessionToken: r.response.sessionToken,
737-
});
738-
})
739-
.then(r => {
740-
expect(r.results.length).toEqual(1);
741732

742-
const session = r.results[0];
743-
expect(session.expiresAt).toBeUndefined();
733+
const r = await rest.create(config, auth.nobody(config), '_User', user);
734+
expect(Object.keys(r.response).length).toEqual(3);
735+
expect(typeof r.response.objectId).toEqual('string');
736+
expect(typeof r.response.createdAt).toEqual('string');
737+
expect(typeof r.response.sessionToken).toEqual('string');
744738

745-
done();
746-
})
747-
.catch(err => {
748-
console.error(err);
749-
fail(err);
750-
done();
751-
});
739+
const s = await rest.find(config, auth.master(config), '_Session', {
740+
sessionToken: r.response.sessionToken,
741+
});
742+
expect(s.results.length).toEqual(1);
743+
expect(s.results[0].expiresAt).toBeUndefined();
752744
});
753745

754746
it('can create object in volatileClasses if masterKey', done => {

0 commit comments

Comments
 (0)