2018-10-04 14:37:37 +01:00
---
id: 587d824e367417b2b2512c58
title: Run Functional Tests on API Endpoints using Chai-HTTP
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301593
2021-01-13 03:31:00 +01:00
dashedName: run-functional-tests-on-api-endpoints-using-chai-http
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-11-26 18:05:42 +00:00
2021-04-29 06:13:38 -04:00
As a reminder, this project is being built upon the following starter project on [Replit ](https://replit.com/github/freeCodeCamp/boilerplate-mochachai ), or cloned from [GitHub ](https://github.com/freeCodeCamp/boilerplate-mochachai/ ).
2019-03-27 17:01:58 +05:30
2021-08-11 01:45:12 +09:00
Mocha allows you to test asynchronous operations like calls to API endpoints with a plugin called `chai-http` .
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
The following is an example of a test using `chai-http` for a suite called `'GET /hello?name=[name] => "hello [name]"'` :
2020-11-26 18:05:42 +00:00
```js
suite('GET /hello?name=[name] => "hello [name]"', function () {
2021-08-11 01:45:12 +09:00
test('?name=John', function (done) {
2020-11-26 18:05:42 +00:00
chai
.request(server)
2021-08-11 01:45:12 +09:00
.get('/hello?name=John')
2020-11-26 18:05:42 +00:00
.end(function (err, res) {
2021-08-11 01:45:12 +09:00
assert.equal(res.status, 200, 'Response status should be 200');
assert.equal(res.text, 'hello John', 'Response should be "hello John"');
2020-11-26 18:05:42 +00:00
done();
});
});
2021-08-11 01:45:12 +09:00
});
2020-11-26 18:05:42 +00:00
```
2021-08-11 01:45:12 +09:00
The test sends a `GET` request to the server with a name as a URL query string (`?name=John` ). In the `end` method's callback function, the response object (`res` ) is received and contains the `status` property.
The first `assert.equal` checks if the status is equal to `200` . The second `assert.equal` checks that the response string (`res.text` ) is equal to `"hello John"` .
Also, notice the `done` parameter in the test's callback function. Calling it without an argument at the end of a test is necessary to signal that the asynchronous operation is complete.
2020-11-26 18:05:42 +00:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
Within `tests/2_functional-tests.js` , alter the `'Test GET /hello with no name'` test (`// #1` ) to assert the `status` and the `text` of the response to make the test pass. Do not alter the arguments passed to the asserts.
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
There should be no URL query. Without a name URL query, the endpoint responds with `hello Guest` .
2020-11-26 18:05:42 +00:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
All tests should pass
2020-11-26 18:05:42 +00:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=0').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-04 14:37:37 +01:00
```
2021-08-11 01:45:12 +09:00
You should test for `res.status` == 200
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=0').then(
(data) => {
assert.equal(data.assertions[0].method, 'equal');
assert.equal(data.assertions[0].args[0], 'res.status');
assert.equal(data.assertions[0].args[1], '200');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
You should test for `res.text` == `'hello Guest'`
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=0').then(
(data) => {
assert.equal(data.assertions[1].method, 'equal');
assert.equal(data.assertions[1].args[0], 'res.text');
assert.match(data.assertions[1].args[1], /('|")hello Guest\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-04 14:37:37 +01:00
```js
2019-10-14 21:00:42 +05:30
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-04 14:37:37 +01:00
```