-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrestrict.test.ts
135 lines (131 loc) · 3.71 KB
/
restrict.test.ts
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
/*
* Copyright (c) Trainline Limited, 2020. All rights reserved.
* See LICENSE.md in the project root for license information.
*/
import { defaultBasePluginConfig } from '../../config/PluginConfig';
import { Stats } from '../../types';
import normalizeStats from '../../helpers/normalizeStats';
import restrict, { RestrictedModule } from './restrict';
const normalizedStats = normalizeStats({
assets: [
{
name: 'file-1-hash.js',
size: 80,
chunks: [1],
chunkNames: ['file-1'],
emitted: true,
},
{
name: 'file-2-hash.js',
size: 80,
chunks: [1, 2],
chunkNames: ['file-2'],
emitted: true,
},
],
modules: [
{
name: './node_modules/lodash/_getNative.js',
issuerPath: [
{
name: './src/client/entry.jsx',
},
{
name: './src/common/component/some-component.jsx',
},
],
chunks: [1, 2],
},
{
name: './node_modules/lodash.omitby/index.js',
issuerPath: [
{
name: './src/client/another-entry.js',
},
{
name: './src/common/lib/some-lib.js',
},
],
chunks: [2],
},
{
name: './node_modules/not-restricted/index.js',
issuerPath: [
{
name: './src/client/another-entry.js',
},
],
chunks: [1],
},
],
} as Stats);
describe('restrict', () => {
it('returns the expected restricted files', () => {
expect(
restrict(normalizedStats, defaultBasePluginConfig.chunkFilename, [
{ search: 'lodash.omitby', responseType: 'error', message: 'no lodash.omitby' },
])
).toEqual<RestrictedModule[]>([
{
filename: './node_modules/lodash.omitby/index.js',
restriction: {
message: 'no lodash.omitby',
responseType: 'error',
search: 'lodash.omitby',
},
chunkNames: ['file-2.js'],
issuerPath: ['./src/client/another-entry.js', './src/common/lib/some-lib.js'],
},
]);
});
it('returns the expected restrict files when using regexp', () => {
expect(
restrict(normalizedStats, defaultBasePluginConfig.chunkFilename, [
{ search: 'lodash.+', responseType: 'error', message: 'no lodash components' },
])
).toEqual<RestrictedModule[]>([
{
filename: './node_modules/lodash.omitby/index.js',
restriction: {
message: 'no lodash components',
responseType: 'error',
search: 'lodash.+',
},
chunkNames: ['file-2.js'],
issuerPath: ['./src/client/another-entry.js', './src/common/lib/some-lib.js'],
},
{
filename: './node_modules/lodash/_getNative.js',
restriction: {
message: 'no lodash components',
responseType: 'error',
search: 'lodash.+',
},
chunkNames: ['file-1.js', 'file-2.js'],
issuerPath: ['./src/client/entry.jsx', './src/common/component/some-component.jsx'],
},
]);
});
it('returns unique results (for multi compile)', () => {
expect(
restrict(
normalizeStats({
children: [normalizedStats.original, normalizedStats.original],
} as Stats),
defaultBasePluginConfig.chunkFilename,
[{ search: 'lodash.omitby', responseType: 'error', message: 'no lodash.omitby' }]
)
).toEqual<RestrictedModule[]>([
{
filename: './node_modules/lodash.omitby/index.js',
restriction: {
message: 'no lodash.omitby',
responseType: 'error',
search: 'lodash.omitby',
},
chunkNames: ['file-2.js'],
issuerPath: ['./src/client/another-entry.js', './src/common/lib/some-lib.js'],
},
]);
});
});