Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const path = require('path')
const supertest = require('supertest')
describe('Starting the server', () => {
describe('Function exported by src/index.js', () => {
process.env.NODE_CONFIG = '{"pubsweet-server":{"port":4001}}'
/* eslint-disable-next-line global-require */
const startServer = require('../startServer')
/* eslint-disable-next-line jest/no-done-callback */
it('starts the server and returns it with express app attached', async done => {
const server = await startServer()
expect(server.listening).toBe(true)
expect(server).toHaveProperty('app')
server.close(done)
})
/* eslint-disable-next-line jest/no-done-callback */
it('returns the server if it is already running', async done => {
const server = await startServer()
server.originalServer = true
const secondAccess = await startServer()
expect(secondAccess).toHaveProperty('originalServer')
server.close(done)
})
})
describe('Custom server app entrypoint', () => {
beforeEach(() => {
jest.resetModules()
})
it('fails if file does not exist', async () => {
process.env.NODE_CONFIG = JSON.stringify({
'pubsweet-server': {
port: 4001,
app: 'some/file/that/does/not/exist.js',
},
})
/* eslint-disable-next-line global-require */
const startServer = require('../startServer')
await expect(startServer()).rejects.toThrow(
'Cannot load app from provided path!',
)
})
/* eslint-disable-next-line jest/no-done-callback */
it('starts the app using the custom file', async done => {
process.env.NODE_CONFIG = JSON.stringify({
'pubsweet-server': {
port: 4001,
app: path.resolve(__dirname, 'helpers', 'customApp.js'),
},
})
/* eslint-disable-next-line global-require */
const startServer = require('../startServer')
const server = await startServer()
const request = supertest(server.app)
const verify = await request.get('/verify')
expect(verify.status).toBe(200)
expect(verify.text).toBe('hi')
const index = await request.get('/')
expect(index.status).toBe(404)
server.close(done)
})
/* eslint-disable-next-line jest/no-done-callback */
it('starts the app using a custom file at ./server/app.js', async done => {
process.env.NODE_CONFIG = JSON.stringify({
'pubsweet-server': {
port: 4001,
},
})
process.env.NODE_CONFIG_STRICT_MODE = false
// Change directory to test/helpers, to reflect the env of an actual app
// so that we can load packages/server/test/helpers/server/app.js
const cwd = process.cwd()
process.chdir(path.join(__dirname, 'helpers', 'mockServer'))
/* eslint-disable-next-line global-require */
const startServer = require('../startServer')
const server = await startServer()
const request = supertest(server.app)
const verify = await request.get('/verify')
expect(verify.status).toBe(200)
process.chdir(cwd)
server.close(done)
})
})
})