1
+ import { createHash } from 'node:crypto' ;
2
+
1
3
import type { TransformedSource } from '@jest/transform' ;
2
4
import { LogContexts , LogLevels , type Logger , createLogger } from 'bs-logger' ;
3
5
import { transformSync } from 'esbuild' ;
@@ -6,6 +8,41 @@ import { type TsJestTransformerOptions, ConfigSet, TsJestTransformer, type TsJes
6
8
import { NgJestCompiler } from './compiler/ng-jest-compiler' ;
7
9
import { NgJestConfig } from './config/ng-jest-config' ;
8
10
11
+ // stores hashes made out of only one argument being a string
12
+ const cache : Record < string , string > = { } ;
13
+
14
+ type DataItem = string | Buffer ;
15
+
16
+ const sha1 = ( ...data : DataItem [ ] ) : string => {
17
+ const canCache = data . length === 1 && typeof data [ 0 ] === 'string' ;
18
+ // caching
19
+ let cacheKey ! : string ;
20
+ if ( canCache ) {
21
+ cacheKey = data [ 0 ] as string ;
22
+ if ( cacheKey in cache ) {
23
+ return cache [ cacheKey ] ;
24
+ }
25
+ }
26
+
27
+ // we use SHA1 because it's the fastest provided by node
28
+ // and we are not concerned about security here
29
+ const hash = createHash ( 'sha1' ) ;
30
+ data . forEach ( ( item ) => {
31
+ if ( typeof item === 'string' ) {
32
+ hash . update ( item , 'utf8' ) ;
33
+ } else {
34
+ hash . update ( item ) ;
35
+ }
36
+ } ) ;
37
+ const res = hash . digest ( 'hex' ) . toString ( ) ;
38
+
39
+ if ( canCache ) {
40
+ cache [ cacheKey ] = res ;
41
+ }
42
+
43
+ return res ;
44
+ } ;
45
+
9
46
export class NgJestTransformer extends TsJestTransformer {
10
47
readonly #ngJestLogger: Logger ;
11
48
@@ -15,8 +52,7 @@ export class NgJestTransformer extends TsJestTransformer {
15
52
context : {
16
53
[ LogContexts . package ] : 'jest-preset-angular' ,
17
54
[ LogContexts . logLevel ] : LogLevels . trace ,
18
- // eslint-disable-next-line @typescript-eslint/no-require-imports
19
- version : require ( '../package.json' ) . version ,
55
+ version : this . version ,
20
56
} ,
21
57
targets : process . env . NG_JEST_LOG ?? undefined ,
22
58
} ) ;
@@ -30,6 +66,11 @@ export class NgJestTransformer extends TsJestTransformer {
30
66
this . _compiler = new NgJestCompiler ( configSet , cacheFS ) ;
31
67
}
32
68
69
+ private get version ( ) : string {
70
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
71
+ return require ( '../package.json' ) . version ;
72
+ }
73
+
33
74
process ( fileContent : string , filePath : string , transformOptions : TsJestTransformOptions ) : TransformedSource {
34
75
// @ts -expect-error we are accessing the private cache to avoid creating new objects all the time
35
76
const configSet = super . _configsFor ( transformOptions ) ;
@@ -55,4 +96,8 @@ export class NgJestTransformer extends TsJestTransformer {
55
96
return super . process ( fileContent , filePath , transformOptions ) ;
56
97
}
57
98
}
99
+
100
+ getCacheKey ( fileContent : string , filePath : string , transformOptions : TsJestTransformOptions ) : string {
101
+ return sha1 ( super . getCacheKey ( fileContent , filePath , transformOptions ) , this . version ) ;
102
+ }
58
103
}
0 commit comments