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的更多相关文章

  1. JS(ES6)、Vue.js、node.js

    JS行为(ESMAScript, JSdom, bom)$.ajax() <- (xmlhttpRequest由这个封装来的)  -> axios(vue版)  =  ajax技术jque ...

  2. 让 Node.js 支持 ES6 的语法

    为了让 Node.js 支持 ES6 的语法, 需要使用 Babel. 安装 es-checker 在使用 Babel 之前 , 我们先检测一下当前 node 对 es6 的支持情况. 在命令行下执行 ...

  3. 让Node.js支持ES6的语法

    使用命令,全局安装es-checker: cnpm install -g es-checker 安装好之后,执行以下命令来查看Node.js对ES6的支持情况. es-checker 可以从输出中查看 ...

  4. io.js - 兼容 NPM 平台的 Node.js 新分支

    io.js(JavaScript I/O)是兼容 NPM 平台的 Node.js 新分支,由 Node.js 的核心开发者在 Node.js 的基础上,引入更多的 ES6 特性,它的目的是提供更快的和 ...

  5. node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法

    1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...

  6. Edge.js:让.NET和Node.js代码比翼齐飞

    通过Edge.js项目,你可以在一个进程中同时运行Node.js和.NET代码.在本文中,我将会论述这个项目背后的动机,并描述Edge.js提供的基本机制.随后将探讨一些Edge.js应用场景,它在这 ...

  7. [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 ...

  8. [Node.js] 01 - How to learn node.js

    基本概念 链接:https://www.zhihu.com/question/47244505/answer/105026648 链接:How to decide when to use Node.j ...

  9. Node.js实战(四)之调试Node.js

    当项目逐渐扩大以后,功能越来越多,这时有的时候需要增加或者修改,同时优化某些功能,就有可能出问题了.针对于线上Linux环境我们应该如何调试项目呢? 别怕,Node.js已经为我们考虑到了. 通过 n ...

随机推荐

  1. C++遍历目录+_finddata_t结构体用法

    Struct _finddata_t是用来存储文件各种信息的结构体,使用这个结构体要引用的头文件为“ #include <io.h>”它的结构体定义如下: struct _finddata ...

  2. [RxJS] Marbles Testings

    Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...

  3. 2.1 使用eclipse4.4 搭建 maven简单结构项目。

    1.前言 1.本博客面向0基础开发人员. 2.本博客为系列博客.<1.X>系列为服务器数据库相关技术,前几章为简单搭建linux+tomcat+mysql+nginx+redis.< ...

  4. 一起talk C栗子吧(第九十 三回:C语言实例--进程间通信之临界资源)

    各位看官们.大家好,前面章回中咱们说的是使用信号和管道进行进程间通信的样例.这一回咱们说的样例是:进程间通信之临界资源.闲话休提,言归正转.让我们一起talk C栗子吧! 我们首先介绍一下,什么是临界 ...

  5. 12.模板别名以及auto定义返回值

    #include <iostream> #include <array> using namespace std; //定义返回值类型 template<class T1 ...

  6. React-怎么写好组件-简单

    数据层:用来决定按钮的个数以及按钮是否选择. 表现层(展示层):按钮使用现有的ui 组件. 逻辑层(业务层):按钮事件等逻辑处理.

  7. react取消监听scroll事件

    如果要移除事件addEventListener的执行函数必须使用外部函数而不能直接使用匿名函数 错误写法: // 这样写是移除不了滚动事件的 componentDidMount() { // 添加滚动 ...

  8. 当鼠标聚焦时输入框变色(focus事件实例)

    当鼠标聚焦时输入框变色css相关,鼠标点击<input>输入域后出现有颜色的边框原理:css伪类之input输入框鼠标点击边框变色效果伪类元素的使用::focus 一:当输入框获得焦点时, ...

  9. 《TCP/IP具体解释卷2:实现》笔记--协议控制块

    协议层使用协议控制块(PCB)存放各UDP和TCP插口所要求的多个信息片.Internet协议维护Internet协议控制块 (internet protocol control block)和TCP ...

  10. js进阶 13-5 jquery队列动画如何实现

    js进阶 13-5 jquery队列动画如何实现 一.总结 一句话总结:同一个jquery对象,直接写多个animate()就好. 1.什么是队列动画? 比如说先左再下,而不是左下一起走 2.怎么实现 ...