Comparing version 0.2.2 to 0.3.0
@@ -7,4 +7,4 @@ #!/usr/bin/env node | ||
.command( | ||
'create <rootDir>', | ||
'Creates Mock Service Worker at the specified directory', | ||
'init <rootDir>', | ||
'Initializes Mock Service Worker at the specified directory', | ||
(yargs) => { | ||
@@ -17,4 +17,4 @@ yargs.positional('rootDir', { | ||
}, | ||
require('./create'), | ||
require('./init'), | ||
) | ||
.help().argv |
@@ -67,2 +67,9 @@ /** | ||
/** | ||
* If the body cannot be resolved (either as JSON or to text/string), | ||
* the default value will be undefined. | ||
*/ | ||
const json = await req.json().catch(() => void 0) | ||
const text = await req.text().catch(() => void 0) | ||
const clientResponse = await messageClient(client, { | ||
@@ -78,2 +85,3 @@ url: req.url, | ||
referrerPolicy: req.referrerPolicy, | ||
body: json || text, | ||
}) | ||
@@ -80,0 +88,0 @@ |
{ | ||
"name": "msw", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Serverless client-side API mocking without a single change to the codebase.", | ||
@@ -12,4 +12,5 @@ "main": "lib/index.js", | ||
"build": "cross-env NODE_ENV=production webpack", | ||
"test": "jest", | ||
"prepublishOnly": "npm test && npm run build" | ||
"test:unit": "cross-env BABEL_ENV=test jest", | ||
"test": "yarn run test:unit", | ||
"prepublishOnly": "yarn test && yarn run build" | ||
}, | ||
@@ -16,0 +17,0 @@ "author": { |
<p align="center"> | ||
<a href="https://www.npmjs.com/package/msw"> | ||
<img src="https://img.shields.io/npm/v/msw.svg" alt="Package version"> | ||
<img src="https://img.shields.io/npm/v/msw.svg" alt="Package version" /> | ||
</a> | ||
<a href="https://circleci.com/gh/kettanaito/msw)"> | ||
<img src="https://img.shields.io/circleci/project/github/kettanaito/msw/master.svg" alt="Build status" /> | ||
</a> | ||
</p> | ||
@@ -14,5 +17,5 @@ | ||
- **Serverless**. Doesn't establish any servers, lives entirely in a browser; | ||
- **Deviation-free**. Request the same resources you would in production, and let MSW handle the mocking of the respective responses; | ||
- **Deviation-free**. Request the very same resources (urls) you would in production, and let MSW handle the mocking of the respective responses; | ||
- **Mocking as a tool**. Enable/disable/change mocking logic on runtime instantly without any compilations or rebuilds. Control the MSW lifecycle from your browser's DevTools. | ||
- **Essentials**. Emulate status codes, headers, cookies, delays, and more. | ||
- **Essentials**. Emulate status codes, headers, delays, and create custom response mocking functions. | ||
@@ -27,3 +30,3 @@ ## Motivation | ||
This library aims to eradicate those problems, as it takes an entirely different approach to the client-side API mocking. | ||
This library aims to annihilate those problems, as it takes an entirely different approach to the client-side API mocking. | ||
@@ -34,4 +37,6 @@ ## Getting started | ||
First, install `msw` with any package manager (npm, yarn, etc.). | ||
```bash | ||
npm install msw --dev | ||
yarn install msw --dev | ||
``` | ||
@@ -44,22 +49,24 @@ | ||
```bash | ||
msw create <rootDir> | ||
node_modiles/.bin/msw init <rootDir> | ||
``` | ||
> Replace `rootDir` with the relative path to your server's root directory (i.e. `msw create public`). | ||
> | ||
> In case you can't execute `msw` directly, try `node_modules/.bin/msw` as a local alternative. | ||
> Replace `rootDir` with the relative path to your server's root directory (i.e. `msw init public`). | ||
This is going to copy the Mock Service Worker to the specified directory, so it could be served as a static file from your server. This makes it possible to be registered from the client application. | ||
This is going to copy the Mock Service Worker file to the specified `rootDir` location, so it's served as a static file by your server. This makes it possible for the browser to register the referenced service worker. | ||
### 3. Use | ||
#### Where is my "root" directory? | ||
It's up to you where your mocks will reside. It's recommended, however, to isolate them into a separate module, which you can import on demand. | ||
This is usually the build directory of your application (`build/`, `public/` or `dest/`). This directory is often _committed to Git_, so should be the Mock Service Worker. Otherwise you could integrate service worker generation as a part of your build step. | ||
### 3. Define mocks | ||
First, create a mocking definition file: | ||
```js | ||
// app/mocks.js | ||
import { msw } from 'msw' | ||
import { composeMocks, rest } from 'msw' | ||
/* Configure mocking routes */ | ||
msw.get( | ||
'https://api.github.com/repo/:repoName', | ||
const { start } = composeMocks( | ||
rest.get('https://api.github.com/repo/:repoName', | ||
(req, res, { status, set, delay, json }) => { | ||
@@ -74,13 +81,44 @@ const { repoName } = req.params // access request's params | ||
) | ||
) | ||
) | ||
/* Start the Service Worker */ | ||
msw.start() | ||
start() | ||
``` | ||
Import your `mocks.js` module anywhere in the root of your application to enable the mocking: | ||
> You can modularize your mock files, but be sure to call `msw.start()` **only once!** | ||
### 4. Integrate | ||
Mocking is a **development-only** procedure. We highly recommend to include your mocking module (`app/mocks.js`) into your application's entry during the build. Please see examples of how this can be done below. | ||
#### Using webpack | ||
```js | ||
// ./webpack.config.js | ||
const __DEV__ = process.env.NODE_ENV === 'development' | ||
module.exports = { | ||
entry: [ | ||
/* Include mocks when in development */ | ||
__DEV__ && 'app/mocks.js', | ||
/* Include your application's entry */ | ||
'app/index.js', | ||
].filter(Boolean), | ||
/* Rest of your config here */ | ||
} | ||
``` | ||
#### Client-side import | ||
Alternatively, you can import mocking file(s) conditionally in your client bundle. | ||
```js | ||
// app/index.js | ||
import './mocks.js' | ||
if (__DEV__) { | ||
require('./mocks.js') | ||
} | ||
``` | ||
@@ -90,3 +128,3 @@ | ||
Service Workers are designed as a caching tool. However, we don't want our mocking definitions to be cached, which may result into out-of-date logic during development. | ||
Service Workers are designed as a caching tool. However, we don't want our mocking definitions to be cached, which would result into out-of-date logic during development. | ||
@@ -101,3 +139,3 @@ It's highly recommend to **enable "Update on reload"** option in the "Application" tab of Chrome's DevTools (under "Service Workers" section). This will force Service Worker to update on each page reload, ensuring the latest logic is applied. | ||
MSW uses Service Worker API with its primary ability to intercept requests, only instead of caching their responses it immitates them. In a nutshell, it works as follows: | ||
MSW (stands for "Mock Service Worker") uses Service Worker API with its primary ability to intercept requests, only instead of caching responses it immitates them. In a nutshell, it works as follows: | ||
@@ -116,2 +154,2 @@ 1. MSW spawns a dedicated Service Worker and creates a communication channel between the worker and the client. | ||
Have an idea? Found a bug? Please communicate it through using the [issues](https://github.com/kettanaito/msw/issues) tab of this repositories. [Pull requests](https://github.com/kettanaito/msw/pulls) are welcome as well! | ||
Have an idea? Found a bug? Please communicate it through using the [issues](https://github.com/kettanaito/msw/issues) tab of this repository. [Pull requests](https://github.com/kettanaito/msw/pulls) are welcome as well! |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2898
148
1
109046