Sure, it’s more or less this. In fact, there’s another weird behavior that might be linked to the problem that I’ll go over below:
async function createSchema() {
// creates user and an Event object
const schemaRegistration = new Parse.Schema('Registration');
schemaRegistration.addPointer('userPointer', '_User');
schemaRegistration.addPointer('eventPointer', 'Event', { required: true });
await schemaRegistration.save();
});
async function makeUser (): Promise<Parse.User> {
const randomEmail = faker.internet.email();
const user = new Parse.User();
user.set('name', faker.name.findName());
user.set('email', randomEmail);
// other fields
return await user.signUp(null, { useMasterKey: true });
}
async function makeEvent (): Promise<Event> {
const event = new Event();
// some event.set() here
await event.save(null, { useMasterKey: true });
return event;
}
async function makeRegistration (user: Parse.User, event: FanfestEvent): Promise<Registration> {
const registration = new Registration();
registration.set('userPointer', user);
registration.set('eventPointer', event);
await registration.save(null, { useMasterKey: true });
return registration;
}
describe('notifications', () => {
it('test that breaks', async () => {
// several events are created here with different data
const event = makeEvent();
// test breaks if this is not run again here, see below
const serverURL = 'http://localhost:1337/parse';
Parse.initialize(process.env.APP_ID);
Parse.CoreManager.set('SERVER_URL', serverURL);
Parse.CoreManager.set('MASTER_KEY', process.env.MASTER_KEY);
const user = await makeUser();
const registration = await makeRegistration(user, event);
expect(registration).toBeTruthy();
);
});
If that Parse.initialize() code isn’t run again in the middle of the test, I get a Cannot use the Master Key, it has not been provided.
This is specific to when the user is created, all other events (4 instances are created) work perfectly well with useMasterKey
. If I comment the event code and run only the user creation I still get the same error. And yes, it has been properly initialized before.
console.log(user)
prints something like ParseUser { _objCount: 0, className: '_User', id: 'xoEs1IYVCZ' }
. That _objCount
seems weird, and the user object is not persisted on the database. I’m guessing this is related to the masterKey
issue above, but I’m at a complete loss on why this is happening.
Pretty much the exact same code runs perfectly well on a seeder script, which make me wonder if somehow Jest is getting in the way. I tried Parse.Object.disableSingleInstance();
on Jest’s initialization but there was no difference.