-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsqip-cli-e2e.test.ts
More file actions
142 lines (122 loc) · 3.88 KB
/
sqip-cli-e2e.test.ts
File metadata and controls
142 lines (122 loc) · 3.88 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { resolve } from 'path'
import { tmpdir } from 'os'
import { stat, remove, readFile } from 'fs-extra'
import { load as cheerioLoad } from 'cheerio'
import { vi } from 'vitest'
import { execa } from 'execa'
const inputFile = resolve(
__dirname,
'..',
'..',
'..',
'..',
'__tests__',
'fixtures',
'beach.jpg'
)
const outputFile = resolve(process.cwd(), 'beach.svg')
const cliPath = resolve(__dirname, '..', '..', 'dist', 'wrapper.js')
const cliCmd = `node`
vi.setConfig({ testTimeout: 20000 })
function isValidStdout(stdout: string) {
expect(stdout).toMatch(/Processing: \/([A-z0-9-_+]+\/)*[A-z0-9-_]+\.jpg/)
expect(stdout).toMatch(/Stored at: \/([A-z0-9-_+]+\/)*[A-z0-9-_]+\.svg/)
expect(stdout).toMatch(/originalWidth.+originalHeight.+width.+height.+type/)
expect(stdout).toMatch(
/Vibrant.+DarkVibrant.+LightVibrant.+Muted.+DarkMuted.+LightMuted/
)
}
describe('cli api', () => {
test('no config exists programm and shows help', async () => {
await expect(() =>
execa(cliCmd, [cliPath], {
stripFinalNewline: true,
stdin: 'ignore'
})
).rejects.toThrow('Please provide the following arguments: input')
})
test('--help shows help screen to user', async () => {
const { stdout } = await execa(cliCmd, [cliPath, '--help'], {
stripFinalNewline: true
})
// Normalize whitespace: strip ANSI codes and collapse runs of spaces
// to a single space so output doesn't depend on terminal width
const normalized = stdout
// eslint-disable-next-line no-control-regex
.replace(/\x1b\[[0-9;]*m/g, '')
.replace(/[^\S\n]+/g, ' ')
.replace(/ $/gm, '')
expect(normalized).toMatchSnapshot()
})
test('--silent disables logging to stdout', async () => {
const { stdout } = await execa(
cliCmd,
[cliPath, '--input', inputFile, '--silent', '-p', 'pixels'],
{
stripFinalNewline: true
}
)
expect(stdout).toMatch('')
})
test('basic svg structure is applied', async () => {
const { stdout } = await execa(
cliCmd,
[cliPath, '-i', inputFile, '-p', 'pixels'],
{
stripFinalNewline: true
}
)
isValidStdout(stdout)
// Does the new file exist
expect(await stat(outputFile)).toBeTruthy()
const content = await readFile(outputFile)
const $ = cheerioLoad(content, { xml: true })
// File content is actually a parseable svg
expect($('svg')).toHaveLength(1)
await remove(outputFile)
})
test('-o saves file to given path', async () => {
const tmpOutputFile = resolve(
tmpdir(),
`sqip-e2e-test-${new Date().getTime()}.svg`
)
const { stdout } = await execa(
cliCmd,
[cliPath, '-i', inputFile, '-o', tmpOutputFile, '-p', 'pixels'],
{
stripFinalNewline: true
}
)
isValidStdout(stdout)
// Does the new file exist
expect(await stat(tmpOutputFile)).toBeTruthy()
await remove(tmpOutputFile)
})
test('reads image from stdin and prints SVG', async () => {
const { stdout } = await execa(
'cat',
[inputFile, '|', cliCmd, cliPath, '-p', 'pixels'],
{ shell: true, stripFinalNewline: true }
)
// stdin mode defaults to --print, so stdout should contain an SVG
expect(stdout).toContain('<svg')
expect(stdout).toContain('</svg>')
})
test('stdin with -o writes file to given path', async () => {
const tmpOutputFile = resolve(
tmpdir(),
`sqip-stdin-e2e-test-${new Date().getTime()}.svg`
)
await execa(
'cat',
[inputFile, '|', cliCmd, cliPath, '-p', 'pixels', '-o', tmpOutputFile],
{ shell: true, stripFinalNewline: true }
)
// Does the new file exist
expect(await stat(tmpOutputFile)).toBeTruthy()
const content = await readFile(tmpOutputFile)
const $ = cheerioLoad(content, { xml: true })
expect($('svg')).toHaveLength(1)
await remove(tmpOutputFile)
})
})