Testing Cloud Code


I’m currently setting up a fresh Parse project (piggy backing off a previous setup) which has Jest installed for testing.

I’m currently writing a bunch of a integration tests, to try out a multi-tenant setup…

We’re using Typescript (for better or worse - I’m very much still learning it)… Anyway, In our project, we have a models.ts file where we define interfaces for what we want our classes to look like. Then, we have a dao.ts file where we extend the Parse object and register the models as sub classes.

I’m grouping functionality into specific files - e.g. tenant, roles, user, etc, into a auth.ts file and here I import what I need from my dao file. - e.g. of dao file:

import { Tenant } from './models'

export class TenantDAO extends Parse.Object {
  constructor(attributes: Tenant) {
    super('Tenant', attributes)

When I import this auth file into my cloud code file and make queries via the api, everything works as expected. E.g. I can make parse queries and use the master key in the cloud code and it works.

Now, in my test file, I’m setting parse up along with the usual config - e.g.:

import Parse from 'parse/node'
import { ParseObjects, TenantDAO, UserDAO } from '../../src/dao'

describe('API Integration (CRUD) Tests', () => {
  beforeEach(() => {
    ;(Parse as any).serverURL = `${config.parse.serverUrl}/parse`
    ;(Parse as any).masterKey = config.parse.masterKey
    ;(Parse as any).cloud = `../../src/${config.parse.cloudFunctions}`
    ParseObjects.register() // registering my Parse objects...

I’m importing Parse here, I’m registering my Parse Objects, but when I run the tests, I get a Parse is not defined error, pointing to where I’m extending Parse in my dao file.

I thought all good, I would just import Parse here. When I do, the tests seem to run, but then it complains about the lack of the master key.

My guess is that I’m missing something related to how I’m initialising Parse, but I’m just not quite sure.

I’d really appreciate any guidance.


Okay, I feel a bit stupid, so I’ll add this here for anyone else that has a brain fart like I had :smiley:

Your cloud code is executed within the context of the parse server instance during the request/response life cycle, so it makes sense that it has access to things like the master key…

What I was doing in the example above was importing those dao objects into the test file directly - i.e. outside of the parse instance…

So, if I make network requests to interact with my cloud code (as in the real world), the dao objects are automatically loaded in the correct context and work as they should.

I wish I’d spent a bit more time looking into test setups before I jump into it (I did, but because I inherited something that “worked”, I didn’t spend the time that I should have).

I’ve come across a few nice examples which I will use to update what I currently have.

For those that find this post, take a look at these two examples - hopefully you’ll save yourself some pain :smiley:

I hope this helps someone else.