Dependency Injection Test Class with Vite #1623
Replies: 3 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
-
|
Ok, I have this Here is a test without a mock describe('Suite de test de Controlador de Procesos', () => {
let colaPrioritaria: ColaPrioritariaImp
let procesosController: ProcesosController
// En cada test creamos la cola
beforeEach(() => {
colaPrioritaria = new ColaPrioritariaImp()
procesosController = new ProcesosController(colaPrioritaria)
})
test('debería obtener todos los procesos respetando prioridad', () => {
const proceso1 = new Proceso(1, 'Proceso 1', 1)
const proceso2 = new Proceso(2, 'Proceso 2', 2)
colaPrioritaria.push(proceso1)
colaPrioritaria.push(proceso2)
const res = procesosController.getAll()
expect(res).toEqual([proceso2, proceso1])
assert.equal(res.length, 2)
assert.equal(res[0], proceso2)
assert.equal(res[1], proceso1)
assert.isTrue(res[0].prioridad >= res[1].prioridad)
})
})I try to make a mock of ColaPrioritaria I use TypeScript import { describe, test, assert, beforeEach, afterEach, expect, vi } from 'vitest'
import Proceso from '../../src/models/Proceso'
import ProcesosController from '../../src/controllers/ProcesosController'
import ColaPrioritariaImp from '../../src/repositories/ColaPrioritariaImp'
// import ProcesosException from '../../src/errors/ProcesosException'
vi.mock('../../src/repositories/ColaPrioritariaImp', () => {
const ColaPrioritariaImp = vi.fn(() => ({
getAll: vi.fn(() => [new Proceso('1', 'proceso 1', 1), new Proceso('2', 'proceso 2', 2)]),
}))
return { ColaPrioritariaImp }
})describe('DI Test', () => {
let cola: ColaPrioritariaImp
beforeEach(() => {
cola = new ColaPrioritariaImp()
})
afterEach(() => {
vi.clearAllMocks()
})
test('Foo', async () => {
const controller: ProcesosController = new ProcesosController(cola)
const result = await controller.getAll()
expect(cola.getAll).toBeCalledTimes(1)
expect(result.length).toBe(2)
})I have this error: I try to use vi.mocked |
Beta Was this translation helpful? Give feedback.
-
|
I try this and works But I am not very sure if it is a correct option, because I have tried others from the doc as I have put in previous messages and perhaps I am doing it wrong. I appreciate any help to improve myself. // Creamos nuestro objeto de mock completo de Cola Prioritaria
const ColaPrioritariaImp = vi.fn(() => ({
getAll: vi.fn(() => [new Proceso(2, 'proceso 2', 2), new Proceso(1, 'proceso 1', 1)]),
size: vi.fn(() => 2),
push: vi.fn(),
pop: vi.fn(() => new Proceso(1, 'proceso 1', 1)),
isEmpty: vi.fn(() => false),
getById: vi.fn(() => new Proceso(1, 'proceso 1', 1)),
}))
describe('Suite de test de Controlador de Procesos Mock Repositorio Cola Prioritaria', () => {
let colaPrioritaria: ColaPrioritaria
let procesosController: ProcesosController
beforeEach(() => {
colaPrioritaria = new ColaPrioritariaImp()
procesosController = new ProcesosController(colaPrioritaria)
})
afterEach(() => {
vi.clearAllMocks()
})
test('debería obtener todos los procesos respetando prioridad', () => {
const res = procesosController.getAll()
expect(colaPrioritaria.getAll).toBeCalledTimes(1)
expect(res.length).toBe(2)
assert.equal(res.length, 2)
assert.isTrue(res[0].prioridad >= res[1].prioridad)
})
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi friends
I would like to test a class that uses another class as dependence in its constructor.
My idea es mock this class and methods, pass the mock toe the class that I am testing and simulate the works similar that I do with mockito or jest, like this: https://stackoverflow.com/questions/55522669/jest-mocking-of-classes-with-di-dependencies
I will try, but I'm not getting it, maybe because I'm still not very expert in Vitest with Mocks.
I have check the doc:
https://vitest.dev/guide/mocking.html#globals
https://vitest.dev/api/#vi-mocked
I use TypeScript
Here it 's my code: https://github.com/joseluisgs/Prueba-Tecnica-Cola-Prioridad-Sin-Repetidos-Kotlin/blob/main/typescript/tests/controllers/ProcesosController.test.ts
Could you help me or give me any advice?
Thank you so much!
Beta Was this translation helpful? Give feedback.
All reactions