Skip to content

Commit

Permalink
Merge pull request #2752 from leifos-gmbh/js-unit-test
Browse files Browse the repository at this point in the history
JS Unit Testing
  • Loading branch information
alex40724 committed Jun 22, 2020
2 parents 9188254 + 09195ac commit 1d9596e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/development/js/js-unit-test-example/src/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import math_mod from "./math.js";

export default class Component {

/**
* Pass dependency to math module in constructor
* @param math
*/
constructor(math) {
this.math = math || math_mod;
}

calculate(a,b) {
return this.math.sum(a,b);
}
}
5 changes: 5 additions & 0 deletions docs/development/js/js-unit-test-example/src/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
sum: function(a,b) {
return a + b;
}
}
30 changes: 30 additions & 0 deletions docs/development/js/js-unit-test-example/test/component.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Component from '../src/component.js';
import { expect } from 'chai';

// test 1
describe('component', function() {

let c = new Component();

it('calculate without mock', function () {
expect(c.calculate(1, 2)).to.equal(3);
});
});

// test 2
describe('component', function() {

// pass mock as dependency
const mathMock = {
sum: (a, b) => {
return a + b;
}
};

let c = new Component(mathMock);

it('calculate with mock', function () {
expect(c.calculate(1, 2)).to.equal(3);
});

});
35 changes: 35 additions & 0 deletions docs/development/js/js-unit-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# JS Unit Testing

ILIAS is using [Mocha](https://mochajs.org/) as test framework for Javascript unit tests. The `esm` extension allows to use ES6 modules seamlessly. [Chai](https://www.chaijs.com/) is being used as assertion library.

## Install

```
> npm i --save-dev mocha
> npm i --save-dev esm
> npm i --save-dev chai
```

## package.json changes

```
{
...
"scripts": {
"test": "mocha --require esm"
},
...
}
```

## Run tests

```
> npm test
```

## Example

Your tests should be located in a subdirectory `test`.

See [js-unit-test-example/test/component.test.js](./js-unit-test-example/test/component.test.js).

0 comments on commit 1d9596e

Please sign in to comment.