-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.test.ts
More file actions
140 lines (121 loc) · 4.91 KB
/
main.test.ts
File metadata and controls
140 lines (121 loc) · 4.91 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
import {expect, test} from '@jest/globals'
import {getStatsDiff} from '../src/get-stats-diff'
import {getChunkModuleDiff} from '../src/get-chunk-module-diff'
import {
printAssetTablesByGroup,
printChunkModulesTable,
printTotalAssetTable
} from '../src/print-markdown'
import {AssetDiff} from '../src/types'
import {readFile} from 'node:fs/promises'
import {resolve} from 'node:path'
import {StatsCompilation} from 'webpack'
async function readJsonFile(path: string): Promise<StatsCompilation> {
const data = await readFile(resolve(__dirname, path), 'utf8')
return JSON.parse(data)
}
test('Shows stats when files are removed', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})
test('Shows stats when files are added', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/new-stats-assets.json'),
await readJsonFile('./__mocks__/old-stats-assets.json')
)
expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})
test('Shows stats when files are unchanged', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/old-stats-assets.json')
)
expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})
test('Shows stats when files are hidden', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-hidden-stats-assets.json'),
await readJsonFile('./__mocks__/new-hidden-stats-assets.json')
)
expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})
test('computes the correct module diff information', async () => {
const statsDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
await readJsonFile('./__mocks__/new-stats-with-chunks.json')
)
expect(statsDiff?.added).toContainEqual({
name: './src/client/this-file-was-added.ts',
diff: 1496,
diffPercentage: Infinity,
new: {size: 1496, gzipSize: NaN},
old: {size: 0, gzipSize: 0}
} as AssetDiff)
expect(statsDiff?.bigger).toContainEqual({
name: './src/client/this-file-grew-larger.tsx',
diff: 200,
diffPercentage: 35.58719,
new: {size: 762, gzipSize: NaN},
old: {size: 562, gzipSize: NaN}
} as AssetDiff)
expect(statsDiff?.smaller).toContainEqual({
name: './src/client/helpers/this-file-grew-smaller.ts',
diff: -200,
diffPercentage: -7.94281,
new: {size: 2318, gzipSize: NaN},
old: {size: 2518, gzipSize: NaN}
} as AssetDiff)
expect(statsDiff?.removed).toContainEqual({
name: './src/client/this-file-will-be-deleted.ts',
diff: -1496,
diffPercentage: -100,
new: {size: 0, gzipSize: 0},
old: {size: 1496, gzipSize: NaN}
} as AssetDiff)
expect(statsDiff?.total.new).toEqual(statsDiff?.total.old)
expect(statsDiff?.total.diff).toEqual(0)
expect(statsDiff?.total.diffPercentage).toEqual(0)
// No async chunks results in no initial stats being reported
expect(statsDiff?.initial).toBe(undefined)
// TODO(mariaines): add test case for initial stats for a build with async chunks
})
// TODO(mariaines): update this snapshot for testing initial stats
test('Shows initial stats for builds with async chunks', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
const chunkModuleDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
await readJsonFile('./__mocks__/new-stats-with-chunks.json')
)
expect(printTotalAssetTable(statsDiff, chunkModuleDiff)).toMatchSnapshot()
})
test('displays module information when files are added/removed/changed', async () => {
const statsDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
await readJsonFile('./__mocks__/new-stats-with-chunks.json')
)
expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
})
test('displays no module information when unchanged', async () => {
const statsDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
await readJsonFile('./__mocks__/old-stats-with-chunks.json')
)
expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
})
test('does not display module information when it does not exist', async () => {
const statsDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/old-stats-assets.json')
)
expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
})