samedi 3 octobre 2020

mocha is unable to find server.js file even though path is specified correctly in the test script

Guys I am using mocha and chai to test my node js/express application.

Here is my directory structure.

package.json
package-lock.json
server.js
app
  db
  models
  routes
    admin
      list_dentist.js
  utils
test
  app
    routes
      admin
        list_dentist.js

the scripts field in package.json file is as follows. enter image description here

This is the testing code I have written for a file named list_dentist.js

const chai = require("chai");
const chaiHttp = require("chai-http");
const should = chai.should();
const app = require("../../../../server");

chai.use(chaiHttp);

describe("should list all the dentists", () => {
  it("/admin/view_dentists", (done) => {
    chai
      .request(app)
      .get("/admin/view_dentists")
      .end((err, res) => {
        console.log(err);
        done();
      });
  });
});

whenever I type npm run test in the terminal I get the following error

Error: Cannot find module '../../../../server'
Require stack:
- D:\Smilycon Internship\Journal Server\test\app\routes\admin\dist\list_dentists.dev.js
- D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\esm-utils.js
- D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\mocha.js
- D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\cli\one-and-dones.js
- D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\cli\options.js
- D:\Smilycon Internship\Journal Server\node_modules\mocha\bin\mocha
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
    at Function.Module._load (internal/modules/cjs/loader.js:862:27)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (D:\Smilycon Internship\Journal Server\test\app\routes\admin\dist\list_dentists.dev.js:9:11)    
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.exports.requireOrImport (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\esm-utils.js:20:12)   
    at Object.exports.loadFilesAsync (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\esm-utils.js:33:34)    
    at Mocha.loadFilesAsync (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\mocha.js:427:19)
    at singleRun (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\cli\run-helpers.js:156:15)
    at exports.runMocha (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\cli\run-helpers.js:225:10)
    at Object.exports.handler (D:\Smilycon Internship\Journal Server\node_modules\mocha\lib\cli\run.js:366:11)
    at D:\Smilycon Internship\Journal Server\node_modules\yargs\lib\command.js:241:49
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! journal-server@1.0.0 test: `mocha test/**/*.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the journal-server@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\UMAKANTH\AppData\Roaming\npm-cache\_logs\2020-10-02T14_10_51_060Z-debug.log

Can anyone help me out with this problem?

Aucun commentaire:

Enregistrer un commentaire