nightwatch-cucumber ES6 --- nightwatch JS ES5 - javascript

I've read that nightwatch does not support ES6. Fair enough.
However, the documentation for nightwatch-cucumber
Looks like it uses ES6 (arrow functions) on the steps it defines as examples.
My question is: Is it possible to use ES6 on the cucumber steps and ES5 on the page objects for nightwatch? Or should I stick to ES5 for everything?

Yes I was able to write my cucumber steps in ES6 and my page objects in ES5. However it might be better to stick to ES5 to have consistency + I did notice I would get some strange failures (rarely) when using:
Given('Go to Site', () => client.url('https://google.com'));

Related

How to use babel to transform es6 to es5 programmatically?

I have a function which runs string as javascript code through eval(). It works fine if the string is es5 but doesn't work for es6. I know babel can transport es6 to es5 but most of them use cases are done in compile stage. How can I use babel programmatically?
Babel has an API.
I assume you can do something like this:
eval(babel.transform(code, options).code)
However I would strongly reconsider that! First, eval is usually a very, very dangerous thing, and next babel is huge. You don't want to deliver that to a browser if you don't have to.

How to get rid of editor’s error for object.key

I have the following code that basically gets some JSON data, looks for the keys with "servergenre", and saves the results in an array.
This is a follow up of this question.
let result = [];
Object.keys(data).forEach( key => {
if(/servergenre/.test(key)){
result.push(data[key])
}
});
Even though the code is working correctly, in some editors it raises syntactic errors:
"key": unsolvable variable or type key
"=>": expression expected
"if( / server...": formal parameter name expected
")){": , expected
"});": statement expected
Here is an image to show you where the errors are:
As I said the code is working fine, I just need it to be fixed or another approach to get rid of the errors.
Furthermore, many compressors and minifiers do not support this bit of code. So I can’t minify it.
Thanks in advance.
ES2015, formerly known as ES6, is a more recent version of JavaScript, which introduces features such as the => syntax for functions you are using.
Not all features of ES2015 are fully supported in all browsers, so many people who use it pass it through a compiler (“transpiler”) first to convert it to ES5, which is supported by all browsers. Babel is one such transpiler. But if you are only targeting newer browsers, that would explain why the => syntax is working for you.
You just need to change your editor settings to understand that syntax. Exactly how you do that depends on what text editor you are using. It could be that your editor's built-in JavaScript mode doesn't know how to read ES2015 syntax, and you need to either upgrade your editor or install a third-party plugin that provides an updated error-checker. Or it could be that your editor supports both ES5 and ES2015, and it thinks you are trying to write your project only in ES5. In this case you just need to go to the settings and tell your editor that you mean for this project to use ES2015 (or ES2016, which is currently the most recent version).
Fat arrows are ES6 syntax. If that causes trouble, just write good old ES5 :
let result = [];
Object.keys(data).forEach( function(key) {
if(/servergenre/.test(key)){
result.push(data[key])
}
});

Destructuring in ES6. Should I worry?

Experimenting with destructuring and found that the same code works on stackoverflow and not Codepen (toy gets "undefined"): http://codepen.io/tsalexey544/pen/VjWxmm?editors=0010#
What does it mean? should I worry when using destructuring in my projects?
let obj = {
species: "Cat",
// toy: "ball",
}
function whatDoTheyDo ({species, toy = "ball"}) {
return `The ${species} playes with a ${toy}`
}
document.write(whatDoTheyDo(obj));
You just need to set the Preprocessor to babel in CodePen, otherwise it will use standard ES5, where destructuring is not supported.
If you want to use ES6/ES7 features you have to "transpile" your code back to ES5 using certain tools such as Babel. Some browsers already support some ES6 features, but full support is still somehow spotty.
Edit - To answer your question: YES, you should worry about serving valid ES5 code, since ES6 is not yet fully supported. At the very minimum you should feed your code to Babel and publish the resulting code, but I strongly suggest looking it Webpack and going for a full toolchain

So i'm using Javascript const keyword, what happens in IE?

I understand that the const keyword has been already implemented across the board in browsers except for IE10 versions, but is it viable? If someone jumps on my site on IE10< will the "const" keyword be re-assigned to "var"? if not will the whole site fail? the MDN docs on the const keyword give a handy chart at the bottom which tells me that not only IE, but rather many Mobile browsers do not support it either. should i just scrap it and use var?
Take Babel, the ECMAScript 2015 (ES6) to ECMAScript 5 transpiler.
If you write:
const a = 123;
It outputs:
"use strict";
var a = 123;
If the potential of breaking your code on unsupported browsers isn't enough, i think that should be.
IE11 and above supports const but IE10 and below do not.
If you attempt to use const in any browser that does not support it, you will get a syntax error. If you must support older browsers, you cannot use const unless you use a transpiler to compile your code down into ES5. Babel is a good example of such a transpiler.
If you want to write clean ES6 (ES2015) code using const you could use JS compiler like Babel. For example:
const a = 1;
it recompile to
"use strict";
var a = 1;
If you want painless babel configuration use this yeoman babel generator.

What's the synax `async/await` in zombiejs code?

When I read the source code of Zombie.js, I found async/await keyword:
before(async function() {
await browser.visit('/streaming');
await browser.pressButton('1');
});
https://github.com/assaf/zombie/blob/41807a39c7aa1a13c4ef51575e0d581be96175bc/test/event_source_test.js#L60
Why can it use such keywords? What is the behaviour of the code? I tried to find some clue from the codebase, but not lucky
If we check out the gulpfile used to build that project, we can see that the source is piped through babel.
gulp.task('build', ['clean'], function() {
return gulp
.src('src/**/*.js')
.pipe(sourcemaps.init())
.pipe(babel({
experimental: true,
loose: 'all',
optional: [
'bluebirdCoroutines',
'runtime'
]
}))
});
Babel is a transpiler which allows you to write ES6+ code and transpile it back to ES5.
Babel will turn your ES6+ code into ES5 friendly code, so you can start using it right now without waiting for browser support.
If we check out the docs on Babel's site, we can see that in the ES7 section of the experimental section, there is an implementation for asyncFunctions.
These keywords are part of the ES7 specification, but they haven't stabilised. Hence them being included as experimental features of Babel.
In simplified terms, an async function will allow you to await a call which returns a promise.
async function() {
// flow will be suspended here until
// the promise returned by someFunction is resolved
await someFunction()
}
ES6 will include what are known as generators, which do a similar thing, but aren't specific to promises. You might start seeing the yield keyword around, or functions declared like this function* () {}. They are what's known as generator functions.
There is a particularly good article from PouchDB which explains a real world use case for these features.
Those keywords aren't available in EcmaScript 5, but are proposed for EcmaScript 7 (the version coming after the upcoming version 6). Right now you can use Babel to transcompile ES6 and some ES7 code into ES5, with some exceptions (notably proxying since it's not possible within ES5). Specifically for this, you can reference Babel's experimental features, specifically Stage 1, es7.asyncFunctions.
It is a new feature planned for ES7 that depends on promises and generators.
Why can it use such keywords?
Beacause they transpile the code with Babel.
What is the behaviour of the code?
It basically means the following in continuation passing style:
before(function() {
browser.visit('/streaming', function() {
browser.pressButton('1');
});
});
It's one of the best things to ever come to JavaScript besides modules and classes. Check out these two articles and library to get an feel for the power:
http://jlongster.com/Taming-the-Asynchronous-Beast-with-CSP-in-JavaScript
http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html
https://github.com/dvlsg/async-csp
Additionally, a search for "javascript async await" will surface some more good articles and examples.

Categories

Resources