A mysterious bug with Chai (very misterious)

A bug that is literally driving me crazy has inexplicable behavior.

I’m facing the issue tracker challenge. I am writing the Chai tests. The problem is that the tests seem to start with the suite statement, but loading the data from the next test expression. Then, when it finally calls the first test statement, it loads the data from the next test statement into it. The tests are done with certain asserts, but with the data of the next test! And start with the suite statement as it would a single test!!

I don’t know what to think anymore and I’m stuck.

Has this ever happened to any of you? What the hell is that wrong?

This is the link to the code, in case anyone wants to take a look at it.

I just can’t understand. I simplified the tests and reduced them to a single test (“create an issue with every field”). The test is this:

const chaiHttp = require('chai-http');
const chai = require('chai');
const assert = chai.assert;
const server = require('../server');

chai.use(chaiHttp);

suite('Functional Tests', function() {
  suite('Testing POST', function() {
    chai
      .request(server)
      .post("/api/issues/test")
      .set("content-type", "application/json")
      .send({
        'issue_title': "newIssue",
        'issue_text': "newText",
        'created_by': "newCreator",
        'assigned_to': "newAssignee",
        'open': "true",
        'status_text': "newStatus"
      })
      .end(function (err, res) {
        console.log(1000, {...res.body});
        assert.equal(res.status, 200);
        assert.equal(res.type, 'application/json');
        assert.equal(res.body.issue_title, 'newIssue');
        assert.equal(res.body.issue_text, 'newText');
        assert.equal(res.body.created_by, 'newCreator');
        assert.equal(res.body.assigned_to, 'newAssignee');
        assert.equal(res.body.open, 'true');
        assert.equal(res.body.status_text, 'newStatus');
        done();
      });
  });
});

As you can see, I have placed a console.log at the beginning of the end statement.

The result is the following:

1000 {
 ...
  _doc: {
    _id: '605f8c6cdc41690260f3aabd',
    project_name: 'test',
    issue_title: 'newIssue',
    issue_text: 'newText',
    created_by: 'newCreator',
    assigned_to: 'newAssignee',
    open: true,
    status_text: 'newStatus',
    created_on: '2021-03-27T19:50:04.401Z',
    updated_on: '2021-03-27T19:50:04.401Z',
    __v: 0
  }
}

So the _doc field appears to contain the fields that are expected to exist.

But right after that the application crashes with the following message

/home/runner/issuetracker/node_modules/mocha/lib/runner.js:936
    throw err;
    ^
AssertionError: expected undefined to equal 'newIssue'
    at /home/runner/issuetracker/tests/2_functional-tests.js:27:16

But what is this “undefined” that should be equal to newIssue if the _body field appears to contain the issue_title field correctly initialized to newIssue?

(This is the link to the code, in case anyone wants to take a look at it).

So no bugs, in mocha/chai. When you log res.body

it outputs

which tells you that what you’re looking for is actually in res.body._doc.issue_title, not res.body.issue_title as you are asserting. This presumably means that somewhere in your response code you are returning a raw mongoose object and not just the required fields, which is what your POST route appears to be doing.

1 Like

The error was not in the tests but in the “creative” way I deal with database transactions. Your intervention was very useful to me. Thanks.

Sorry if I always come here to ask for help, but I am continually faced with strange and counterintuitive problems.

I’m always facing the issue tracker challenge and I’m pretty much done. The Chai tests all pass. But the system test does not.

The error that is returned to me is:

You can send a GET request to /api/issues/{projectname} for an
array of all issues for that specific projectname, with all the
fields present for each issue.

You can send a GET request to /api/issues/{projectname} and
filter the request by also passing along any field and value as a
URL query (ie. /api/issues/{project}?open=false). You can pass
one or more field/value pairs at once. (Test timed out)

The fact is that during the system tests the console gives me the following data:

