Skip to content

Commit 730fc74

Browse files
committed
INITIAL COMMIT
0 parents  commit 730fc74

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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)

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "jest-liveserver",
3+
"version": "0.0.1",
4+
"description": "live server environment for your jest tests",
5+
"main": "src/jestEnvironment.js",
6+
"scripts": {},
7+
"files": [
8+
"src"
9+
],
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"jest-environment-node": "^24.9.0",
15+
"live-server": "^1.2.1"
16+
},
17+
"devDependencies": {}
18+
}

src/jestEnvironment.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const NodeEnvironment = require('jest-environment-node')
2+
const liveServer = require('live-server')
3+
4+
class liveServerEnv extends NodeEnvironment {
5+
constructor(config, context) {
6+
super(config, context)
7+
this.liveServerConfig = config.testEnvironmentOptions.liveServer || {}
8+
this.liveServerJestPlugins =
9+
config.testEnvironmentOptions.liveServerJestPlugins || []
10+
}
11+
12+
async setup() {
13+
this.liveServerJestPlugins.forEach(plugin => {
14+
console.log('plugin', plugin)
15+
const { liveServerConfig } =
16+
(plugin.preRun &&
17+
plugin.preRun({
18+
__dirname,
19+
cwdPath: process.cwd(),
20+
liveServerConfig: this.liveServerConfig
21+
})) ||
22+
{}
23+
Object.assign(this.liveServerConfig, liveServerConfig)
24+
})
25+
26+
console.log(
27+
`[jest liveserver ] Starting the server @${this.liveServerConfig.port}`
28+
)
29+
await liveServer.start(this.liveServerConfig)
30+
this.global.__LIVESERVER__ = liveServer
31+
this.global.liveSeverConfig = this.liveServerConfig
32+
}
33+
34+
async teardown() {
35+
this.liveServerJestPlugins.forEach(
36+
plugin =>
37+
plugin.tearDown &&
38+
plugin.tearDown({
39+
__dirname,
40+
cwdPath: process.cwd(),
41+
liveServerConfig: this.liveServerConfig
42+
})
43+
)
44+
console.log('[jest liveserver ] Shutting down the server')
45+
await this.global.__LIVESERVER__.shutdown()
46+
await super.teardown()
47+
}
48+
49+
runScript(script) {
50+
return super.runScript(script)
51+
}
52+
}
53+
54+
module.exports = liveServerEnv

0 commit comments

Comments
 (0)