Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
palmerj3 committed Jan 17, 2017
1 parent 17764b7 commit aa42fa1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn.lock
node_modules
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Jest-Offline

Jest-Offline is an addon to your Jest test suite which will fail tests that make network requests.

To install:

```
yarn add jest-offline
```

To use:

```javascript
{
"jest": {
"setupFiles": [
"<rootDir>/node_modules/jest-offline"
]
}
}
```

Any tests that make network requests will fail with a thrown exception.

## Why do this?

Fundamentally, unit tests should not make network requests. Unit tests should be as small as possible and test components in isolation whenever possible.

Any tests that rely on the network are inherently flaky and can put a strain on systems.

The best unit tests are tests that give the exact same result every single time you run the test. If they rely on network then that is simply impossible to achieve.

So jest-offline offers a simple way to:
1. Identify what tests are hitting the network
2. Prevent tests from hitting the network

You could either use this locally in order to see what tests are hitting the network. Or you could enable this by default on CI so no new tests can be added that hit the network.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Mitm = require('mitm');
const mitm = Mitm();

mitm.on('request', (req, res) => {
throw new Error('Network requests forbidden in offline mode');
res.end();
});
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "jest-offline",
"version": "1.0.0",
"description": "Add to setupFiles in jest to block network access for tests",
"main": "index.js",
"author": "Jason Palmer",
"license": "Apache 2.0",
"dependencies": {
"mitm": "^1.3.2"
}
}

0 comments on commit aa42fa1

Please sign in to comment.