refactor: remove sinon (#42337)
This commit is contained in:
committed by
GitHub
parent
7d1e9af9df
commit
802273cf8a
@ -1,7 +1,5 @@
|
||||
/* global describe xdescribe it expect */
|
||||
/* global describe xdescribe it expect jest */
|
||||
import { first, find } from 'lodash';
|
||||
import sinon from 'sinon';
|
||||
import { mockReq, mockRes } from 'sinon-express-mock';
|
||||
|
||||
import {
|
||||
buildUserUpdate,
|
||||
@ -23,6 +21,22 @@ import {
|
||||
mockCompletedChallenges
|
||||
} from './fixtures';
|
||||
|
||||
export const mockReq = opts => {
|
||||
const req = {};
|
||||
return { ...req, ...opts };
|
||||
};
|
||||
|
||||
export const mockRes = opts => {
|
||||
const res = {};
|
||||
res.status = jest.fn().mockReturnValue(res);
|
||||
res.json = jest.fn().mockReturnValue(res);
|
||||
res.redirect = jest.fn().mockReturnValue(res);
|
||||
res.set = jest.fn().mockReturnValue(res);
|
||||
res.clearCookie = jest.fn().mockReturnValue(res);
|
||||
res.cookie = jest.fn().mockReturnValue(res);
|
||||
return { ...res, ...opts };
|
||||
};
|
||||
|
||||
describe('boot/challenge', () => {
|
||||
xdescribe('backendChallengeCompleted', () => {});
|
||||
|
||||
@ -221,37 +235,35 @@ describe('boot/challenge', () => {
|
||||
const validObjectId = '5c716d1801013c3ce3aa23e6';
|
||||
|
||||
it('declares a 403 for an invalid id in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: { id: 'not-a-real-id' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('declares a 403 for an invalid challengeType in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: { id: validObjectId, challengeType: 'ponyfoo' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('declares a 403 for an invalid solution in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: {
|
||||
id: validObjectId,
|
||||
@ -260,13 +272,12 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls next if the body is valid', () => {
|
||||
@ -278,11 +289,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls next if only the id is provided', () => {
|
||||
@ -292,11 +303,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('can handle an "int" challengeType', () => {
|
||||
@ -307,11 +318,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@ -337,10 +348,10 @@ describe('boot/challenge', () => {
|
||||
);
|
||||
const req = mockReq();
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
|
||||
expect(res.redirect.calledWith(mockLearnUrl));
|
||||
expect(res.redirect).toHaveBeenCalledWith(mockLearnUrl);
|
||||
done();
|
||||
});
|
||||
|
||||
@ -362,10 +373,10 @@ describe('boot/challenge', () => {
|
||||
user: mockUser
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
|
||||
expect(res.redirect.calledWith(expectedUrl)).toBe(true);
|
||||
expect(res.redirect).toHaveBeenCalledWith(expectedUrl);
|
||||
done();
|
||||
});
|
||||
|
||||
@ -386,10 +397,10 @@ describe('boot/challenge', () => {
|
||||
user: { ...mockUser, currentChallengeId: '' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
const expectedUrl = `${mockHomeLocation}${firstChallengeUrl}`;
|
||||
expect(res.redirect.calledWith(expectedUrl)).toBe(true);
|
||||
expect(res.redirect).toHaveBeenCalledWith(expectedUrl);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,5 @@
|
||||
/* global describe it expect */
|
||||
import sinon from 'sinon';
|
||||
import { mockReq as mockRequest, mockRes } from 'sinon-express-mock';
|
||||
/* global describe it expect jest */
|
||||
import { mockReq as mockRequest, mockRes } from '../boot_tests/challenge.test';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import { homeLocation } from '../../../../config/env';
|
||||
@ -110,11 +109,11 @@ describe('request-authorization', () => {
|
||||
expect.assertions(2);
|
||||
const req = mockReq({ path: '/some-path/that-needs/auth' });
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is required for this request'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when the access token is invalid', () => {
|
||||
@ -126,12 +125,12 @@ describe('request-authorization', () => {
|
||||
cookie: { jwt_access_token: invalidJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is invalid'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when the access token has expired', () => {
|
||||
@ -146,12 +145,12 @@ describe('request-authorization', () => {
|
||||
cookie: { jwt_access_token: invalidJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is no longer valid'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('adds the user to the request object', async done => {
|
||||
@ -163,9 +162,9 @@ describe('request-authorization', () => {
|
||||
cookie: { jwt_access_token: validJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req).toHaveProperty('user');
|
||||
expect(req.user).toEqual(users['456def']);
|
||||
return done();
|
||||
@ -179,9 +178,9 @@ describe('request-authorization', () => {
|
||||
cookie: { jwt_access_token: validJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(res.set.calledWith('X-fcc-access-token', validJWT)).toBe(true);
|
||||
expect(res.set).toHaveBeenCalledWith('X-fcc-access-token', validJWT);
|
||||
return done();
|
||||
});
|
||||
|
||||
@ -189,9 +188,9 @@ describe('request-authorization', () => {
|
||||
// currently /unsubscribe does not require authorization
|
||||
const req = mockReq({ path: '/unsubscribe/another/route' });
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@ -200,11 +199,11 @@ describe('request-authorization', () => {
|
||||
expect.assertions(2);
|
||||
const req = mockReq({ path: '/some-path/that-needs/auth' });
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is required for this request'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when the access token is invalid', () => {
|
||||
@ -215,12 +214,12 @@ describe('request-authorization', () => {
|
||||
headers: { 'X-fcc-access-token': invalidJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is invalid'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when the access token has expired', () => {
|
||||
@ -234,12 +233,12 @@ describe('request-authorization', () => {
|
||||
headers: { 'X-fcc-access-token': invalidJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
expect(() => requestAuthorization(req, res, next)).toThrowError(
|
||||
'Access token is no longer valid'
|
||||
);
|
||||
expect(next.called).toBe(false);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('adds the user to the request object', async done => {
|
||||
@ -250,9 +249,9 @@ describe('request-authorization', () => {
|
||||
headers: { 'X-fcc-access-token': validJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(req).toHaveProperty('user');
|
||||
expect(req.user).toEqual(users['456def']);
|
||||
return done();
|
||||
@ -266,9 +265,9 @@ describe('request-authorization', () => {
|
||||
cookie: { jwt_access_token: validJWT }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(res.set.calledWith('X-fcc-access-token', validJWT)).toBe(true);
|
||||
expect(res.set).toHaveBeenCalledWith('X-fcc-access-token', validJWT);
|
||||
return done();
|
||||
});
|
||||
|
||||
@ -276,9 +275,9 @@ describe('request-authorization', () => {
|
||||
// currently /unsubscribe does not require authorization
|
||||
const req = mockReq({ path: '/unsubscribe/another/route' });
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await requestAuthorization(req, res, next);
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
setAccessTokenToResponse,
|
||||
removeCookies
|
||||
} from './getSetAccessToken';
|
||||
import { mockReq, mockRes } from 'sinon-express-mock';
|
||||
import { mockReq, mockRes } from '../boot_tests/challenge.test';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
describe('getSetAccessToken', () => {
|
||||
@ -130,7 +130,8 @@ describe('getSetAccessToken', () => {
|
||||
|
||||
setAccessTokenToResponse({ accessToken }, req, res, validJWTSecret);
|
||||
|
||||
expect(res.cookie.getCall(0).args).toEqual([
|
||||
expect(res.cookie).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'jwt_access_token',
|
||||
expectedJWT,
|
||||
{
|
||||
@ -138,7 +139,7 @@ describe('getSetAccessToken', () => {
|
||||
domain,
|
||||
maxAge: accessToken.ttl
|
||||
}
|
||||
]);
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -152,16 +153,18 @@ describe('getSetAccessToken', () => {
|
||||
|
||||
removeCookies(req, res);
|
||||
|
||||
expect(res.clearCookie.getCall(0).args).toEqual([
|
||||
expect(res.clearCookie).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'jwt_access_token',
|
||||
jwtOptions
|
||||
]);
|
||||
expect(res.clearCookie.getCall(1).args).toEqual([
|
||||
);
|
||||
expect(res.clearCookie).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'access_token',
|
||||
jwtOptions
|
||||
]);
|
||||
expect(res.clearCookie.getCall(2).args).toEqual(['userId', jwtOptions]);
|
||||
expect(res.clearCookie.getCall(3).args).toEqual(['_csrf', jwtOptions]);
|
||||
);
|
||||
expect(res.clearCookie).toHaveBeenNthCalledWith(3, 'userId', jwtOptions);
|
||||
expect(res.clearCookie).toHaveBeenNthCalledWith(4, '_csrf', jwtOptions);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,5 @@
|
||||
/* global describe beforeEach expect it */
|
||||
/* global describe beforeEach expect it jest */
|
||||
import inMemoryCache from './in-memory-cache';
|
||||
import sinon from 'sinon';
|
||||
|
||||
describe('InMemoryCache', () => {
|
||||
let reportErrorStub;
|
||||
@ -10,7 +9,7 @@ describe('InMemoryCache', () => {
|
||||
const emptyCacheValue = null;
|
||||
|
||||
beforeEach(() => {
|
||||
reportErrorStub = sinon.spy();
|
||||
reportErrorStub = jest.fn();
|
||||
});
|
||||
|
||||
it('throws if no report function is passed as a second argument', () => {
|
||||
@ -43,7 +42,6 @@ describe('InMemoryCache', () => {
|
||||
});
|
||||
|
||||
it('reports errors thrown from the update function', () => {
|
||||
const reportErrorStub = sinon.spy();
|
||||
const cache = inMemoryCache(before, reportErrorStub);
|
||||
|
||||
const updateError = new Error('An update error');
|
||||
@ -52,7 +50,7 @@ describe('InMemoryCache', () => {
|
||||
};
|
||||
|
||||
cache.update(updateThatThrows);
|
||||
expect(reportErrorStub.calledWith(updateError)).toBe(true);
|
||||
expect(reportErrorStub).toHaveBeenCalledWith(updateError);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
/* global describe it expect afterAll */
|
||||
/* global describe it expect jest */
|
||||
import moment from 'moment-timezone';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import {
|
||||
prepUniqueDaysByHours,
|
||||
@ -10,13 +9,13 @@ import {
|
||||
} from './user-stats';
|
||||
import { mockUserID, mockApp, mockUser } from '../boot_tests/fixtures';
|
||||
|
||||
// setting now to 2016-02-03T11:00:00 (PST)
|
||||
const clock = sinon.useFakeTimers(1454526000000);
|
||||
jest.useFakeTimers('modern');
|
||||
const PST = 'America/Los_Angeles';
|
||||
|
||||
describe('user stats', () => {
|
||||
afterAll(() => {
|
||||
clock.restore();
|
||||
beforeEach(() => {
|
||||
// setting now to 2016-02-03T11:00:00 (PST)
|
||||
jest.setSystemTime(1454526000000);
|
||||
});
|
||||
|
||||
describe('prepUniqueDaysByHours', () => {
|
||||
|
Reference in New Issue
Block a user