Skip to content
Permalink
Browse files
fix(rest): LastResponse.body() is now type-safe
`LastResponse.body<T>()` has been made generic so that you can specify the shape of response you're
expecting. If you prefer to use the non-type-safe approach, please configure it with `any`:
`LastResponse.body<any>()`
  • Loading branch information
jan-molak committed Jul 5, 2020
1 parent 3a1a5a3 commit a936a1f6b02657ebe81c23d3ad083dd94d60703f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
@@ -47,7 +47,7 @@ describe('@serenity-js/local-server', () => {
Ensure.that(LocalServer.url(), startsWith('http://127.0.0.1')),
Send.a(GetRequest.to(LocalServer.url())),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body(), equals('Hello World!')),
Ensure.that(LastResponse.body<string>(), equals('Hello World!')),
StopLocalServer.ifRunning(),
)).to.be.fulfilled.then(() => {

@@ -41,7 +41,7 @@ describe('ManageALocalServer', () => {
Ensure.that(LocalServer.url(), startsWith('http://127.0.0.1')),
Send.a(GetRequest.to(LocalServer.url())),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body(), equals('Hello World!')),
Ensure.that(LastResponse.body<string>(), equals('Hello World!')),
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression
});

@@ -79,7 +79,7 @@ describe('ManageALocalServer', () => {
Ensure.that(LocalServer.url(), startsWith('https://127.0.0.1')),
Send.a(GetRequest.to(LocalServer.url())),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body(), equals('Hello World!')),
Ensure.that(LastResponse.body<string>(), equals('Hello World!')),
];

given(
@@ -33,7 +33,7 @@ import { getPortPromise } from 'portfinder';
* StartLocalTestServer.onRandomPort(),
* Send.a(GetRequest.to(LocalServer.url())),
* Ensure.that(LastResponse.status(), equals(200)),
* Ensure.that(LastResponse.body(), equals('Hello!')),
* Ensure.that(LastResponse.body<string>(), equals('Hello!')),
* StopLocalTestServer.ifRunning(),
* );
*
@@ -17,6 +17,10 @@ import { expect } from '../../expect';
/** @test {Send} */
describe('Send', () => {

interface ExampleResponse {
id: number;
}

/**
* @test {Send.a}
* @test {HTTPRequest}
@@ -34,7 +38,7 @@ describe('Send', () => {
return actor.attemptsTo(
Send.a(GetRequest.to('/products/2')),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body(), equals({ id: 2 })),
Ensure.that(LastResponse.body<ExampleResponse>(), equals({ id: 2 })),
);
});

@@ -58,7 +62,7 @@ describe('Send', () => {
url: '/products/2',
}),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body(), equals({ id: 2 })),
Ensure.that(LastResponse.body<ExampleResponse>(), equals({ id: 2 })),
);
});

@@ -15,6 +15,11 @@ describe('LastResponse', () => {

describe('when asserting on the response to the last HTTP request', () => {

interface Product {
id: number;
name: string;
}

const
url = '/products/2',
body = { id: 2, name: 'apple' },
@@ -37,7 +42,7 @@ describe('LastResponse', () => {
*/
it('enables access to the response body', () => actor.attemptsTo(
Send.a(GetRequest.to(url)),
Ensure.that(LastResponse.body(), equals(body)),
Ensure.that(LastResponse.body<Product>(), equals(body)),
));

/**
@@ -20,12 +20,17 @@ import { HTTPRequest } from './HTTPRequest';
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
* import { Ensure, equals } from '@serenity-js/assertions';
*
* interface Book {
* title: string;
* author: string;
* }
*
* const actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com/api'));
*
* actor.attemptsTo(
* Send.a(GetRequest.to('/books/0-688-00230-7')),
* Ensure.that(LastResponse.status(), equals(200)),
* Ensure.that(LastResponse.body(), equals({
* Ensure.that(LastResponse.body<Book>(), equals({
* title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
* author: 'Robert M. Pirsig',
* })),
@@ -11,13 +11,18 @@ import { CallAnApi } from '../abilities';
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
* import { Ensure, equals } from '@serenity-js/assertions';
*
* interface Book {
* title: string;
* author: string
* }
*
* const actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com/api'));
*
* actor.attemptsTo(
* Send.a(GetRequest.to('/books/0-688-00230-7')),
* Ensure.that(LastResponse.status(), equals(200)),
* Ensure.that(LastResponse.header('Content-Type'), equals('application/json')),
* Ensure.that(LastResponse.body(), equals({
* Ensure.that(LastResponse.body<Book>(), equals({
* title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
* author: 'Robert M. Pirsig',
* })),
@@ -41,11 +46,34 @@ export class LastResponse {
* @desc
* Enables asserting on the {@link LastResponse} body
*
* @example <caption>A type-safe approach using generics</caption>
* interface Book {
* title: string;
* author: string
* }
*
* actor.attemptsTo(
* // ...
* Ensure.that(LastResponse.body<Book>(), equals({
* title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
* author: 'Robert M. Pirsig',
* })),
* );
*
* @example <caption>A non-type-safe approach using `any`</caption>
* actor.attemptsTo(
* // ...
* Ensure.that(LastResponse.body<any>(), equals({
* title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
* author: 'Robert M. Pirsig',
* })),
* );
*
* @returns {@serenity-js/core/lib/screenplay~Question<any>}
*/
static body(): Question<any> {
return Question.about<any>(`the body of the last response`, actor => {
return CallAnApi.as(actor).mapLastResponse(response => response.data);
static body<T = any>(): Question<T> {
return Question.about<T>(`the body of the last response`, actor => {
return CallAnApi.as(actor).mapLastResponse<T>(response => response.data as T);
});
}

0 comments on commit a936a1f

Please sign in to comment.