Мокко не может распознать файловую систему - PullRequest
1 голос
/ 06 июля 2019

Я новичок в тестах, я могу без проблем обращаться к своим конечным точкам с почтальоном, но mocha, похоже, не распознает мои переменные, которые зависят от файловой системы, он говорит, что callBackPath и appPath не определены

app.use((req, res, next) => {
  let version = req.url.match(/\/api\/(v[0-9]+).*/) || [];
  const {
    readdirSync
  } = require('fs');

  const dirPath = path.join(__dirname, './api');
  const getDirectories = srcPath => fs.readdirSync(srcPath).filter(file => fs.statSync(path.join(srcPath, file)).isDirectory());

  const lastDir = getDirectories(dirPath)[getDirectories(dirPath).length - 1];
  version = version[1] || '';
  if (version != '') {
    const appPath = path.join(__dirname, `./api/${version}/index.js`);
    const callBackPath = path.join(__dirname, `./api/${lastDir}/index.js`);
    console.log(callBackPath);
    if (!fs.existsSync(appPath)) {

      return res.status(404).send({
        message: 'It\'s not us, sorry we can\'t find this end point'
      });
    }
    require(appPath)(app);
  } else {
    require(callBackPath)(app);
  }
  next();
});

app.listen(config.port);
console.log(`Server started on port ${
      config.port
    }`);

  module.exports = app;

код мокко

chai.use(chaiHttp);
chai.should();

describe('user', ()=>{
  describe('POST /', ()=> {
    it("should create new user", done=>{
      chai.request(app)
        .post('/register')
        .end((err, res) => {
          res.should.have.status(200);
          res.should.be.a('object');
          done();
        });
    });
  });
});
...