1
- import {
2
- AST_NODE_TYPES ,
3
- TSESLint ,
4
- TSESTree ,
5
- } from '@typescript-eslint/experimental-utils' ;
1
+ import { TSESLint } from '@typescript-eslint/experimental-utils' ;
6
2
import resolveFrom from 'resolve-from' ;
7
3
import rule from '../no-large-snapshots' ;
8
4
9
- const noLargeSnapshots = rule . create . bind ( rule ) ;
10
-
11
5
const ruleTester = new TSESLint . RuleTester ( {
12
6
parser : resolveFrom ( require . resolve ( 'eslint' ) , 'espree' ) ,
13
7
parserOptions : {
@@ -71,12 +65,12 @@ ruleTester.run('no-large-snapshots', rule, {
71
65
code : generateExportsSnapshotString ( 20 ) ,
72
66
} ,
73
67
{
74
- // "it should not report whitelisted large snapshots "
68
+ // "it should not report snapshots that are allowed to be large "
75
69
filename : '/mock-component.jsx.snap' ,
76
70
code : generateExportsSnapshotString ( 58 ) ,
77
71
options : [
78
72
{
79
- whitelistedSnapshots : {
73
+ allowedSnapshots : {
80
74
'/mock-component.jsx.snap' : [ 'a big component 1' ] ,
81
75
} ,
82
76
} ,
@@ -178,13 +172,12 @@ ruleTester.run('no-large-snapshots', rule, {
178
172
] ,
179
173
} ,
180
174
{
181
- // "it should report if file is not whitelisted "
175
+ // "it should report if file is not allowed "
182
176
filename : '/mock-component.jsx.snap' ,
183
- // code: generateExportsSnapshotString(58),
184
177
code : generateExportsSnapshotString ( 58 ) ,
185
178
options : [
186
179
{
187
- whitelistedSnapshots : {
180
+ allowedSnapshots : {
188
181
'/another-mock-component.jsx.snap' : [ / a b i g c o m p o n e n t \d + / u] ,
189
182
} ,
190
183
} ,
@@ -196,6 +189,27 @@ ruleTester.run('no-large-snapshots', rule, {
196
189
} ,
197
190
] ,
198
191
} ,
192
+ {
193
+ // "should not report allowed large snapshots based on regexp"
194
+ filename : '/mock-component.jsx.snap' ,
195
+ code : [
196
+ generateExportsSnapshotString ( 58 , 'a big component w/ text' ) ,
197
+ generateExportsSnapshotString ( 58 , 'a big component 2' ) ,
198
+ ] . join ( '\n\n' ) ,
199
+ options : [
200
+ {
201
+ allowedSnapshots : {
202
+ '/mock-component.jsx.snap' : [ / a b i g c o m p o n e n t \d + / u] ,
203
+ } ,
204
+ } ,
205
+ ] ,
206
+ errors : [
207
+ {
208
+ messageId : 'tooLongSnapshots' ,
209
+ data : { lineLimit : 50 , lineCount : 58 } ,
210
+ } ,
211
+ ] ,
212
+ } ,
199
213
{
200
214
// "should not report whitelisted large snapshots based on regexp"
201
215
filename : '/mock-component.jsx.snap' ,
@@ -218,7 +232,26 @@ ruleTester.run('no-large-snapshots', rule, {
218
232
] ,
219
233
} ,
220
234
{
221
- // "should not report whitelisted large snapshots based on regexp"
235
+ filename : '/mock-component.jsx.snap' ,
236
+ code : [
237
+ generateExportsSnapshotString ( 58 , 'a big component w/ text' ) ,
238
+ generateExportsSnapshotString ( 58 , 'a big component 2' ) ,
239
+ ] . join ( '\n\n' ) ,
240
+ options : [
241
+ {
242
+ allowedSnapshots : {
243
+ '/mock-component.jsx.snap' : [ 'a big component 2' ] ,
244
+ } ,
245
+ } ,
246
+ ] ,
247
+ errors : [
248
+ {
249
+ messageId : 'tooLongSnapshots' ,
250
+ data : { lineLimit : 50 , lineCount : 58 } ,
251
+ } ,
252
+ ] ,
253
+ } ,
254
+ {
222
255
filename : '/mock-component.jsx.snap' ,
223
256
code : [
224
257
generateExportsSnapshotString ( 58 , 'a big component w/ text' ) ,
@@ -242,47 +275,31 @@ ruleTester.run('no-large-snapshots', rule, {
242
275
} ) ;
243
276
244
277
describe ( 'no-large-snapshots' , ( ) => {
245
- const buildBaseNode = < Type extends AST_NODE_TYPES > (
246
- type : Type ,
247
- ) : TSESTree . BaseNode & { type : Type } => ( {
248
- type,
249
- range : [ 0 , 1 ] ,
250
- loc : {
251
- start : { line : 1 , column : 0 } ,
252
- end : { line : 1 , column : 1 } ,
253
- } ,
254
- } ) ;
255
-
256
- describe ( 'when "whitelistedSnapshots" option contains relative paths' , ( ) => {
278
+ describe ( 'when "allowedSnapshots" option contains relative paths' , ( ) => {
257
279
it ( 'should throw an exception' , ( ) => {
258
- const { ExpressionStatement = ( ) => { } } = noLargeSnapshots ( {
259
- id : 'my-id' ,
260
- getFilename : ( ) => '/mock-component.jsx.snap' ,
261
- options : [
280
+ expect ( ( ) => {
281
+ const linter = new TSESLint . Linter ( ) ;
282
+
283
+ linter . defineRule ( 'no-large-snapshots' , rule ) ;
284
+
285
+ linter . verify (
286
+ 'console.log()' ,
262
287
{
263
- whitelistedSnapshots : {
264
- 'mock-component.jsx.snap' : [ / a b i g c o m p o n e n t \d + / u] ,
288
+ rules : {
289
+ 'no-large-snapshots' : [
290
+ 'error' ,
291
+ {
292
+ allowedSnapshots : {
293
+ 'mock-component.jsx.snap' : [ / a b i g c o m p o n e n t \d + / u] ,
294
+ } ,
295
+ } ,
296
+ ] ,
265
297
} ,
266
298
} ,
267
- ] ,
268
- parserOptions : { } ,
269
- parserPath : '' ,
270
- settings : { } ,
271
- getAncestors : ( ) => [ ] ,
272
- getDeclaredVariables : ( ) => [ ] ,
273
- getScope : jest . fn ( ) ,
274
- getSourceCode : jest . fn ( ) ,
275
- markVariableAsUsed : ( ) => false ,
276
- report : jest . fn ( ) ,
277
- } ) ;
278
-
279
- expect ( ( ) =>
280
- ExpressionStatement ( {
281
- ...buildBaseNode ( AST_NODE_TYPES . ExpressionStatement ) ,
282
- expression : buildBaseNode ( AST_NODE_TYPES . JSXClosingFragment ) ,
283
- } ) ,
284
- ) . toThrow (
285
- 'All paths for whitelistedSnapshots must be absolute. You can use JS config and `path.resolve`' ,
299
+ 'mock-component.jsx.snap' ,
300
+ ) ;
301
+ } ) . toThrow (
302
+ 'All paths for allowedSnapshots must be absolute. You can use JS config and `path.resolve`' ,
286
303
) ;
287
304
} ) ;
288
305
} ) ;
0 commit comments