forked from styled-components/styled-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.test.js
82 lines (73 loc) · 2.16 KB
/
example.test.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
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
import app from '../example/devServer.js'
import { launch } from 'puppeteer'
import { toMatchImageSnapshot } from 'jest-image-snapshot'
expect.extend({ toMatchImageSnapshot })
const PORT = 9000
const urlWhitelist = [
new RegExp(`http:\/\/localhost:${PORT}.*`),
/https:\/\/unpkg\.com\/react@[\d\.]+\/dist\/react\.min\.js/,
/https:\/\/unpkg\.com\/react-dom@[\d\.]+\/dist\/react-dom\.min\.js/,
/https:\/\/unpkg\.com\/react-dom@[\d\.]+\/dist\/react-dom-server\.min\.js/,
/https:\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/babel-core\/[\d\.]+\/browser\.min\.js/
]
const globalCss = `
* {
-webkit-animation: unset !important;
animation: unset !important;
font-family: 'Arial' !important;
font-smooth: never !important;
}
pre, pre * {
font-family: 'Courier' !important;
}
.hero-header {
min-height: auto !important;
}
`
function startServer(app, port) {
return new Promise((resolve, reject) => {
const server = app.listen(port, error => {
if (error) {
reject(error)
}
resolve(server)
})
})
}
describe('example page', () => {
let server
let browser
let page
beforeAll(async () => {
server = await startServer(app, PORT)
browser = await launch({
executablePath: process.env.GOOGLE_CHROME_BINARY || undefined,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-lcd-text']
})
page = await browser.newPage()
page.on('request', req => {
if (urlWhitelist.find(regexp => req.url.match(regexp))) {
req.continue()
} else {
throw new Error(req.url)
req.abort()
}
})
await page.setRequestInterception(true)
await page.setViewport({ width: 1024, height: 768, deviceScaleFactor: 1 })
})
afterAll(async () => {
await page.close()
await browser.close()
await server.close()
})
it('should match screenshot', async () => {
await page.goto(`http://localhost:${PORT}`)
await page.addStyleTag({ content: globalCss })
const screenshot = await page.screenshot({ fullPage: true })
expect(screenshot).toMatchImageSnapshot({
failureThreshold: '0.06',
failureThresholdType: 'percent'
})
})
})