Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Unit Testing #2752

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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).