100 {
  data: [
    {
      _id: 60607dc43c610107b503edc1,
      project_name: 'get_issues_test_388523',
      issue_text: 'Get Issues Test',
      created_by: 'fCC',
      issue_title: 'Faux Issue 1',
      created_on: 2021-03-28T12:59:48.616Z,
      updated_on: 2021-03-28T12:59:48.616Z,
      open: true,
      __v: 0
    },
    {
      _id: 60607dc43c610107b503edc2,
      project_name: 'get_issues_test_388523',
      issue_text: 'Get Issues Test',
      created_by: 'fCC',
      issue_title: 'Faux Issue 2',
      created_on: 2021-03-28T12:59:48.979Z,
      updated_on: 2021-03-28T12:59:48.979Z,
      open: true,
      __v: 0
    },
    {
      _id: 60607dc53c610107b503edc3,
      project_name: 'get_issues_test_388523',
      issue_text: 'Get Issues Test',
      created_by: 'fCC',
      issue_title: 'Faux Issue 3',
      created_on: 2021-03-28T12:59:49.297Z,
      updated_on: 2021-03-28T12:59:49.297Z,
      open: true,
      __v: 0
    }
  ]
}
100 {
  data: [
    {
      _id: 60607dc93c610107b503edc4,
      project_name: 'get_issues_test_393041',
      issue_title: 'To be Filtered',
      issue_text: 'Filter Issues Test',
      created_by: 'Alice',
      assigned_to: 'Bob',
      created_on: 2021-03-28T12:59:53.167Z,
      updated_on: 2021-03-28T12:59:53.167Z,
      open: true,
      __v: 0
    },
    {
      _id: 60607dca3c610107b503edc5,
      project_name: 'get_issues_test_393041',
      issue_title: 'To be Filtered',
      issue_text: 'Filter Issues Test',
      created_by: 'Alice',
      assigned_to: 'Bob',
      created_on: 2021-03-28T12:59:54.171Z,
      updated_on: 2021-03-28T12:59:54.171Z,
      open: true,
      __v: 0
    },
    {
      _id: 60607dcb3c610107b503edc6,
      project_name: 'get_issues_test_393041',
      issue_title: 'To be Filtered',
      issue_text: 'Filter Issues Test',
      created_by: 'Alice',
      assigned_to: 'Eric',
      created_on: 2021-03-28T12:59:55.498Z,
      updated_on: 2021-03-28T12:59:55.498Z,
      open: true,
      __v: 0
    },
    {
      _id: 60607dcc3c610107b503edc7,
      project_name: 'get_issues_test_393041',
      issue_title: 'To be Filtered',
      issue_text: 'Filter Issues Test',
      created_by: 'Carol',
      assigned_to: 'Eric',
      created_on: 2021-03-28T12:59:56.849Z,
      updated_on: 2021-03-28T12:59:56.849Z,
      open: true,
      __v: 0
    }
  ]
}

which seem to be precisely the results required by the challenge.

Is there any mistake?
What are the results that the system expects to achieve?

(As alwais this is the link to the replit).

So if your response on the GET route is like this:

the information is there, but not in form the test expects. You’re sending some JSON and one of the fields (data) has the array of issues, but the test just wants the array of issues. An array of JSON objects is valid JSON, so just send the array.

The fault is mine for being unclear. The output I transcribed is generated by an instruction

console.log(100, {data});

placed just before res.send().

In other words, routing to GET ends with

console.log(100, {data});
res.send(data);

So the returned data structure is exactly an array of issues, as required by the problem.

I believe you need to use res.json() instead of res.send().

Regardless, when I run you repl.it, it gets through its tests and then stops and displays the “cannot reach your repl” message in the picture-in-picture browser (and a new tab). If I comment out the console.log() in the GET route, it continues running (not sure why). When I run the fCC tests against it, several of the route tests fail and there is a single undefined logged to the console.

To proceed, I would try to find that random undefined being logged to console, and then log the inputs and outputs of the routes (individually) to see if they are receiving the correct information and returning the correct information for the fCC tests (even though all your tests are passing). I would also look into those deprecation warnings in the console. Finally, I notice you have both mongodb and mongoose required, but only use mongoose so I would remove the mongodb requirement.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.