A
A
Andrey Rudakov2022-01-16 10:34:17
Unit testing
Andrey Rudakov, 2022-01-16 10:34:17

How to use a test base (not the main one) in NetsJS with TypeORM in unit tests?

Greetings.
Before running the tests, I start the connection to the test database:

beforeEach(() => {
    return createConnection({
        type: "sqlite",
        database: 'testBase",
        entities: [User, SmsCodes],
        ...........
});
afterEach(() => {
    let conn = getConnection();
    return conn.close();
});


Next, I have a function that works with a real base and I need to test it:
it('Возврат: true при правильном коде SMS', async () => {
            const usersService = UsersService;
            await getRepository(SmsCodes).insert({
                email: '[email protected]',
                code: 1234,
            });
            const checkCorrectSms = await usersService.checkCorrectSmsCode('[email protected]', 1234); 
// Та самая тестируемая функция
            expect(checkCorrectSms).toBe(true);
        })

The function under test must refer to the test base, not the real one. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Rudakov, 2022-01-16
@DEMETRA-WORK

I figured it out, here's the solution for anyone who needs it:
You need to pass the test repository to the service constructor, where this function is declared.

describe('Users Service Test /services/users.service.ts', () => {
    let usersService: UsersService;
    let smsCodesRepository: Repository<SmsCodes>;
    let testingModule: TestingModule;

    const testConnectionName = 'testConnection';

    beforeEach(async () => {
        testingModule = await Test.createTestingModule({
            providers: [
                UsersService,
                {
                    provide: getRepositoryToken(SmsCodes),
                    useClass: Repository,
                },
            ],
        }).compile();

        let connection = await createConnection({
            type: "postgres",
            database: "baseTest",
            dropSchema: true,
            entities: [SmsCodes],
            synchronize: true,
            name: testConnectionName
        });

        smsCodesRepository = getRepository(SmsCodes, testConnectionName);
        usersService = new UsersService(userRepository, smsCodesRepository);

        return connection;
    });

    afterEach(async () => {
        await getConnection(testConnectionName).close()
    });

    describe('checkCorrectSmsCode', () => {
        it('Возврат: true при правильном коде SMS', async () => {
            const mockEmail = '[email protected]'
            const mockCode = 1234;
            await smsCodesRepository.insert({
                email: mockEmail,
                code: mockCode,
            });
            const checkCorrectSms = await usersService.checkCorrectSmsCode(mockEmail, mockCode);
            expect(checkCorrectSms).toBe(true);
        })
    })
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question