I am trying to follow this tutorial to establish integration tests on our web application. Our stack currently includes Nexus, Next, Apollo, Prisma, and PostgreSQL.
I am using ApolloClient in place of GraphQLClient from graphql-request, I opted to use ApolloClient instead, especially since our web application is server less.
This is currently what I have inside the helper.ts, and the ApolloClient does work when I execute mutations. However, after executing a mutation on ApolloClient and checking if the data persists through Prisma, I get a null value.
Did I do these adjustments correctly? I am definitely missing something if Prisma is not querying correctly. Maybe there is a disconnect here between ApolloClient and Prisma or ApolloClient and the database? Any help would be much appreciated.
All of the code is below.
helper.ts
functiongraphqlTestContext(){letserverInstance: ServerInfo|null=null;return{asyncbefore(){constrootUrl=getRootUrl();consthttpLink=createHttpLink({uri: rootUrl+"api/graphql",credentials: "include",
fetch
});constclient=newApolloClient({// ssrMode: typeof window === "undefined",link: httpLink,cache: newInMemoryCache(),});returnclient;},asyncafter(){serverInstance?.server.close()},}}functionprismaTestContext(){constprismaBinary=join(__dirname,'../../','node_modules','.bin','prisma');letschema='';letdatabaseUrl='';letprismaClient: null|PrismaClient=null;return{asyncbefore(){// Generate a unique schema identifier for this test contextschema=`test_${nanoid()}`;// Generate the pg connection string for the test schemadatabaseUrl=`${process.env.ROOT_DB_URL}/testing?schema=${schema}`;// Set the required environment variable to contain the connection string// to our database test schemaprocess.env.DATABASE_URL=databaseUrl;// Run the migrations to ensure our schema has the required structureexecSync(`${prismaBinary} migrate dev`,{env: {
...process.env,DATABASE_URL: databaseUrl,},});// Construct a new Prisma Client connected to the generated Postgres schemaprismaClient=newPrismaClient();returnprismaClient;},asyncafter(){// Drop the schema after the tests have completedconstclient=newClient({connectionString: databaseUrl,});awaitclient.connect();awaitclient.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);awaitclient.end();// Release the Prisma Client connectionawaitprismaClient?.$disconnect();},}User.int.test.ts
constctx=createTestContext();describe("User",()=>{it("creates a new user with REGISTER_MUTATION",async()=>{constuserResult=awaitctx.client.mutate({mutation: gql` mutation Register( $firstName: String! $lastName: String! $email: String! $password: String! ) { registerUser( firstName: $firstName lastName: $lastName email: $email password: $password ) { user { email firstName } } } `,variables: {firstName: "FirstName",lastName: "LastName",email: "test@email.com",password: "password"}});expect(userResult).toMatchInlineSnapshot(`Object { "data": Object { "registerUser": Object { "__typename": "UserLoginPayload", "user": Object { "__typename": "User", "email": "test@email.com", "firstName": "FirstName", }, }, },}`);});it("verifies that user persists",async()=>{constpersistedData=awaitctx.prisma.user.findMany();expect(persistedData).toMatchInlineSnapshot(`Array []`);});});
I am trying to follow this tutorial to establish integration tests on our web application. Our stack currently includes Nexus, Next, Apollo, Prisma, and PostgreSQL.
I am using ApolloClient in place of
GraphQLClientfromgraphql-request, I opted to useApolloClientinstead, especially since our web application is server less.This is currently what I have inside the
helper.ts, and theApolloClientdoes work when I execute mutations. However, after executing a mutation on ApolloClient and checking if the data persists through Prisma, I get anullvalue.Did I do these adjustments correctly? I am definitely missing something if Prisma is not querying correctly. Maybe there is a disconnect here between ApolloClient and Prisma or ApolloClient and the database? Any help would be much appreciated.
All of the code is below.
helper.ts
User.int.test.ts