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://github.com/sinonjs/sinon/issues/1358 ์ด์ ์ ์ง๋ฌธ๋์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ https://github.com/sinonjs/sinon/issues/1121 ์ ๋ณต์ ๋ณธ์ผ๋ก ์๋ชป ๋ซํ์ง๋ง ์์ ํ ๋ค๋ฅธ ๋ฌธ์ ์
๋๋ค.
์ด๊ฒ์ ์ด๋ฏธ ๊ทธ ๋ฌธ์ ์์ ์ธ๊ธ๋์์ง๋ง ๋ ์ด์ ๋๋ณด์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
Sinon.JS๋ ๋ชจ๋ ์์คํ ๊ณผ ๊ด๋ จ์ด ์์ต๋๋ค. ๋๋ ์ด๊ฒ์ ๋ซ๋๋ค.
๊ณ์ํด์ ๋ค์ด์ค๋ ๋ง์ ์์ฒญ๊ณผ ํ๋ก์ ํธ ์คํ์ด๋๋ ์๊ณ๋ฅผ ์ํด ํ ์คํธ๋ฅผ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ CommonJS์ ์ฐ๊ฒฐ ์ด์์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ถํํ์ต๋๋ค.
๋ฌธ์ ๋ชฉ๋ก์ ๊ด๋ฆฌํ ์ ์๋๋ก ์ ์งํ๊ณ Sinon.JS ์ฌ์ฉ์ ๋ฉ์ผ๋ง ๋ชฉ๋ก ๋๋ StackOverflow ์์ ์ฌ์ฉ ์ง๋ฌธ์ ํ๋๋ก ๋์์ฃผ์ธ์. ๋ ๋ค ๋ ๋ง์ ์ฌ์ฉ์๊ฐ ๋์์ ์ค ์ ์๋ ์ข์ ๋ฆฌ์์ค์ ๋๋ค.
@sebakerckhof ์ฒดํฌ์์ https://github.com/sinonjs/sinon/issues/562#issuecomment -399090111