Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug with npm test

I am using VS Code. When I try to run the test in debug mode, it says describe is not a function. Thus, only way I am able to run the test is through npm Note.

Note: I am using mocha and chai.

var { describe,it, before, after } = require('mocha');
var assert = require('chai').assert;
var AuthAPI = require('../api/controllers/API.js');
     describe('getItem tests', function() {
    it('getItem ', function(done) {
      var API = new AuthAPI(clientId, PASS, List);

      api_jwt = API.getItem();
      assert.isNotEmpty(api_jwt);
    });
    )}
like image 571
MANOJ Avatar asked Jan 17 '18 19:01

MANOJ


1 Answers

I think you just need another launch configuration for your mocha tests.

Go to Debug section in your Visual Studio Code.

Click the select control, then select "Add Configuration..." (or just click the gear icon and then click the "Add Configuration..." button);

You should be able to select "Node.js: Mocha Tests" there, it would generate a launch config like that:

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
    "args": [
        "-u",
        "tdd",
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/test"
    ],
    "internalConsoleOptions": "openOnSessionStart"
},

!Note: starting from mocha 6 it requires a proper selection of the interface (https://mochajs.org/#interfaces), change tdd to bdd if you want to use describe functions.

Make sure your mocha tests are in ./test folder and you have installed mocha locally or customise the launcher config.

Let's say you have a test ./test/it_should_work.js (note, it does not have any require, since mocha binary knows that functions by itself)

describe('test', () => {
  it('should work', () => {
  });
})

Launch a newly created "Mocha Test" configuration, it should return a successful result.

test
    ✓ should work
  1 passing (9ms)

But if you really need to programmatically launch mocha tests, check out this official wiki page https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically.

like image 160
Nik Markin Avatar answered Oct 11 '22 01:10

Nik Markin