简体   繁体   English

像Mocha一样在node.js上如何使用Jasmine

[英]How to use Jasmine like Mocha does on node.js

Hi there I was wondering how can I use jasmine like Mocha does w/ Chai. 嗨,我在想如何像茉莉(Mocha)搭配柴一样使用茉莉。

So basically, in Mocha you just have to install it on node and then include it within your test files like this: 因此,基本上,在Mocha中,您只需将其安装在node上,然后将其包含在测试文件中,如下所示:

const assert = require('chai').assert;
const expect = require('chai').expect;
const should = require('chai').should();
const app = require('../app');

Then you automatically include some test and run npm run test and you can see the failing and passing test via the terminal. 然后,您将自动包含一些测试并运行npm run test ,您可以通过终端查看失败和通过的测试。

While I tried to install jasmine via node using npm install jasmine --save-dev and then put the ff: 当我尝试使用npm install jasmine --save-dev通过节点安装茉莉花时, npm install jasmine --save-dev ,然后放入ff:

const expect = require('jasmine');
const app = require('../app');

And then tried some test: 然后尝试了一些测试:

describe('Multiply Numbers', function(){

  it('should return correct output', function(){
    let result = app.multiplyNumbers(2,2);
    expect(result, 4);
  });

});

I got the ff error: 我收到了ff错误:

> jasmine-test@1.0.0 test c:\xampp\htdocs\unitTesting\jasmine
> jasmine

Started


No specs found
Finished in 0.003 seconds

Any idea how can I run some test on Node js like mocha and chai does? 知道如何在象Mocha和Chai这样的Node js上进行一些测试吗? Also where can I find the assertion options (like expect, should..etc in chai) documentation? 另外,在哪里可以找到断言选项(例如,chai中的Expect,.. etc)文档?

PS. PS。 Here's what my package.json file: 这是我的package.json文件:

{
  "name": "jasmine-test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "directories": {
    "test": "test"
  },
  "scripts":{
    "test": "jasmine"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jasmine": "^2.8.0"
  }
}

There is a different way to implement testing environment using Jasmine for node setup. 使用Jasmine进行节点设置可以实现测试环境的另一种方式。

First Install Jasmine-node Module 首先安装茉莉花节点模块

npm install jasmine-node --save-dev

Create a tests directory and a spec file 创建一个测试目录和一个规范文件

mkdir tests
touch tests/myFirstTest.js

Now you can add your jasmine specs in this file, following the usual Jasmine syntax. 现在,您可以按照通常的Jasmine语法在此文件中添加茉莉花规格。 The documentation to be followed is here: https://jasmine.github.io/2.0/introduction.html 要遵循的文档在这里: https : //jasmine.github.io/2.0/introduction.html

Add following in your spec file to integrate your app with the spec. 在规范文件中添加以下内容,以将您的应用与规范集成。

var myApp = require("../myApp.js")

One problem identified regarding this integration is that the specs may run again and again if server is not closed. 有关此集成的一个问题是,如果未关闭服务器,则规范可能会一次又一次地运行。 For preventing this, just go to your application's main js file and export the close server function. 为了防止这种情况,只需转到应用程序的主js文件并导出关闭服务器功能即可。 Basically you need to add following in your app.js: 基本上,您需要在app.js中添加以下内容:

exports.closeServer = function(){
    server.close();
};

Be sure to close the server in afterEach jasmine function (refer doc above) as follows: 确保按照afterEach茉莉功能关闭服务器(请参阅上面的文档),如下所示:

myApp.closeServer();

In order to run tests from terminal using npm test, just add this to your packages.json file: 为了使用npm test从终端运行测试,只需将其添加到您的packages.json文件中:

"scripts": {
"test": "jasmine-node tests"
}

You are all setup! 一切就绪! Just run in your terminal, 只需在您的终端上运行

npm test

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

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