Es un caso de uso común para simular módulos ES.
por ejemplo, tengo un módulo que importa getEventByEventId
import getEventByEventId from '../queries/getEventByEventId';
export default async (
{
connection,
session
}: ResolverContextType
) => {
if (!session || !session.userId) {
throw new Error('User must be authenticated.');
}
const event = await getEventByEventId(connection, eventId);
// ..
}
En mi prueba quiero burlarme de getEventByEventId, por ejemplo
import test from 'ava';
import sinon from 'sinon';
import moment from 'moment';
import createReservation from '../../../src/mutators/createReservation';
test('throws an error if the event is in the past', async (t) => {
const parameters: any = {};
const context: any = {
session: {
userId: 1
}
};
const stub = sinon.stub().returns({
date: moment().format('YYYY-MM-DD'),
time: moment(new Date().getTime() - 1000 * 60).format('HH:mm')
});
// How to use the stub to mock `getEventByEventId`?
await t.throws(createReservation(context), 'Cannot create a reservation for a past event.');
});
Esto se preguntó antes aquí: https://github.com/sinonjs/sinon/issues/1358 . Pero esto se cerró incorrectamente como un duplicado de: https://github.com/sinonjs/sinon/issues/1121 , mientras que es un problema totalmente diferente.
Esto ya se mencionó en ese número, pero parece que ya no se atiende.
Sinon.JS no se ocupa de los sistemas de módulos. Estoy cerrando esto.
Debido a las muchas solicitudes que siguen llegando, y a que los administradores del proyecto también escriben pruebas para ganarse la vida, hemos publicado Cómo usar conexiones de enlaces con CommonJS .
Ayúdenos a mantener la lista de problemas manejable y haga preguntas de uso en la lista de StackOverflow . Ambos son buenos recursos donde una audiencia más amplia de usuarios puede ofrecer ayuda.
@sebakerckhof revisa esto https://github.com/sinonjs/sinon/issues/562#issuecomment -399090111