Dieses Blog durchsuchen

Dienstag, 6. Dezember 2016

ES6 unittests with jest-cli

After 2 days fighting with babel and mocha , I couldnt figure out why none of my module exports worked.

Now I found jest-cli on a video on Egghead.io with Kent C. Dodds, which does the job of my unittest of ES6 classes.

1) install npm dependencies. Download package.json and run npm install

{
"name": "ject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.18.0",
"jest-cli": "^17.0.3"
},
"dependencies": {
"babel-jest": "^17.0.2"
}
}
view raw package.json hosted with ❤ by GitHub
2) save .babelrc with following content in the root of your project:
{
"presets": ["es2015"]
}
view raw .babelrc hosted with ❤ by GitHub


3) save following content to your root as sum.js
   
class Test {
constructor(x, y) {
this.x = x;
this.y = y;
}
sum(a, b) {
return a+b;
}
}
export default Test;
view raw sum.js hosted with ❤ by GitHub
4) save following content to your root as sum.test.js
/**
* Created by pboethig on 07.12.16.
*/
import Test from './sum';
test('1 + 2 = 3', ()=>
{
var inst = new Test(1,2);
inst.sum(1,2)
});
view raw sum.test.js hosted with ❤ by GitHub

5) run npm test

That`s all. Now you are able to test ES6 classes. Here are the souces on github: https://github.com/pboethig/unittest-es6-javascript

Keine Kommentare:

Kommentar veröffentlichen