nathanboktae / chai-dom

DOM assertions for the Chai assertion library using vanilla JavaScript
Other
76 stars 26 forks source link

How to request a URL and asset the value of an h1 #28

Closed jinky32 closed 6 years ago

jinky32 commented 6 years ago

Hi,

I'm new to mocha and am trying to test an express route and assert the value of the h1 rendered on the page.

I have a test:

const expect  = require('chai').expect;
const assert = require('chai').assert;
const request = require('request');
const app = require('../../app');
const chai = require('chai');
chai.use(require('chai-dom'));
chai.use(chaiHttp);

describe('Story Homepage', function(){
  it('Should have an H1 of My Home Page', function(){
  chai.request(app)
  .get('http://localhost:7777/', function (){
      expect(body.querySelector('h1')).should.have.text('My Home Page');
    // document.querySelector('h1').should.have.text('My Home Page');
     });
  })
});

//some other tests

The good news is that the test passes. The bad news is that it shouldn't. Can anyone advise what I am doing wrong here?

nathanboktae commented 6 years ago

Yes, your test is asynchronous but mocha thinks it's synchronous because you are not returning a promise or taking a done parameter. See the documentation on it.

jinky32 commented 6 years ago

Hi - Sorry to come back on this but I'm still having issues. Would you be willing to post an answer to https://stackoverflow.com/questions/48331488/using-chai-to-test-value-of-html-element ?