简体   繁体   中英

How to configure Mocha with WebStorm

I have issue with running the Mocha tests .

I made configurations for WebStorm, and when I run the Mocha with WebStorm test runner mine tests are working.

But when I run the tests with 'node 'the filename'' from mine terminal I got error of "describe is not defined".

const assert = require('assert');

describe('login', function() {
    describe('find_user', function() {
        it('should find the user after login', function() {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for login
        });
    });
});


describe('register', function() {
    describe('register_user', function() {
        it('should find the user after register', function() {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for register
        });
    });
});

describe('contact', function() {
    describe('contact_us', function() {
        it('should find the contact message within the database', function() 
   {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for contact us
        });
    });
});

After modifying mine code to this version: I got error "describe is not a function".

const assert = require('assert');
const mocha = require('mocha');
const  describe = mocha.describe;
const  it = mocha.it;

describe('login', function() {
    describe('find_user', function() {
        it('should find the user after login', function() {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for login
        });
    });
});


describe('register', function() {
    describe('register_user', function() {
        it('should find the user after register', function() {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for register
        });
    });
});

describe('contact', function() {
    describe('contact_us', function() {
        it('should find the contact message within the database', function() 
 {
            assert.equal([1,2,3].indexOf(4), -1);
            // should be code for contact us
        });
    });
});

package.json:

{
  "name": "couponsystem",
  "version": "1.0.0",
  "description": "electron desktop project",
  "main": "js/src/app.js",
  "scripts": {
    "start": "electron ."
  },
  "dependencies": {
    "bootstrap": "^4.2.1",
    "electron": "^4.0.0",
    "handlebars": "^4.0.12",
    "sequelize": "^4.42.0"
  },
  "devDependencies": {
    "mocha": "^5.2.0"
  },
  "author": "maks burkov",
  "license": "ISC"
}

Can you explain me what configurations I need in order to run the tests from terminal?

You need using mocha test runner to run your tests, just passing test file to node interpreter as you did won't work.

Just add "test": "mocha" to the "scripts": {} section of your package.json and then run npm test in your terminal.

See https://mochajs.org/#getting-started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM