[Node.js] Testing ES6 Promises in Node.js using Mocha and Chai
Writing great ES6 style Promises for Node.js is only half the battle. Your great modules must include tests as well to ensure future iterations don't break them. In this lesson, I show you a simple ES6 Promise in Node.js, then walk you through creating tests in Mocha using chai and chai-as-promised to test both resolve and reject methods.
Install:
npm i -g mocha
npm i -D chai chai-as-promised
Index.js
exports.foo = (opts) => {
return new Promise(
(resolve, reject) => {
if(opts === ) {
reject('Found an error');
} else {
setTimeout( () => {
console.log(opts);
resolve(opts);
}, );
}
}
);
};
exports.foo()
.catch(err => {
console.log(err);
});
test/index.js:
const chai = require('chai');
const expect = chai.expect;
const chaiAsPromised = require('chai-as-promised');
const index = require('../index.js');
chai.use(chaiAsPromised);
describe('Function foo', () => {
it('should accpet anything but one', () => {
const promise = index.foo();
return expect(promise).to.eventually.equal();
});
it('should throw error is apply one', () => {
const promise = index.foo();
// return expect(promise).to.be.rejected;
return expect(promise).to.be.rejectedWith('Found an error');
})
});
[Node.js] Testing ES6 Promises in Node.js using Mocha and Chai的更多相关文章
- JS(ES6)、Vue.js、node.js
JS行为(ESMAScript, JSdom, bom)$.ajax() <- (xmlhttpRequest由这个封装来的) -> axios(vue版) = ajax技术jque ...
- 让 Node.js 支持 ES6 的语法
为了让 Node.js 支持 ES6 的语法, 需要使用 Babel. 安装 es-checker 在使用 Babel 之前 , 我们先检测一下当前 node 对 es6 的支持情况. 在命令行下执行 ...
- 让Node.js支持ES6的语法
使用命令,全局安装es-checker: cnpm install -g es-checker 安装好之后,执行以下命令来查看Node.js对ES6的支持情况. es-checker 可以从输出中查看 ...
- io.js - 兼容 NPM 平台的 Node.js 新分支
io.js(JavaScript I/O)是兼容 NPM 平台的 Node.js 新分支,由 Node.js 的核心开发者在 Node.js 的基础上,引入更多的 ES6 特性,它的目的是提供更快的和 ...
- node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法
1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...
- Edge.js:让.NET和Node.js代码比翼齐飞
通过Edge.js项目,你可以在一个进程中同时运行Node.js和.NET代码.在本文中,我将会论述这个项目背后的动机,并描述Edge.js提供的基本机制.随后将探讨一些Edge.js应用场景,它在这 ...
- [Whole Web, Node.js, PM2] Configuring PM2 for Node applications
In this lesson, you will learn how to configure node apps using pm2 and a json config file. Let's sa ...
- [Node.js] 01 - How to learn node.js
基本概念 链接:https://www.zhihu.com/question/47244505/answer/105026648 链接:How to decide when to use Node.j ...
- Node.js实战(四)之调试Node.js
当项目逐渐扩大以后,功能越来越多,这时有的时候需要增加或者修改,同时优化某些功能,就有可能出问题了.针对于线上Linux环境我们应该如何调试项目呢? 别怕,Node.js已经为我们考虑到了. 通过 n ...
随机推荐
- 程序中为什么会使用while(0)
https://blog.csdn.net/u012062760/article/details/46446207 关于while(0)实际上是用来宏定义的,这样的宏定义可以避免调用的时候出错. 如下 ...
- IOS - 获取UITextField的输入文本
当UITextField文本改变时, 依据内容更新数据, 通过写监听事件就可以. 加入监听: [timesField addTarget:self action:@selector(textField ...
- 17.Node.js 回调函数--异步编程
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程 ...
- bind()和trigger()额外数据
$(function(){ $('input').click(function(e,data1,data2,data3,data4){ alert(data1 + '|' + data2 + '|' ...
- dataguard主备延迟多长时间的查询方法
select value from v$dataguard_stats where name='apply lag';
- Vue Invalid handler for event "": got undefined
原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.
- 设计模式六大原则(一):单一职责原则(Single Responsibility Principle)
单一职责(SRP)定义: 不要存在多于一个导致类变更的原因,通俗的说,即一个类只负责一项职责. 问题由来: 类T负责两个不同的职责:职责P1,职责P2.当由于职责P1需求发生改变而需要修改类T时,有可 ...
- do_pj--下拉代码脚本的使用
接本目录 /home/zhangshuli/git2/vanzo_team/xulei/Platform.py 在~/bin目录下链接 ln -sf ~/git2/vanzo_team/xulei/P ...
- C Tricks(十八)—— 整数绝对值的实现
为正还是为负:(对 int 类型而言,第一位为符号位,其余为数值,则右移 31 位,再与 1 求与) 如果为正 ⇒ 返回原值 如果为负 ⇒ 对其二进制形式各位取反 + 1 int abs(int x) ...
- 程序是怎么跑起来的? —— CPU 是什么?C/C++程序的运行
1. 概念初步 程序:计算机的程序,和做饭.运动会的程序一样,指的是"做事的先后次序": 程序的组成:程序是指令(及物动词)和数据(宾语)的组合体: C 语言 printf(&qu ...