Sinon: 存根默认导出函数

创建于 2017-11-16  ·  2评论  ·  资料来源: sinonjs/sinon

模拟 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 :
这在那个问题中已经提到过,但它似乎不再受到关注。

ES2015+

所有2条评论

Sinon.JS 不关心模块系统。 我正在关闭这个。

由于不断收到许多请求,并且该项目的管理员也以编写测试为生,因此我们发布了How To Use Link Seams With CommonJS

请帮助我们保持问题列表的可管理性,并在Sinon.JS 用户邮件列表StackOverflow上提出使用问题。 两者都是很好的资源,更广泛的用户可以提供帮助。

@sebakerckhof结帐这个https://github.com/sinonjs/sinon/issues/562#issuecomment -399090111

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

kevinburkeshyp picture kevinburkeshyp  ·  4评论

brettz9 picture brettz9  ·  3评论

NathanHazout picture NathanHazout  ·  3评论

akdor1154 picture akdor1154  ·  4评论

andys8 picture andys8  ·  4评论