|
| 1 | +# jest-liveserver |
| 2 | + |
| 3 | +A pluggable jest environment which run [`live-server`](https://github.com/tapio/live-server) in background which can be used in your tests for better e2e tests |
| 4 | + |
| 5 | +> ## WIP and can be buggy |
| 6 | +
|
| 7 | +## Install |
| 8 | + |
| 9 | +`yarn add jest-liveserver -D` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +In your `jest.config.js`, add/replace the following |
| 14 | + |
| 15 | +```js |
| 16 | +{ |
| 17 | + testEnvironment: 'jest-liveserver', |
| 18 | + testEnvironmentOptions: { |
| 19 | + liveServer: {port,...}, |
| 20 | + liveServerJestPlugins: [plugin] |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Options |
| 26 | + |
| 27 | +There are two kind of options this package accepts, |
| 28 | + |
| 29 | +**1. liveServer** |
| 30 | + |
| 31 | +This is the `live-server` package's options |
| 32 | +ref [this options](https://github.com/tapio/live-server#usage-from-node) |
| 33 | + |
| 34 | +**2. liveServerJestPlugins** |
| 35 | + |
| 36 | +It accepts a array of plugins. [ref](#plugins) |
| 37 | + |
| 38 | +## Plugins |
| 39 | + |
| 40 | +### Why ? |
| 41 | + |
| 42 | +This package supports plugins which can be added to the core of the environment. |
| 43 | +The core plugin just takes the options for the live-server and simply run a server with them. But sometimes you may need to do some extra works like moving or copy-pasting your site or files to your test folder so for these kind of works use plugins. |
| 44 | + |
| 45 | +### Write a Plugin |
| 46 | + |
| 47 | +Plugins are simply objects which returns two methods, |
| 48 | + |
| 49 | +**preRun** |
| 50 | +This will run before running the `live-server` server, so work like moving files/folder can be done in this. |
| 51 | +this plugin should return a `live-server` options which will update the default options or the options passed through `jest.config.js` under `testEnvironmentOptions.liveServer` |
| 52 | + |
| 53 | +eg |
| 54 | + |
| 55 | +```js |
| 56 | +{ |
| 57 | + preRun: (dirname, cwdPath, _liveServerConfig) => new_live_server_config |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +**tearDown** |
| 62 | +This is the method which will run before stopping the `live-server` server, this can be use-full when doing operation like cleaning the dir or deleting temporary files etc. |
| 63 | + |
| 64 | +> It should not return any thing |
| 65 | +
|
| 66 | +eg |
| 67 | + |
| 68 | +```js |
| 69 | +{ |
| 70 | + tearDown: (dirname, cwdPath, _liveServerConfig) => {} |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Tips |
| 75 | + |
| 76 | +If you are facing issues like environment teardown quickly and closing the server, try to increase the jest timeout time |
| 77 | + |
| 78 | +`jest.setTimeout()` |
| 79 | + |
| 80 | +## Maintained by |
| 81 | + |
| 82 | +<img alt="pluggingIn logo" src="https://imgur.com/kjyrz79.png" width="250px" /> |
| 83 | + |
| 84 | +## Author |
| 85 | + |
| 86 | +[Aniketh Saha](https://twitter.com/__ANIX__) |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +[MIT](./LICENSE) |
0 commit comments