chaijs / chai-http

HTTP Response assertions for the Chai Assertion Library.
http://chaijs.com/plugins/chai-http
632 stars 112 forks source link

chai-http is interfering with chai-dom #158

Open PhilippMi opened 7 years ago

PhilippMi commented 7 years ago

I created a little test to demonstrate the issue:

const chai = require('chai');
const chaiHttp = require('chai-http');
const chaiDom = require('chai-dom');
const jsdom = require('jsdom');

const expect = chai.expect;

before(function() {
  //chai.use(chaiHttp);
  chai.use(chaiDom);
});

describe('chai-http', function() {

  it('should not interfere with chai-dom', function() {

    return new Promise((resolve, reject) => {
        jsdom.env({
            html: "<html><body>This is the content</body></html>",
            done: function (errs, window) {
                if(errs) {
                    return resject(errs);
                }
                global.window = window;
                global.NodeList = window.NodeList;
                global.HTMLElement = window.HTMLElement;
                resolve();
            }
        });
    }).then(() => {
        expect(window.document.querySelector('body')).to.have.text('This is the content');
    });

  });

});

When I execute this code, everything works as expected and the test passes. But when I uncomment chai.use(chaiHttp), I get the following error:

expected <body>This is the content</body> to have headers or getHeader method

It seems like chai-http is interfering here with the chai-dom assertions.

I created a little repository where you can try my test code: https://github.com/PhilippMi/chai-http-dom-test

stevenhair commented 6 years ago

It also interferes with chai-html. I've had success putting chai.use(chaiHttp) in a before block, but 1. that may just work because of the order in which my tests are running, and 2. that doesn't really help you if you need to use both in the same test.

I'm not sure that this will ever be "fixed" because using any two plugins together can cause this.

keithamus commented 6 years ago

Hey @PhilippMi thanks for the issue!

Thanks @stevenhair for the extra comment.

We have some plans to fix this kind of thing long term, but yeah this wont be fixed any time soon. Follow https://github.com/chaijs/chai/issues/585 which hopes to address this (and many other issues) in the chai ecosystem