简体   繁体   English

如何使用WebStorm配置Mocha

[英]How to configure Mocha with WebStorm

I have issue with running the Mocha tests . 我在运行Mocha测试时遇到问题。

I made configurations for WebStorm, and when I run the Mocha with WebStorm test runner mine tests are working. 我为WebStorm进行了配置,当我使用WebStorm测试运行Mocha时,我的测试正常运行。

But when I run the tests with 'node 'the filename'' from mine terminal I got error of "describe is not defined". 但是,当我从我的终端使用'node'filename'运行测试时,出现“ 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: 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. 您需要使用mocha测试运行程序来运行测试,只是将测试文件传递给节点解释器就不能了。

Just add "test": "mocha" to the "scripts": {} section of your package.json and then run npm test in your terminal. 只需在package.json"scripts": {}部分中添加"test": "mocha" ,然后在终端中运行npm test

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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