Run Functional Tests on an API Response using Chai-HTTP III - PUT method

My code:

test('send {surname: "Colombo"}', function (done) {
      chai
        .request(server)
        .put("/travellers")
        .send({ surname: "Colombo" }) // send payload
        .end(function (err, res) {
          console.log('>>>RES BODY =', res.body); // empty object :-()
          assert.equal(res.status, 200, 'status must be 200'); // OK
          assert.equal(res.type, 'application/json', 'reponse type must be json'); // OK
          assert.equal(res.body.name, 'Cristoforo', 'name must be "Cristoforo"'); // FAIL: undefined
          assert.equal(res.body.surname, 'Colombo', 'surname must be "Colombo"'); // FAIL: undefined
          done();
        });
    });

Debug shows that in server.js, in travellers() ‘req’ come with empty object ‘body’. So switch/case fails and returns an empty object.

solution: https://fcc-mochachai.aswonder53.repl.co
git: GitHub - LLPeterX/fcc-mochachai: A boilerplate for the freeCodeCamp curriculum.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Run Functional Tests on an API Response using Chai-HTTP III - PUT method

Link to the challenge:

Hello there,

You cannot comment out code that has been pre-written. It is needed for the tests.

Otherwise, I notice you are not doing this:

Send the following JSON response as a payload:

Specifically:

The '/travellers' endpoint accepts a JSON object

Pay attention to the object type expected.

Hope this helps

I commented out the code for faster testing.

But why the empty req.body comes into travellers() in server.js?
P.S. Change PUT to POST didn’t help.
Even with incorrect data, something must go into req.body in travellers() ?

Have you made the object a JSON object? Currently you have a JS object literal.

I found my error: I thought the ‘body-parser’ is deprecated in Express 4, so changed the server code incorrectly. With original server.js all tests passed.
Thanks.

But what is wrong in next task (https://www.freecodecamp.org/learn/quality-assurance/quality-assurance-and-testing-with-chai/run-functional-tests-on-an-api-response-using-chai-http-iv---put-method)
My code:

test('send {surname: "da Verrazzano"}', function (done) {
    chai.request(server)
      .put("/travellers")
      .send({ surname: "da Verrazzano" })
      .end(function (err, res) {
        console.log('>>>RES BODY =', res.body); // OK
        assert.equal(res.status, 200, 'status must be 200');
        assert.equal(res.type, 'application/json', 'reponse type must be json');
        assert.equal(res.body.name, 'Giovanni', 'body.name must be "Giovanni"');
        assert.equal(res.body.surname, 'da Verrazzano', 'body.surname must be "da Verrazzano"');
        done();
      });
  }); // end test

On repl.it:

Listening on port 3000
Running Tests...


  Functional Tests
>>>RES BODY = { name: 'Giovanni', surname: 'da Verrazzano', dates: '1485 - 1528' }
    ✓ send {surname: "da Verrazzano"} (89ms)
    Integration tests with chai-http
      ✓ Test GET /hello with no name
      ✓ Test GET /hello with your name
      ✓ send {surname: "Colombo"}


  4 passing (184ms)

But check res.body.name & res.body.surname fails on freecodecamp:

// running tests
You should test for 'res.body.name' to be 'Giovanni'
You should test for 'res.body.surname' to be 'da Verrazzano'
// tests completed

What’s wrong?

Would you mind starting your app, and sharing a link to your project code?

Yes, I start the app on repl.it, wait until tests are done, then copy link ‘https://fcc-mochachai.aswonder53.repl.co’ to ‘Solution link’ field and press “I’v completed the challenge”.
Two first tests are OK, but with “body.name” and “body,surname” fails.
repl.it: https://fcc-mochachai.aswonder53.repl.co
github: https://github.com/LLPeterX/fcc-mochachai.git

Please do not comment out the other code given in the boilerplate or past tests. The freeCodeCamp tests rely on the order and number of tests given in each suite, and you commenting them out causes the tests to think some do not exist, and are testing for the wrong sections.

Hope this clarifies

No.
Without commenting future tests, the application won’t start due to assert.fail() calls in the tests that I haven’t done yet.
In my case the error is “ReferenceError: browser is not defined” (the next lesson).

That error only comes about, if you have changed something you should not have. I suggest you re-copy the boilerplate, and only work within the sections you are meant to.

Many Campers struggle, because of adding/removing {} curly brackets, and miss-aligning the tests within the suites. So, watch out for this.

Thank you.
Using your original boilerplate did help for me.

1 Like