Search code examples
node.jsunit-testingintegration-testingprismats-jest

Can I apply 'setupFilesAfterEnv' to specific file in jest config?


I'm working on appling prisma unit testing and Integration testing

I want to apply unit testing for *.service.test.ts files and intergration testing for *.test.ts files.

I followed the Prisma document, but there is something that doesn't work.

  • singleton.ts
import { mockReset, mockDeep, DeepMockProxy } from "jest-mock-extended";
import { PrismaClient } from "@prisma/client";
import Prisma from "../src/db/prisma";

jest.mock("../src/db/prisma", () => {
  return {
    __esModule: true,
    default: mockDeep<PrismaClient>(),
  };
});

beforeEach(() => {
  // eslint-disable-next-line no-use-before-define
  mockReset(prismaMock);
});

export const prismaMock = Prisma as unknown as DeepMockProxy<PrismaClient>;

  • jest.config.ts

When turing off setupFilesAfterEnv option, testing *.test.ts files are working.

So I Want turn off setupFilesAfterEnv option in Integration testing

Is it applicable only when unit testing?

...

setupFilesAfterEnv: [
 "./jest/singleton.ts"

]

Solution

  • I think your question is a bit incomplete, but I might know what you are talking about because I am running into a similar problem.

    If you are trying to do the integration tests from prisma documentation, you need to unmock your prisma client on your integration tests. Otherwise it will still be mocked by your singleton.ts file

    something like this:

    myTest.test.js (integration test file)

    jest.unmock("../src/db/prisma");
    

    Another way of doing it, is just to remove the singleton from setupFilesAfterEnv and just import the prisma client from the singleton file inside your tests.

    What I did:

    I created 2 tests files (one for integration and another one for unit testing: CreateData.unit.test.ts and CreateData.int.test. I also created 2 singleton files:

    singleton.unit.ts (I wanted that to be applied on my unit tests)

    import { PrismaClient } from '@prisma/client';
    import { mockDeep, mockReset, DeepMockProxy, mock } from 'jest-mock-extended';
    import prismaClient from '../prismaClient';
    
    jest.mock('../prismaClient', () => ({
        __esModule: true,
        default: mockDeep<PrismaClient>(),
    }));
    
    beforeEach(() => {
        mockReset(prismaMock);
    });
    
    export const prismaMock = prismaClient as unknown as DeepMockProxy<PrismaClient>;
    

    singleton.int.ts (I wanted that applied in my integration tests)

    import prismaClient from '../prismaClient';
    
    afterAll(async () => {
        const deleteData = prismaClient.data.deleteMany();
    
        await prismaClient.$transaction([
            deleteData,
        ]);
    
        await prismaClient.$disconnect();
    });
    
    export { prismaClient }; 
    

    I removed setupFilesAfterEnv from jest.config.js

    Then create your unit tests and integration tests. You don't need to unmock prisma client if you removed the singleton from setupFilesAfterEnv in jest.config.ts

    myTest.unit.test.ts

    import { prismaMock } from "<path>/singleton.unit";
    import { CreateData } from "<path>/CreateData";
    
    let createData;
    let createDate = new Date();
    const data = {
        id: "randomId1234",
        name: "Bob Singer",
        email: "[email protected]",
        password: "123456",
    };
    beforeEach(() => {
        createData = new CreateData();
    });
    
    describe('CreateData', () => {
        it("should create new data", async () => {
            const result = createData.execute(data);
            prismaMock.data.create.mockResolvedValue(data);
    
            await expect(result).resolves.toEqual({
                id: "randomId1234",
                name: "Bob Singer",
                email: "[email protected]",
                password: "123456",
            });
        });
    });
    
    

    myTest.int.test.ts

    import prismaClient from "<path>/singleton.int";
    import { CreateData } from "<path>/CreateData"
    
    
    let createData;
    let createDate = new Date();
    const data = {
        id: "randomId1234",
        name: "Bob Singer",
        email: "[email protected]",
        password: "123456",
    };
    beforeEach(() => {
        createData = new CreateData();
    });
    
    describe('CreateTrainer', () => {
        it("should create new trainer", async () => {
            const result = await createData.execute(data);
            const newData = await prismaClient.data.findUnique({
                where: {
                    email: "[email protected]"
                }
            });
    
            console.log(result);
            expect(newData?.email).toEqual(data.email);
        });
    });