Hi, I’m interested to know if anyone has some good...
# orm-help
m
Hi, I’m interested to know if anyone has some good examples of resolver unit tests, preferably in typescript? The most challenging piece would seem to be mocking
prisma
on
context
. It might simply come down to needing to
@ts-ignore
every use of a mock prisma seeing as the type definition is pretty huge to replicate an instance of. Thanks!
a
Use the any type?
m
Yep, that’s a possibility. This is the best implementation I have found so far: https://github.com/javascript-af/javascript-af/blob/master/packages/backend/tests/repo.test.ts
v
Hello, I’m struggling with the same problem
I show u an example of my current approach
test-utils.ts
Copy code
import { graphql } from 'graphql'
import { makePrismaSchema } from 'nexus-prisma'

import datamodelInfo from '../types/generated/nexus-prisma'
import * as allTypes from '../schema'

interface Options {
  context?: {
    db?: any
    locale?: any
    request?: any
  }
  variables?: any
}

export const schema = makePrismaSchema({
  types: allTypes,
  prisma: {
    datamodelInfo,
    client: jest.fn(),
  },
} as any)

// Nice little helper function for tests
export const deathStar = (
  query: any,
  { context, variables }: Options = {},
  root = undefined
) => {
  return graphql(
    schema,
    query,
    root,
    {
      ...context,
    },
    variables
  )
}
resource.spec.ts
Copy code
import { deathStar } from 'test-utils'

  test("it hasn't activeSpeedFactory", async () => {
    const query = `
      query fetchSpeedy {
        speedfactory {
          id
        }
      }
    `

    const context = () => ({
      db: {
        configuration: () => ({
          activeSpeedFactory: () => null,
        }),
      },
      locale: <http://Locales.US|Locales.US>,
    })

    expect.hasAssertions()
    const { data, errors } = await deathStar(query, {
      context: context(),
    })

        expect(errors[0].message).toBe(
      `There isn't a valid activeSpeedfactory for ${
        <http://Locales.US|Locales.US>
      }, check your configuration!`
    )
    expect(errors).toMatchSnapshot()
h
There is an open FR which will eventually make this easier: https://github.com/prisma/prisma/issues/3898 But right now you can use the approach I am using in one of my side projects: https://github.com/javascript-af/javascript-af/blob/master/packages/backend/tests/upvote.test.ts In this approach I have jest mock functions for different relational stuff and I am making sure that they are getting resolved correctly.