es6-modules

Jest: Mock ES6 Module with both default and named export

心不动则不痛 提交于 2019-12-04 18:31:43
问题 I have an ES6 module with both a default and a named export: /** /src/dependency.js **/ export function utilityFunction() { return false; } export default function mainFunction() { return 'foo'; } Its being used by a second ES6 module: /** /src/myModule.js **/ import mainFunction, { utilityFunction } from './dependency'; // EDIT: Fixed syntax error in code sample // export default myModule() { export default function myModule() { if (!utilityFunction()) return 2; return mainFunction(); } I'm

Is there any way to mock private functions with Jest?

给你一囗甜甜゛ 提交于 2019-12-04 15:34:58
问题 The ES6 module that I want to test looks as follows: function privateFunction() { ... } export function publicFunction() { ... does something ... privateFunction() ... does something else ... } I am using JEST for my unit tests and I am trying to find a way to test publicFunction and avoiding the execution of privateFunction by mocking it but I couldn't succeed in the mock attempt. Any idea? 回答1: I found out a way to mock my private function by using the babel-plugin-rewire module. In package

Webpack 4 Multi-Part library and sub-libraries

核能气质少年 提交于 2019-12-04 13:23:57
Overview I'am developing a javascript library with Webpack4 & ES6 called: "shared-services" which contains different sub-folders. Each of these subfolders represent a "local sub-library" inside which could contain: references to other 3rd libraries like lodash , etc but also references to the methods of the other "sub-folders or sub-libraries" Example I've made an example of my problem and pushed it to github: https://github.com/iarroyo5/multi-part-library-webpack . Helpfully it should be as simple as follow the Readme file in order to execute it :) Issues In my example I've named the "shared

pm2 not working with experimental-modules flag

时光怂恿深爱的人放手 提交于 2019-12-04 11:44:25
问题 I am using ES6 modules by adding the --experimental-modules arguments to Node. Running node --experimental-modules app.mjs works perfectly fine. However, when I run the same command with pm2 I get the following error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module My current pm2 config file looks like this: "apps": [ { "name": "api", "script": "app.mjs", "exec_mode": "cluster", "instances": "max", "node_args": "--experimental-modules", "env": { variables here.. } } ], I have also

Running tests .mjs / ESM on Node using Jasmine or any other alternative

梦想的初衷 提交于 2019-12-04 08:08:20
My Node-based project is implemented using native ES module support on Node thanks to the --experimental-modules CLI switch (i.e. node --experimental-modules ). Obviously, when I run a spec using Jasmine node --experimental-modules ./node_modules/jasmine/bin/jasmine I get the following error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Is it ever possible to use Jasmine using ES modules in Node? If not, is there any alternative to don't use a framework (e.g. running tests with npm scripts)? It was easier than I thought. It's just about calling a file which you might call run.mjs

es6 module import of d3 4.x fails

大兔子大兔子 提交于 2019-12-04 07:58:09
TL;DR: The documented way to import d3 into es6 modules fails. What is the correct way to do this? My guess is the documentation assumes I use a workflow that resolves these problems Details: The readme for d3 4.x says: D3 is written using ES2015 modules. Create a custom bundle using Rollup, Webpack, or your preferred bundler. To import D3 into an ES2015 application, either import specific symbols from specific D3 modules: import {scaleLinear} from "d3-scale"; Or import everything into a namespace (here, d3): import * as d3 from "d3"; Yet when I yarn add d3 and use a es6 script tag, this fails

How do I utilize dot notation when rendering components?

一笑奈何 提交于 2019-12-04 07:48:09
I have a simple component which is supposed to render different kind of fields into my form component: import React from "react"; export default class Field extends React.Component { render() { switch (this.props.type) { case 'textarea': { return ( <div className="col-xs-12"> <textarea placeholder={this.props.placeholder} name={this.props.name} > </textarea> </div> ) } case 'text': { return ( <div className="col-md-6 col-lg-4"> <input type="text" placeholder={this.props.placeholder} name={this.props.name} /> </div> ) } } } } And I'm using this component in my form component like this: export

Jest: Mock ES6 Module with both default and named export

若如初见. 提交于 2019-12-04 03:06:53
I have an ES6 module with both a default and a named export: /** /src/dependency.js **/ export function utilityFunction() { return false; } export default function mainFunction() { return 'foo'; } Its being used by a second ES6 module: /** /src/myModule.js **/ import mainFunction, { utilityFunction } from './dependency'; // EDIT: Fixed syntax error in code sample // export default myModule() { export default function myModule() { if (!utilityFunction()) return 2; return mainFunction(); } I'm trying to write a unit test for myModule.js using Jest. But when I try to mock both the named and the

ES6 (EcmaScript 2015) modules: import index.js

社会主义新天地 提交于 2019-12-04 00:03:52
Looking on the internet I'm confused with the special "index.js" module file. Using babelJS + nodeJS or Browserify/Webpack I can import an "index.js" module inside a "libs" directory using import myLib from "./libs" (ie. omitting the /index or /index.js part). Is the "index.js" module resolution (specifying the containing folder) supported by the ES6 (EcmaScript 2015) modules official standard? Or is it just "custom" NodeJS/CommonJS transpiling behaviour? Will it be possible to omit the /index | /index.js part of the import in all browsers as well (when modules will be supported on all

How to import jest?

时光怂恿深爱的人放手 提交于 2019-12-03 22:35:15
I would like to get rid of globals in my jest test code. Specifically describe , it and expect describe('Welcome (Snapshot)', () => { it('Welcome renders hello world', () => { ... }); }); So I tried add import {describe,it} from 'jest'; and import jest from 'jest'; jest.describe( ... jest.it( ... and other variations.. But no luck. How should I get it working? The simplest solution for this is adding jest: true to your env config in eslint, like so: "env": { "browser": true, "node": true, "jasmine": true, "jest": true, "es6": true }, After I realized jest is running in node, it realized I