Skip to content

Commit a222650

Browse files
committed
JavaScript-Unit-Testing-examples
1 parent c7815ad commit a222650

File tree

26 files changed

+778
-0
lines changed

26 files changed

+778
-0
lines changed

js-unit-testing-examples/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
insert_final_newline = true
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
end_of_line = lf
8+
9+
[*.{js,md}]
10+
indent_style = space
11+
indent_size = 4
12+
13+
[{package.json,.travis.yml}]
14+
indent_style = space
15+
indent_size = 2
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
"mocha": true
6+
},
7+
"globals": {
8+
"chai": true,
9+
"expect": true,
10+
"sinon": true
11+
},
12+
"extends": "eslint:recommended",
13+
"parserOptions": {
14+
"sourceType": "module"
15+
},
16+
"rules": {
17+
"indent": [
18+
"error",
19+
4
20+
],
21+
"linebreak-style": [
22+
"error",
23+
"unix"
24+
],
25+
"quotes": [
26+
"error",
27+
"single"
28+
],
29+
"semi": [
30+
"error",
31+
"always"
32+
],
33+
"no-console": "warn"
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"vsicons.presets.angular": false
3+
}

js-unit-testing-examples/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Marc Littlemore
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

js-unit-testing-examples/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# JavaScript Unit Testing Examples
2+
3+
Example project to allow me to show how best to unit test a JavaScript Express application.
4+
5+
## Install
6+
7+
Clone this repository and install the dependencies as follows:
8+
9+
```
10+
git clone git@github.com:MarcL/js-unit-testing-framework.git
11+
npm install
12+
```
13+
14+
## Running the tests
15+
16+
The code sets up a basic Express server with a few routes and some tests which cover testing the server setup. Run the test suite using npm:
17+
18+
```
19+
npm test
20+
```
21+
22+
## JavaScript Tests
23+
24+
Take a look in the `test` directory to see all of the test code. There are lots of examples of different types of tests and how to create them.
25+
26+
### Asynchronous functions and promises
27+
28+
Some examples of how to test asynchronous functions and promises, including some tips and tricks and gotchas.
29+
30+
#### Asynchronous function
31+
- Timeout because `done` callback isn't called when function succeeds
32+
- Passing test but slow because timer isn't stubbed
33+
- Passing test and fast because timer is stubbed
34+
- Timeout because `done` callback isn't called when function throws an error
35+
- Passing test to call `done` callback when function throws an error
36+
37+
#### Promise : resolving
38+
- Test passes incorrectly because Promise isn't returned
39+
- Passing test because promise is returned
40+
- Passing test because `done` callback is called after resolution
41+
- Passing test because `done` callback is called after resolution using `chai-as-promised` syntax
42+
43+
#### Promise : rejecting
44+
- Test passes incorrectly because Promise isn't returned
45+
- Failing test because rejected Promise error isn't caught
46+
- Passing test because Promise is returned and rejection is caught
47+
- Passing test because Promise rejection is caught and `done` callback is called
48+
- Passing test because Promise rejection is caught and `done` callback is called using `chai-as-promised` syntax
49+
50+
#### Slow tests
51+
- Passing test but is slow due to Promise function in chain taking a long time
52+
- Passing test and much faster as longer function is now stubbed to execute immediately
53+
54+
## License
55+
56+
See [LICENSE](LICENSE)

js-unit-testing-examples/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('babel-register');
2+
3+
var server = require('./src/launch');
4+
server.start();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "js-unit-testing-framework",
3+
"version": "0.0.1",
4+
"description": "Unit testing Express routes using Mocha",
5+
"main": "index.js",
6+
"scripts": {
7+
"lint": "eslint src/ test/",
8+
"precommit": "npm run lint",
9+
"prepush": "npm run test",
10+
"start": "node .",
11+
"test": "mocha",
12+
"test:watch": "mocha --watch"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+ssh://git@github.com/MarcL/js-unit-testing-framework.git"
17+
},
18+
"keywords": [
19+
"unit",
20+
"testing",
21+
"mocha",
22+
"express",
23+
"routes"
24+
],
25+
"author": "Marc Littlemore",
26+
"license": "MIT",
27+
"homepage": "https://github.com/MarcL/unit-test-express-routes#readme",
28+
"dependencies": {
29+
"babel-preset-es2015": "^6.24.1",
30+
"babel-register": "^6.24.1",
31+
"bluebird": "^3.5.0",
32+
"express": "^4.15.2"
33+
},
34+
"devDependencies": {
35+
"chai": "^3.5.0",
36+
"chai-as-promised": "~6.0.0",
37+
"eslint": "^3.19.0",
38+
"eslint-config-airbnb-base": "^11.1.3",
39+
"eslint-plugin-import": "~2.2.0",
40+
"husky": "^0.13.3",
41+
"mocha": "~3.4.1",
42+
"node-mocks-http": "^1.6.1",
43+
"proxyquire": "^1.7.10",
44+
"sinon": "^2.2.0",
45+
"sinon-chai": "^2.10.0"
46+
}
47+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
events: []
3+
};
4+

0 commit comments

Comments
 (0)