Node.js & ES Modules & Jest
Node.js & ES Modules & Jest
CJS & ESM

CommonJS
https://en.wikipedia.org/wiki/CommonJS
https://nodejs.org/api/modules.html#modules_modules_commonjs_modules

module wrapper
https://nodejs.org/api/modules.html#modules_the_module_wrapper

ES Modules
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules

Jest
ES5
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-0
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
const sum = (a = 0, b = 0) => {
try {
log(`a, b =`, a, b);
return a + b;
} catch (err) {
throw new Error(`参数错误`, err);
}
};
module.exports = sum;
const sum = require('./sum');
// const sum = require('./sum.js');
test('sum(1, 2) should return 3', () => {
expect(sum(1, 2).toBe(3));
});
ES6
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-0
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
const sum = (a = 0, b = 0) => {
try {
log(`a, b =`, a, b);
return a + b;
} catch (err) {
throw new Error(`参数错误`, err);
}
};
export default sum;
export {
sum,
};
import { sum } from "./sum";
// import { sum } from "./sum.mjs";
test('sum(1, 2) should return 3', () => {
expect(sum(1, 2).toBe(3));
});
module.exports & exports
Node.js
https://www.w3schools.com/nodejs/nodejs_modules.asp
https://www.sitepoint.com/understanding-module-exports-exports-node-js/
https://tc39.es/ecma262/#sec-modules
https://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-js
https://github.com/xgqfrms-GitHub/node-express4-restful-api/issues/2
refs
https://flaviocopes.com/how-to-enable-es-modules-nodejs/
http://www.ruanyifeng.com/blog/2020/08/how-nodejs-use-es6-module.html
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
Node.js & ES Modules & Jest的更多相关文章
- node --experimental-modules & node.js ES Modules
node --experimental-modules & node.js ES Modules how to run esm modules in node.js cli $ node -v ...
- Node.js & ES modules & .mjs
Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...
- [Node.js] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...
- [Node.js] 4. Modules
4.2 Missing Exports Notice the two different files: high_five.js on the left side andapp.js on the r ...
- [Node.js] CommonJS Modules
CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...
- Node.js学习 - Modules
创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...
- Node.js require 模块加载原理 All In One
Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...
- 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境
1. 安装webstorm 并破解 2. 安装node (以及express框架) 至官网下载并安装.(http://nodejs.org)v0.10.32 msi 安装后测试,打开命令行, c ...
随机推荐
- 阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建
懒得重新排版然后发到博客了.用在线文档看吧 https://www.kdocs.cn/l/srV6o8rABW9V 用线上IDE(code-server)写scala的时候,出现BUG可以参考下面两篇 ...
- Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol
Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pushe ...
- 洛谷P4981
Description 给定 n 个点,组成一棵树,有多少种组合方法: Analysis 首先,结合题目简化意义和这句话 最多可能存在多少种父子关系 我们可以知道当且仅当有至少一个节点的儿子不同时称他 ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- Kafka客户端Producer与Consumer
Kafka客户端Producer与Consumer 一.pom.xml 二.相关配置文件 producer.properties log4j.properties base.properties 三. ...
- Django(图书管理系统)#转
自己虽然实现了, 但是写的太LOW了,为了不误导大家,推荐一篇好的博客 https://www.cnblogs.com/alice-bj/p/9114084.html
- 小白搭建WNMP详细教程---PHP安装与设置
php的安装请参考WAMP中PHP的安装教程https://www.cnblogs.com/missbye/p/12049925.html 需要注意的是,我们下载的PHP版本要下载Non Thread ...
- 分析 BAT 互联网巨头在大数据方向布局及大数据未来发展趋势
> 风起云涌的大数据战场上,早已迎百花齐放繁荣盛景,各大企业加速跑向"大数据时代".而我们作为大数据的践行者,在这个"多智时代"如何才能跟上大数据的潮流, ...
- 给jekyll博客添加搜索功能
使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...
- Java线程八锁
package com.atguigu.juc1205; import java.util.concurrent.TimeUnit; class Phone//Phone.java ---> P ...