|
1 | 1 | import {expect, test} from '@jest/globals'
|
2 | 2 | import {getStatsDiff} from '../src/get-stats-diff'
|
3 | 3 | import {getChunkModuleDiff} from '../src/get-chunk-module-diff'
|
4 |
| -import {getCommentBody} from '../src/to-comment-body' |
5 | 4 | import {
|
6 | 5 | printAssetTablesByGroup,
|
7 | 6 | printChunkModulesTable,
|
8 | 7 | printTotalAssetTable
|
9 | 8 | } from '../src/print-markdown'
|
10 | 9 | import {AssetDiff} from '../src/types'
|
| 10 | +import {readFile} from 'node:fs/promises' |
| 11 | +import {resolve} from 'node:path' |
| 12 | +import {StatsCompilation} from 'webpack' |
11 | 13 |
|
12 |
| -test('Shows stats when files are removed', () => { |
| 14 | +async function readJsonFile(path: string): Promise<StatsCompilation> { |
| 15 | + const data = await readFile(resolve(__dirname, path), 'utf8') |
| 16 | + return JSON.parse(data) |
| 17 | +} |
| 18 | + |
| 19 | +test('Shows stats when files are removed', async () => { |
13 | 20 | const statsDiff = getStatsDiff(
|
14 |
| - require('./__mocks__/old-stats-assets.json'), |
15 |
| - require('./__mocks__/new-stats-assets.json') |
| 21 | + await readJsonFile('./__mocks__/old-stats-assets.json'), |
| 22 | + await readJsonFile('./__mocks__/new-stats-assets.json') |
16 | 23 | )
|
17 | 24 |
|
18 | 25 | expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
|
19 | 26 | expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
|
20 | 27 | })
|
21 | 28 |
|
22 |
| -test('Shows stats when files are added', () => { |
| 29 | +test('Shows stats when files are added', async () => { |
23 | 30 | const statsDiff = getStatsDiff(
|
24 |
| - require('./__mocks__/new-stats-assets.json'), |
25 |
| - require('./__mocks__/old-stats-assets.json') |
| 31 | + await readJsonFile('./__mocks__/new-stats-assets.json'), |
| 32 | + await readJsonFile('./__mocks__/old-stats-assets.json') |
26 | 33 | )
|
27 | 34 |
|
28 | 35 | expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
|
29 | 36 | expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
|
30 | 37 | })
|
31 | 38 |
|
32 |
| -test('Shows stats when files are unchanged', () => { |
| 39 | +test('Shows stats when files are unchanged', async () => { |
33 | 40 | const statsDiff = getStatsDiff(
|
34 |
| - require('./__mocks__/old-stats-assets.json'), |
35 |
| - require('./__mocks__/old-stats-assets.json') |
| 41 | + await readJsonFile('./__mocks__/old-stats-assets.json'), |
| 42 | + await readJsonFile('./__mocks__/old-stats-assets.json') |
36 | 43 | )
|
37 | 44 |
|
38 | 45 | expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
|
39 | 46 | expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
|
40 | 47 | })
|
41 | 48 |
|
42 |
| -test('computes the correct module diff information', () => { |
| 49 | +test('computes the correct module diff information', async () => { |
43 | 50 | const statsDiff = getChunkModuleDiff(
|
44 |
| - require('./__mocks__/old-stats-with-chunks.json'), |
45 |
| - require('./__mocks__/new-stats-with-chunks.json') |
| 51 | + await readJsonFile('./__mocks__/old-stats-with-chunks.json'), |
| 52 | + await readJsonFile('./__mocks__/new-stats-with-chunks.json') |
46 | 53 | )
|
47 | 54 |
|
48 | 55 | expect(statsDiff?.added).toContainEqual({
|
@@ -78,28 +85,28 @@ test('computes the correct module diff information', () => {
|
78 | 85 | expect(statsDiff?.total.diffPercentage).toEqual(0)
|
79 | 86 | })
|
80 | 87 |
|
81 |
| -test('displays module information when files are added/removed/changed', () => { |
| 88 | +test('displays module information when files are added/removed/changed', async () => { |
82 | 89 | const statsDiff = getChunkModuleDiff(
|
83 |
| - require('./__mocks__/old-stats-with-chunks.json'), |
84 |
| - require('./__mocks__/new-stats-with-chunks.json') |
| 90 | + await readJsonFile('./__mocks__/old-stats-with-chunks.json'), |
| 91 | + await readJsonFile('./__mocks__/new-stats-with-chunks.json') |
85 | 92 | )
|
86 | 93 |
|
87 | 94 | expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
|
88 | 95 | })
|
89 | 96 |
|
90 |
| -test('displays no module information when unchanged', () => { |
| 97 | +test('displays no module information when unchanged', async () => { |
91 | 98 | const statsDiff = getChunkModuleDiff(
|
92 |
| - require('./__mocks__/old-stats-with-chunks.json'), |
93 |
| - require('./__mocks__/old-stats-with-chunks.json') |
| 99 | + await readJsonFile('./__mocks__/old-stats-with-chunks.json'), |
| 100 | + await readJsonFile('./__mocks__/old-stats-with-chunks.json') |
94 | 101 | )
|
95 | 102 |
|
96 | 103 | expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
|
97 | 104 | })
|
98 | 105 |
|
99 |
| -test('does not display module information when it does not exist', () => { |
| 106 | +test('does not display module information when it does not exist', async () => { |
100 | 107 | const statsDiff = getChunkModuleDiff(
|
101 |
| - require('./__mocks__/old-stats-assets.json'), |
102 |
| - require('./__mocks__/old-stats-assets.json') |
| 108 | + await readJsonFile('./__mocks__/old-stats-assets.json'), |
| 109 | + await readJsonFile('./__mocks__/old-stats-assets.json') |
103 | 110 | )
|
104 | 111 |
|
105 | 112 | expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
|
|
0 commit comments