-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2752 from leifos-gmbh/js-unit-test
JS Unit Testing
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
docs/development/js/js-unit-test-example/test/component.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |