node-migrate: can I use es6 import in migration script? - node.js

With node-migration npm package can I use es6 imports
I want to do:
import dash from './utils/rethinkdb';
let r = dash();
exports.up = function(next){
next();
};
exports.down = function(next){
next();
};
I know that i can just require, but I am writing everything in es6 so, wanted to be consistent
Thx

It does not necessarily depend on the migration framework you use, but on node.js itself.
Node.js has a "bit" of support for ES6 already, by using the --harmony flag. Take a look at it, if all your features you want are covered by --harmony, this might be an option:
https://github.com/joyent/node/wiki/es6-%28a.k.a.-harmony%29-features-implemented-in-v8-and-available-in-node
Another option is to use https://babeljs.io/ to transpile your code back to ES5 in order to run it.
Also note, if you're using io.js, you have to know that io.js doesn't handle the --harmony flag exactly the same as node does. All you need to know about this, you can read here:
https://iojs.org/en/es6.html

Related

mocking es6 modules in unit test

Suppose we have a file(source.js) to test:
// source.js
import x from './x';
export default () => x();
And the unit test code is very simple:
// test.js
import test from 'ava';
import source from './source'
test("OK", t => {
source();
t.pass();
});
But this is the tricky thing. The file "./x" does not exist in the test environment. Since it is a unit test, we do not need "./x". We can mock the function "x()" anyhow(using sinon or something else). But the unit test tool, ava, keeps showing "Error: Cannot find module './x'";
Is there any way to run the unit test without the file "./x"?
To do this, you'd need to override the import process itself. There are libraries for doing something similar-- overriding the require function with proxyquire, for example-- but these force you to invoke their custom function to import the module under test. In other words, you'd need to abandon using ES6 module syntax to use them.
You should also note that ES modules are only supported experimentally (and relatively recently) in Node. If you're transpiling with Babel, you're not actually using ES modules. You're using the syntax, sure, but Babel transpiles them to CommonJS "equivalents", complete with require calls and module.exports assignments.
As such, if you're using Babel, you can probably proxyquire to import modules under test in your test files, even if you're using ES module syntax in those modules. This will break, though, if you ever switch away from Babel. :\
My personal recommendation would be to avoid directly exporting anything you might need to stub. So functions like x should be in a static module imported like import foo from './foo' and invoked like foo.x(). Then, you can easily stub it with sinon.stub(foo, 'x'). Of course, the './foo' file will have to exist for this still, but what it really comes down to is how hardcore you want to be about TDD practices versus how much complexity you're willing to introduce to your mocking/stubbing process. I prefer relaxing the former to avoid the latter, but in the end it's up to you.
If you are using Jest, it can be achieved easily by mocking the import using the inbuilt mock method of jest.
Please refer the link below to find more about ES6 import mocks
https://jestjs.io/docs/en/es6-class-mocks
If you are using jest with JavaScript this is very simple:
use jest.mock('MODULE_NAME')
If it is a node module or your own module, no problem jest will automatically mock that module.

How to use ES6 and ES7 in my Gruntfile

I would like to use ES6 and ES7 in my Gruntfile. The reason is that I would like to write a task, which includes the git-repository module. As you can see from the documentation, the module is only available in ES6 and ES7 and I would like to integrate this module as easy as possible. Is there a way that I can use ES6 and ES7 also in my Gruntfile - somthing like babel grunt? Unfortunately, I did not find anything on Google and therefore I hope that you can help me.
Thank you in advance! :-)
Usually you may want to use a build tool to transpile, so this is a 'who will build a build tool' problem.
It isn't usual for public package on NPM to be available only in ES.next or ES6 with feature set that is not supported by Node. git-repository isn't exception. It surely has transpiled code in the package and can be used without Babel.
Because the package was transpiled with babel-plugin-transform-runtime, it requires babel-polyfill to work.
Documentation uses async...await only as an example because it suits the workflow. async functions use promises, in ES5/ES6 it would be
require('babel-polyfill');
Repo.open('./example', { init: true })
.then(repo =>
repo.setRemote('origin', 'https://github.com/user/example.git')
.then(() => repo.add('--all .'))
.then(() => repo.commit('Commit message'))
...
);
co is excellent alternative to async...await for Node ES6 feature set that requires no transpiler.

Using node module in angularjs?

What's the best practice for using external code, e.g. code found in node modules, in angular?
I'd like to use this https://www.npmjs.com/package/positionsizingcalculator node module in my angular app. I've created an angular service intended to wrap the node module, and now I want to make the service use the node module.
'use strict';
angular.module('angularcalculator')
.service('MyService', function () {
this.calculate = function () {
return {
//I want to call the node module here, whats the best practice?
};
}
});
To do this, I would crack open the package and grab the .js out of it. This package is MIT license, so we can do whatever we want. If you navigate to /node_modules/positionsizingcalculator/ you'll find an index.js. Open that up and you'll see the moudle export, which takes a function that returns an object.
You'll notice this is an extremely similar pattern to .factory, which also takes a function that returns an object (or constuctor, depending on your pattern). So I'd do the following
.factory('positionsizingcalculator', function(){
basicValidate = function (argument) {
... //Insert whole declaration in here
return position;
})
and the inject it where you need it:
.controller('AppController', function(positionsizingcalculator){
//use it here as you would in node after you inject it via require.
})
--
Edit: This is good for one off grabs of the JS, but if you want a more extensible solution, http://browserify.org/ is a better bet. It allows you to transform your requirements into a single package. Note that this could result in pulling down a lot more code that you might otherwise need, if you make one require bundle for your whole site, as this is not true AMD, and you need to to load everything you might want one the client, unless you make page specific bundles.
You'd still want to do the require in a factory and return it, to keep it in angular's dependency injection framework.

Write a module that works both in nodejs and requirejs

I wrote a small parser that currently works in node app, but wondering if there is a way that I can make a module that will work both in NodeJS app and client side app that uses requirejs?
path/to/lib/index.js
function someRandom(strings) {
// we are doing something here
return strings
}
exports.someRandom = someRandom;
Right now I'm getting this in client-side
Uncaught ReferenceError: exports is not defined
I know that I can use node requirejs and then change the structure to use define but is there other way without adding node requirejs?
This is my js/main.js file
require(["path/to/lib/index"], function(something) {
// will do something here
});
The way I prefer to do it is to write all my modules in the AMD syntax (use define) and use amd-loader to load them in Node. Note that this solution is not using RequireJS, even though the AMD syntax is used.
However, there's a way to do it without having to use the AMD syntax. You can use r.js to wrap your Node modules. For instance, if you put your tree of Node modules in in, you can do:
$ r.js -convert in out
This will create in out a tree of files that correspond to those in in but wrapped with the define call. You can then load these in the browser using RequireJS. There are limitations. Some are obvious, like not being able to use the Node modules that depend on the Node runtime (like fs, child_process, etc.). Some are more subtle, like the fact that you can't use require(foo) where foo is a variable (RequireJS will handle only string literals there). See the documentation for the details.

Is there dojo.hitch equivalent in node.js

I am looking for a function that kind bind the 'this' of a function. Although it is easy to write one, I want to be sure that it is not part of any popular module in node.js environment before I write or use the one from dojo.
(function() {
alert(this.toString());
}).bind(new String("Man ES5 is awesome.")).call();
Function.prototype.bind[docs]
Node.js runs on V8. V8 is fully ES5 compliant.

Resources