-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
ssr.js
55 lines (49 loc) · 1.61 KB
/
ssr.js
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
const fetch = require(`node-fetch`)
const execa = require(`execa`)
const fs = require(`fs-extra`)
const path = require(`path`)
describe(`SSR`, () => {
test(`is run for a page when it is requested`, async () => {
const html = await fetch(`http://localhost:8000/`).then(res => res.text())
expect(html).toMatchSnapshot()
})
test(`dev & build outputs match`, async () => {
const childProcess = await execa(`yarn`, [`test-output`])
expect(childProcess.code).toEqual(0)
})
test(`it generates an error page correctly`, async () => {
const src = path.join(__dirname, `/fixtures/bad-page.js`)
const dest = path.join(__dirname, `../src/pages/bad-page.js`)
fs.copySync(src, dest)
const pageUrl = `http://localhost:8000/bad-page/`
await new Promise(resolve => {
// Poll until the new page is bundled (so starts returning a non-404 status).
const testInterval = setInterval(() => {
fetch(pageUrl).then(res => {
if (res.status !== 404) {
clearInterval(testInterval)
resolve()
}
})
}, 1000)
})
const rawDevHtml = await fetch(
`http://localhost:8000/bad-page/`
).then(res => res.text())
expect(rawDevHtml).toMatchSnapshot()
fs.remove(dest)
// After the page is gone, it'll 404.
await new Promise(resolve => {
setTimeout(() => {
const testInterval = setInterval(() => {
fetch(pageUrl).then(res => {
if (res.status === 404) {
clearInterval(testInterval)
resolve()
}
})
}, 400)
}, 400)
})
})
})