vendredi 12 juin 2020

test express servers in a way that has the functionality building throughout the tests with mocha/chai (No Database)

Is there a way to test express servers in a way that has the functionality building throughout the tests?

I'm trying to test an express server that has a couple of important, but non database variables. This server supports a webapp that is dependent on the user going through steps that build a final product (like a wizard).

For example. The first step is to choose a number of variables saved in a object called "columns". I want to POST that to my express server, and then test that the form is right when I access it with a GET. I am trying to test it with the following code, but even though it works in production, the tests are failing because it's not actually saving it to the variable during testing so I can't reuse it.

// Import the dependencies for testing
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../server');
// Configure chai
chai.use(chaiHttp);
chai.should();


describe('App', function () {
    // ...

    describe('/get', function () {
        it('responds with status 200', function (done) {
            chai.request(app)
                .post('/api/devices/selectedColumns', {columns: ['one', 'two', 'three']})
                .then(function () {
                    chai.request(app)
                        .get('/api/devices/selectedColumns')
                        .end(function (err, res) {
                            res.should.have.status(200);
                            res.body.should.equal({columns: ['one', 'two', 'three']});
                            done();
                        });
                });
        });
    });
});

I have the POST being tested in its own function, and it passes with the correct 200 status, but then if I print it out in the return statement, it prints as undefined.

Is there a way to test express servers in a way that has the functionality building throughout the tests? If not, how should I refactor my project?

Aucun commentaire:

Enregistrer un commentaire