【问题标题】:How to configure Mocha with WebStorm如何使用 WebStorm 配置 Mocha
【发布时间】:2018-12-26 10:50:38
【问题描述】:

我在运行 Mocha 测试时遇到问题。

我为 WebStorm 进行了配置,当我使用 WebStorm 测试运行器运行 Mocha 时,我的测试正在运行。

但是当我从我的终端使用 'node 'the filename'' 运行测试时,出现“未定义描述”的错误。

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"
}

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

【问题讨论】:

    标签: javascript mocha.js webstorm


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多