FAQ: Server Testing Patterns - Response Content

This community-built FAQ covers the “Response Content” exercise from the lesson “Server Testing Patterns”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Testing for Web Development

FAQs on the exercise Response Content

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am getting a syntax error here if I use single quotations marks, like this:

router.get('/', (req, res) => {
    res.send('<h1 id='page-title'>Messaging App</h1>');
});

Provides this error message:

    res.send('<h1 id='page-title'>Messaging App</h1>');
             ^^^^^^^^^
SyntaxError: missing ) after argument list

But if I change it to double quotation marks, I am fine:

router.get('/', (req, res) => {
    res.send('<h1 id="page-title">Messaging App</h1>');
});

This never happened in any other exercises so far… is it because the HTML string is already using a set of single quotation marks? Or am I missing something?

I looked at jsdom documentation and in the notes. Why must we put a # in the string as below?

It’s coming from htmlAsString or maybe because of jsdom?

assert.equal(parseTextFromHTML(response.text, '#page-title'), "Messaging App")

Full script below:

const {assert} = require('chai');
const request = require('supertest');
const {jsdom} = require('jsdom');

const app = require('../../app');

const parseTextFromHTML = (htmlAsString, selector) => {
  const selectedElement = jsdom(htmlAsString).querySelector(selector);
  if (selectedElement !== null) {
    return selectedElement.textContent;
  } else {
    throw new Error(`No element with selector ${selector} found in HTML string`);
  }
};

describe('root page', () => {
  describe('GET request', () => {
    it('returns a 200 status', async () => {
      const response = await request(app).
      get('/');
      assert.equal(response.status, 200);
    });
    
    it('contains the correct title', async () => {
      const response = await request(app).
      get('/');
      
      assert.equal(parseTextFromHTML(response.text, '#page-title'), "Messaging App")

    });
  });
});

https://www.codecademy.com/paths/back-end-engineer-career-path/tracks/becp-test-driven-development-with-javascript/modules/fscp-learn-server-testing-with-tdd/lessons/test-server-patterns/exercises/test-server-patterns-response-content

Good question. I looked back in the lessons and found this:

const parseTextFromHTML = (htmlAsString, selector) =>{
   ...}

where the second argument is ‘selector’ which I understand to be an HTML selector, similar to the DOM method,

document.querySelector('#elementID') ;

So I am guessing we need to designate what element by writing ‘#elementID’. I presume we could also use ‘.elementClass’ as well?

Thank Jeffa,

Yes, it’s a CSS selector. The backend engineering path doesn’t cover CSS enough prior to this point. It just slips it in and expects us to know or find out on our own. Just another disjointed thing that causes unnecessary stress and wastes time in this path.

3 Likes

I get what you’re saying. There are some parts of this course that are very frustrating…needlessly frustrating.

1 Like

For our tests, once we retrieve the response from the server, we use assert.include() from the Chai library to check the response.
assert.include(parseTextFromHTML(response.text, ‘#my-name’), “My Name”); //True

Why does the hint for the first task tell us to use equal instead of include?

Use assert.equal to check the text returned from parseTextFromHTML().

1 Like