ESモジュールをモックするのは一般的な使用例です。
たとえば、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);
// ..
}
私のテストでは、getEventByEventIdをモックしたいと思います。
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.');
});
これは前にここで尋ねられました: https : https://github.com/sinonjs/sinon/issues/1121の複製として不当に閉鎖されましたが、まったく別の問題です。
これはその号ですでに言及されていますが、もう世話をされていないようです。
Sinon.JSは、モジュールシステムとは関係ありません。 これを閉じます。
多くのリクエストが寄せられており、プロジェクトのスチュワードも生計を立てるためのテストを作成しているため、 Link Seams WithCommonJSの使用方法を公開しました。
問題リストを管理しやすくし、 Sinon.JSユーザーのメーリングリストまたはStackOverflowで使用法に関する質問をするのを手伝ってください。 どちらも、幅広いユーザーがヘルプを提供できる優れたリソースです。
@sebakerckhofチェックアウトこのhttps://github.com/sinonjs/sinon/issues/562#issuecomment-399090111