Well, let us look at the tests, as I think this would be most beneficial:
const res = await fetch(url + '/api/exercise/new-user', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
});
if (res.ok) {
const { _id, username } = await res.json(); // This is the response from your app
// Now, using all the information provided, an expected object is created...
const expected = {
username,
description: 'test',
duration: 60,
_id,
date: 'Mon Jan 01 1990'
};
- A
POST
request is made to your app’s endpoint /api/exercise/new-user
- In this POST, a username is passed:
username=fcc_test_${Date.now()}
.substr(0, 29)` (This is just a fancy way to ensure that every time a new user is posted, it has a different username, because the username is linked to the time of request)
- An expected object gets created.
- This has very little to do with the actual test, but is useful to use for testing
const addRes = await fetch(url + '/api/exercise/add', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}&date=1990-01-01`
});
- A new POST request is made to the app endpoint:
/api/exercise/add
- The following arguments are passed in the request:
let body = {
userId: _id,
description: expected.description,
duration: expected.duration,
date: "1990-01-01"
}
- The
_id
comes from this line above:
const { _id, username } = await res.json();
This is exactly the same as if you had made use of the UI to create a new-user
:

Then, add an exercise:

Now:
if (addRes.ok) {
const actual = await addRes.json();
assert.deepEqual(actual, expected);
- If the response returns an
ok
status, the actual user object is stored in the variable actual
-
actual
is compared to the expected
created earlier.
In summary:
- New user created
-
expected
values are based on this new user
- An exercise is added to a user based on the
_id
- The returned (
actual
) user object is compared to the expected
user object
Hope this helps