1b8752e595
This commit updates the preview server tests to take advantage of features supported in the latest version of Jasmine that were not supported when the rests were first written. Changes include: - Use [async/await] in tests. - Use the new [toBeInstanceOf()] and [toHaveBeenCalledBefore()] matchers. - Use the new [toBeResolved()] and [toBeRejected()] async matchers (and their variants). - Use the new [withArgs()] method of `Spy` to simplify "trained" responses. - Use the new [resolveTo()]/[rejectWith()] methods of `SpyStrategy` (and their variants) to simplify promise-based spies. - Implement custom async matchers (via [addAsyncMatchers()]) to simplify certain tests. [addAsyncMatchers()]: https://jasmine.github.io/api/3.5/jasmine.html#.addAsyncMatchers [async/await]: https://jasmine.github.io/tutorials/async [rejectWith()]: https://jasmine.github.io/api/3.5/SpyStrategy.html#rejectWith [resolveTo()]: https://jasmine.github.io/api/3.5/SpyStrategy.html#resolveTo [toBeInstanceOf()]: https://jasmine.github.io/api/3.5/matchers.html#toBeInstanceOf [toBeRejected()]: https://jasmine.github.io/api/3.5/async-matchers.html#toBeRejected [toBeResolved()]: https://jasmine.github.io/api/3.5/async-matchers.html#toBeResolved [toHaveBeenCalledBefore()]: https://jasmine.github.io/api/3.5/matchers.html#toHaveBeenCalledBefore [withArgs()]: https://jasmine.github.io/api/3.5/Spy.html#withArgs PR Close #36837
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import * as express from 'express';
|
|
import {PreviewServerError} from '../../lib/preview-server/preview-error';
|
|
import {respondWithError, throwRequestError} from '../../lib/preview-server/utils';
|
|
|
|
describe('preview-server/utils', () => {
|
|
describe('respondWithError', () => {
|
|
let endSpy: jasmine.Spy;
|
|
let statusSpy: jasmine.Spy;
|
|
let response: express.Response;
|
|
|
|
beforeEach(() => {
|
|
endSpy = jasmine.createSpy('end');
|
|
statusSpy = jasmine.createSpy('status').and.callFake(() => response);
|
|
response = {status: statusSpy, end: endSpy} as any;
|
|
});
|
|
|
|
it('should set the status on the response', () => {
|
|
respondWithError(response, new PreviewServerError(505, 'TEST MESSAGE'));
|
|
expect(statusSpy).toHaveBeenCalledWith(505);
|
|
expect(endSpy).toHaveBeenCalledWith('TEST MESSAGE', jasmine.any(Function));
|
|
});
|
|
|
|
it('should convert non-PreviewServerError errors to 500 PreviewServerErrors', () => {
|
|
respondWithError(response, new Error('OTHER MESSAGE'));
|
|
expect(statusSpy).toHaveBeenCalledWith(500);
|
|
expect(endSpy).toHaveBeenCalledWith('OTHER MESSAGE', jasmine.any(Function));
|
|
});
|
|
});
|
|
|
|
describe('throwRequestError', () => {
|
|
it('should throw a suitable error', () => {
|
|
let caught = false;
|
|
try {
|
|
const request = {
|
|
body: 'The request body',
|
|
method: 'POST',
|
|
originalUrl: 'some.domain.com/path',
|
|
} as express.Request;
|
|
throwRequestError(505, 'ERROR MESSAGE', request);
|
|
} catch (error) {
|
|
caught = true;
|
|
expect(error).toBeInstanceOf(PreviewServerError);
|
|
expect(error.status).toEqual(505);
|
|
expect(error.message).toEqual(`ERROR MESSAGE in request: POST some.domain.com/path "The request body"`);
|
|
}
|
|
expect(caught).toEqual(true);
|
|
});
|
|
});
|
|
});
|