Why Jest’s toThrow won’t work when create an instance of a ES6 class directly in the constructor?

Here expect(new TestObject()).toThrow(); new TestObject() is evaluated first, then expect(…), then …toThrow(), in accordance with operator precedence. When new TestObject() throws, anything else doesn’t matter. This is why toThrow expects a function that is supposed to throw: expect(() => { new TestObject(); }).toThrow(); This way it can be wrapped with try..catch internally when being called. … Read more

ES6 module syntax: is it possible to `export * as Name from …`?

No, it’s not allowed in JS either, however there is a proposal to add it. For now, just use the two-step process with importing into a local variable and exporting that: // file: constants.js export const SomeConstant1 = ‘yay’; export const SomeConstant2 = ‘yayayaya’; // file: index.js import * as Constants from ‘./constants.js’; export {Constants};

ES2015 vs ESM2015

According to the documentation for Angular Package Format: Short for ECMAScript Modules. A file containing statements that import and export symbols. This is identical to the definition of modules in the ECMAScript spec.

Can I use an ES6/2015 module import to set a reference in ‘global’ scope?

Shimming modules is the way to go: http://webpack.github.io/docs/shimming-modules.html I quote from the page: plugin ProvidePlugin This plugin makes a module available as variable in every module. The module is required only if you use the variable. Example: Make $ and jQuery available in every module without writing require(“jquery”). new webpack.ProvidePlugin({ $: “jquery”, jQuery: “jquery”, “window.jQuery”: … Read more

How to correctly use ES6 “export default” with CommonJS “require”?

To use export default with Babel, you can do 1 of the following: require(“myStuff”).default npm install babel-plugin-add-module-exports –save-dev Or 3: //myStuff.js var thingToExport = {}; Object.defineProperty(exports, “__esModule”, { value: true }); exports[“default”] = thingToExport;

What is the difference between using constructor vs state = {} to declare state in react component?

They are roughly equivalent. The significant difference is that the initializer in the second example is executed before constructor. The second approach uses class fields proposal. It is not a part of ECMAScript standard yet so you need to set up transpiler properly to use the second approach. UPD Take a look at Babel output … Read more

JavaScript ES6: Test for arrow function, built-in function, regular function?

Believe it or not… Testing for presence of “=>” in string representation of a function is likely the most reliable way (but not 100%). Obviously we can’t test against either of 2 conditions you mentioned — lack of prototype property and lack of [[Construct]] as that might give false positives with either host objects or … Read more