'How to use GLOBAL FIXTURES in mocha JS to prepare preconditions of all test

I have read the document of mocha but can't follow the instruction. Because I have an Express Class like that

// ExpressServer.js

const http = require('http');
const express = require('express');
class ExpressServer {
   constructor(port, openApiYaml) {
     this.port = port;
     this.app = express();
   }
   launch() {
     http.createServer(this.app).listen(this.port);
   }
}

// setUpMocha.js

const { ExpressServer } = require('../expressServer');
const config = require('../config');

let expressServer;

const mochaGlobalSetup = async () => {
   expressServer = new ExpressServer(config.URL_PORT, config.OPENAPI_YAML);
   expressServer.launch();
};

module.exports = {
  mochaGlobalSetup,
  expressServer
}

// testAPIs

enter code here
const chai = require('chai');
const chaiHttp = require('chai-http');

const { expressServer } = require('./setUp');
const configTest = require('./configTest');

describe('Test all public APIS', function(){
   it('should get all diagrams belong to any site', function() {
       chai.request(expressServer.app)
       .get('/site/diagrams')
       .set('authorization', configTest.accessToken)
       .end(function(err, res){
         console.log(res);
       });
   });
});

So when i call

mocha --require ./test/setUp.js ./test/publicAPIs.js

It said that

TypeError: Cannot read property 'app' of undefined of line chai.request(expressServer.app)

The above file is example of my issue. I checked that i require all needed libraries. It is some think that wrong about set up, the way that mocha work ?

So I'm very confused "How can I fix this to prepare server for testing REST APIS'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source