如何使用 WebStorm 配置 Mocha

How to configure Mocha with WebStorm

我对 运行Mocha 测试有疑问。

我为 WebStorm 进行了配置,当我 运行 带有 WebStorm 的 Mocha 测试时 运行ner mine 测试正在运行。

但是当我 运行 从我的终端使用“node 'the 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
        });
    });
});

将我的代码修改为这个版本后:我收到错误 "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"
}

你能解释一下我需要什么配置才能 运行 从终端进行测试吗?

您需要使用 mocha 测试 运行ner 来 运行 您的测试,只是像您那样将测试文件传递给节点解释器是行不通的。

只需将 "test": "mocha" 添加到 package.json"scripts": {} 部分,然后在终端中添加 运行 npm test

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