@@ -25,68 +25,80 @@ export const buildSnapshotResolver = (
25
25
function createSnapshotResolver ( snapshotResolverPath : ?Path ) : SnapshotResolver {
26
26
return typeof snapshotResolverPath === 'string'
27
27
? createCustomSnapshotResolver ( snapshotResolverPath )
28
- : {
29
- resolveSnapshotPath : ( testPath : Path ) =>
30
- path . join (
31
- path . join ( path . dirname ( testPath ) , '__snapshots__' ) ,
32
- path . basename ( testPath ) + DOT_EXTENSION ,
33
- ) ,
28
+ : createDefaultSnapshotResolver ( ) ;
29
+ }
30
+
31
+ function createDefaultSnapshotResolver ( ) {
32
+ return {
33
+ resolveSnapshotPath : ( testPath : Path ) =>
34
+ path . join (
35
+ path . join ( path . dirname ( testPath ) , '__snapshots__' ) ,
36
+ path . basename ( testPath ) + DOT_EXTENSION ,
37
+ ) ,
38
+
39
+ resolveTestPath : ( snapshotPath : Path ) =>
40
+ path . resolve (
41
+ path . dirname ( snapshotPath ) ,
42
+ '..' ,
43
+ path . basename ( snapshotPath , DOT_EXTENSION ) ,
44
+ ) ,
34
45
35
- resolveTestPath : ( snapshotPath : Path ) =>
36
- path . resolve (
37
- path . dirname ( snapshotPath ) ,
38
- '..' ,
39
- path . basename ( snapshotPath , DOT_EXTENSION ) ,
40
- ) ,
41
- } ;
46
+ testPathForConsistencyCheck : path . posix . join (
47
+ 'consistency_check' ,
48
+ '__tests__' ,
49
+ 'example.test.js' ,
50
+ ) ,
51
+ } ;
42
52
}
43
53
44
54
function createCustomSnapshotResolver (
45
55
snapshotResolverPath : Path ,
46
56
) : SnapshotResolver {
47
57
const custom = ( require ( snapshotResolverPath ) : SnapshotResolver ) ;
48
58
49
- if ( typeof custom . resolveSnapshotPath !== 'function' ) {
50
- throw new TypeError ( mustImplement ( 'resolveSnapshotPath' ) ) ;
51
- }
52
- if ( typeof custom . resolveTestPath !== 'function' ) {
53
- throw new TypeError ( mustImplement ( 'resolveTestPath' ) ) ;
54
- }
59
+ [
60
+ [ 'resolveSnapshotPath' , 'function' ] ,
61
+ [ 'resolveTestPath' , 'function' ] ,
62
+ [ 'testPathForConsistencyCheck' , 'string' ] ,
63
+ ] . forEach ( ( [ propName , requiredType ] ) => {
64
+ if ( typeof custom [ propName ] !== requiredType ) {
65
+ throw new TypeError ( mustImplement ( propName , requiredType ) ) ;
66
+ }
67
+ } ) ;
55
68
56
69
const customResolver = {
57
70
resolveSnapshotPath : testPath =>
58
71
custom . resolveSnapshotPath ( testPath , DOT_EXTENSION ) ,
59
72
resolveTestPath : snapshotPath =>
60
73
custom . resolveTestPath ( snapshotPath , DOT_EXTENSION ) ,
74
+ testPathForConsistencyCheck : custom . testPathForConsistencyCheck ,
61
75
} ;
62
76
63
77
verifyConsistentTransformations ( customResolver ) ;
64
78
65
79
return customResolver ;
66
80
}
67
81
68
- function mustImplement ( functionName : string ) {
82
+ function mustImplement ( propName : string , requiredType : string ) {
69
83
return (
70
84
chalk . bold (
71
- `Custom snapshot resolver must implement a \`${ functionName } \` function .` ,
85
+ `Custom snapshot resolver must implement a \`${ propName } \` as a ${ requiredType } .` ,
72
86
) +
73
87
'\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
74
88
) ;
75
89
}
76
90
77
91
function verifyConsistentTransformations ( custom : SnapshotResolver ) {
78
- const fakeTestPath = path . posix . join (
79
- 'some-path' ,
80
- '__tests__' ,
81
- 'snapshot_resolver.test.js' ,
82
- ) ;
83
- const transformedPath = custom . resolveTestPath (
84
- custom . resolveSnapshotPath ( fakeTestPath ) ,
92
+ const resolvedSnapshotPath = custom . resolveSnapshotPath (
93
+ custom . testPathForConsistencyCheck ,
85
94
) ;
86
- if ( transformedPath !== fakeTestPath ) {
95
+ const resolvedTestPath = custom . resolveTestPath ( resolvedSnapshotPath ) ;
96
+ if ( resolvedTestPath !== custom . testPathForConsistencyCheck ) {
87
97
throw new Error (
88
98
chalk . bold (
89
- `Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${ fakeTestPath } ')) === ${ transformedPath } ` ,
99
+ `Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${
100
+ custom . testPathForConsistencyCheck
101
+ } ')) === ${ resolvedTestPath } `,
90
102
) ,
91
103
) ;
92
104
}
0 commit comments