H.Burns
H.Burns

Reputation: 419

How to match a particular string rendered on the webpage through mocha and chai

I am using mocha and chai packages to build a test suite in my Node JS application. As of now I am validating a test case as pass/fail by checking the status code (200) returned when a page is successfully rendered.

How can I validate the page as pass/fail based on what I am rendering on the page. For example if the the page displays "Welcome to Express" , I want to match any one of the words "Welcome" or "Express" to validate it as pass.

Below is the snippet of my code to check the status code:

describe('Home Page', () => {
    it('This is the Home page', (done)=> {
        chai.request(server)
        .get('/home')
        .end((error, respond) => {            
            expect(respond.statusCode).to.equal(200);                      
        done();
        });
    }); 
});

Upvotes: 1

Views: 371

Answers (1)

A.G.Progm.Enthusiast
A.G.Progm.Enthusiast

Reputation: 1010

As a simple solution you may directly use the content/string you want to match in the expect() statement as below:

describe('Home Page', () => {
    it('This is the Home page', (done)=> {
        chai.request(server)
        .get('/home')
        .end((error, respond) => {            
            expect(/Welcome/, done);                     
        done();
        });
    }); 
});

Upvotes: 1

Related Questions