node.js测试: 如何利用import / require语法打桩测试指定函数/ 类方法
2017-08-04 14:23
811 查看
node.js测试: 如何利用import / require语法打桩测试指定函数/ 类方法
ES6: Use of “import { property } from ‘module'” is Not a Great Plan – Ex Rationehttps://www.exratione.com/2015/12/es6-use-of-import-property-from-module-is-not-a-great-plan/
ES6: Use of "import { property } from 'module'" is Not a Great Plan
By ReasonDecember 18th, 2015PermalinkThe Javascript ES6 standard brings a new syntax for declaring imports from other modules
and exports from the present modules. Here I'll argue that some of that syntax, specifically the ability to import one or more named properties from a module, is overused in the community and causes more trouble than it is worth when writing ES6 Javascript
applications. The ES6 syntax for import and export is noted below in brief:
There are both obvious and subtle differences between the ES6 syntax listed above and the module frameworks in common use for ES5 Javascript ecosystems, such as Node.js. In the Node.js ES5 world, every module exports a default object unless that is replaced,
and an entirely empty module still exports an object with no properties. There is only one way to import a module, and that imports either the default export object, or whatever that object was explicitly replaced with. So:
exampleObject.js:
In ES6 modules there is no one straightforward analogy to module.exports in Node.js ES5, though it can be recreated by some combinations of export and import definitions:
example.es6.js:
If you look at the ES6 ecosystem, you'll see that the use of property import is popular. I'm seeing a lot of it now that I'm working with modules relating to React,
both in documentation and code. In Redux, for example, important functions are exported as properties in modules with no default export, and the documentation provides
examples like the following:
like Sinon. It is somewhere between very hard and impossible to write sufficient unit tests without the ability to stub and spy on function calls. It requires an
annoying amount of additional boilerplate code to make that possible for property importing, as the following small examples illustrate.
Stubbing in ES5 Javascript Testing
Stubbing a function in Javascript requires the function to be bound to a context, any context, that is in scope for both the test code and the code being tested. In a sane world this context is provided by the module. For example, in ES5 Node.js:
lib/example.js
lib/example.js
In ES6 using "import { x } from 'y'" is analogous to overwriting module.exports with a function in ES5. The result is that an imported function is encapsulated in the module and cannot be stubbed in unit tests without writing more boilerplate code that would
otherwise have been the case. See this example:
lib/example.es6.js
lib/example.es6.js
The bottom line is that "import { x } from 'y'" should be largely or completely absent from ES6 code in order to make the code both clean and testable. Yes, it's the new new thing, but no, it doesn't make life any better.
相关文章推荐
- nodejs require js文件入口,在package.json中指定默认入口main方法
- [置顶] 在js中如何实现方法重载?以及函数的参数问题
- js 小技巧之利用匿名函数控制方法访问权限
- 理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]
- js 利用iframe和location.hash跨域解决的方法,java图片上传回调JS函数跨域
- Node.js-require的使用方法
- NUnit详细使用用法(补充)--在.NET中如何利用NUnit测试Private和Protected方法
- JS随机调用指定函数的方法
- js推断指定函数、变量是否存在的方法
- JavaScript基础(基本语法:变量、语句、数组、函数、对象、全局方法以及js与java不同,js与html关联)
- js 定义函数的几种方法 以及如何调用
- 一篇写的比较好的js cookie的文章教你如何操纵 Cookies方法函数
- 如何更好的利用Node.js的性能极限
- jquery中ajax如何返回值到上层函数的方法以及对于js处理json对象方法的记录
- NUnit详细使用用法(补充)--在.NET中如何利用NUnit测试Private和Protected方法
- node -e "require('nan')" npm 安装 tty.js 时报错 ,解决方法
- JS字符串替换函数全部替换方法 JS 正则表达式如何使用变量
- js判断指定函数、变量是否存在的方法
- 在js中如何实现方法重载?以及函数的参数问题
- NUnit详细使用用法(补充)--在.NET中如何利用NUnit测试Private和Protected方法