Hi folks, we are running into various jest problem...
# orm-help
j
Hi folks, we are running into various jest problems with our prisma. It seems each test-file is creating a new instance of prisma, leading to a major heap size after a while. The prisma instances do not seem to be closed or removed either. Since we can’t share globals in jest (meaning we can’t have a single instance of prisma), did anyone experience this, and figure out a way to circumvent this problem? Example of a jest file:
Copy code
// Intantiation class in a /src/utils/makeTestPrisma.ts
export const makeTestPrisma = () => {
    if (process.env.NODE_ENV === 'test') {
      const prisma = global.testPrisma || new PrismaClient({
        datasources: { postgresql: { url: 'OUR_URL' } },
      });
  
      global.testPrisma = prisma;
  
      return prisma;
    }
  
    throw new Error('Not possible; only works in Test environment!');
  }


// In a test file, /src/Users/User.test.ts

const prisma = makeTestPrisma();

test(async () => {
   const res = await services.doSomeStuffInDb(prisma);

   expect(res.users).toEqual(2);
});
@Daan Helsloot
a
Can you do something like
Copy code
let primsa;

beforeEach(() => {
  makeTestPrisma();
});

afterEach(() => {
  prisma.disconnect()
});
d
Hi! That's what we already did @alexwasik but such queries are ran before EVERY test script and therefore still causes memory leaks because disconnect doesn't clean up everything. We want to run something ONCE and share that between every test
a
is it necessary to actually query the db in a test? can you create mock data?