Skip to content

Commit de02c79

Browse files
committedNov 20, 2024·
fix: include version from package.json into cache key
Fixes #2844
1 parent 549d44b commit de02c79

5 files changed

+89
-20
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ node_modules
77
e2e/**/.yarn/*
88
examples/**/.yarn/*
99
website/.yarn/*
10+
tmp
1011
.idx
1112
.vscode
1213
!.yarn/patches

‎src/__snapshots__/ng-jest-transformer.spec.ts.snap

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
44
[
55
"
66
const pi = parseFloat(3.124);
7-
7+
88
export { pi };
99
",
1010
{
@@ -23,7 +23,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
2323
[
2424
"
2525
const pi = parseFloat(3.124);
26-
26+
2727
export { pi };
2828
",
2929
{
@@ -42,7 +42,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
4242
[
4343
"
4444
const pi = parseFloat(3.124);
45-
45+
4646
export { pi };
4747
",
4848
{
@@ -61,7 +61,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
6161
[
6262
"
6363
const pi = parseFloat(3.124);
64-
64+
6565
export { pi };
6666
",
6767
{
@@ -80,7 +80,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
8080
[
8181
"
8282
const pi = parseFloat(3.124);
83-
83+
8484
export { pi };
8585
",
8686
{
@@ -99,7 +99,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
9999
[
100100
"
101101
const pi = parseFloat(3.124);
102-
102+
103103
export { pi };
104104
",
105105
{
@@ -118,7 +118,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
118118
[
119119
"
120120
const pi = parseFloat(3.124);
121-
121+
122122
export { pi };
123123
",
124124
{
@@ -137,7 +137,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
137137
[
138138
"
139139
const pi = parseFloat(3.124);
140-
140+
141141
export { pi };
142142
",
143143
{
@@ -156,7 +156,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
156156
[
157157
"
158158
const pi = parseFloat(3.124);
159-
159+
160160
export { pi };
161161
",
162162
{
@@ -175,7 +175,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
175175
[
176176
"
177177
const pi = parseFloat(3.124);
178-
178+
179179
export { pi };
180180
",
181181
{
@@ -194,7 +194,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
194194
[
195195
"
196196
const pi = parseFloat(3.124);
197-
197+
198198
export { pi };
199199
",
200200
{
@@ -213,7 +213,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
213213
[
214214
"
215215
const pi = parseFloat(3.124);
216-
216+
217217
export { pi };
218218
",
219219
{

‎src/ng-jest-transformer.spec.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { transformSync } from 'esbuild';
22
import { TsJestTransformer } from 'ts-jest';
33

4+
import packageJson from '../package.json';
45
import { NgJestCompiler } from './compiler/ng-jest-compiler';
56
import { NgJestConfig } from './config/ng-jest-config';
67
import { NgJestTransformer } from './ng-jest-transformer';
@@ -49,7 +50,7 @@ describe('NgJestTransformer', () => {
4950
tr.process(
5051
`
5152
const pi = parseFloat(3.124);
52-
53+
5354
export { pi };
5455
`,
5556
'foo.js',
@@ -73,7 +74,7 @@ describe('NgJestTransformer', () => {
7374
tr.process(
7475
`
7576
const pi = parseFloat(3.124);
76-
77+
7778
export { pi };
7879
`,
7980
'node_modules/tslib.es6.js',
@@ -125,7 +126,7 @@ describe('NgJestTransformer', () => {
125126
tr.process(
126127
`
127128
const pi = parseFloat(3.124);
128-
129+
129130
export { pi };
130131
`,
131132
'foo.mjs',
@@ -134,7 +135,7 @@ describe('NgJestTransformer', () => {
134135
tr.process(
135136
`
136137
const pi = parseFloat(3.124);
137-
138+
138139
export { pi };
139140
`,
140141
'node_modules/foo.js',
@@ -184,7 +185,7 @@ describe('NgJestTransformer', () => {
184185
tr.process(
185186
`
186187
const pi = parseFloat(3.124);
187-
188+
188189
export { pi };
189190
`,
190191
'foo.mjs',
@@ -193,7 +194,7 @@ describe('NgJestTransformer', () => {
193194
tr.process(
194195
`
195196
const pi = parseFloat(3.124);
196-
197+
197198
export { pi };
198199
`,
199200
'node_modules/foo.js',
@@ -205,4 +206,25 @@ describe('NgJestTransformer', () => {
205206

206207
mockedTransformSync.mockClear();
207208
});
209+
210+
it('should include version from package.json', async () => {
211+
const transformCfg = {
212+
config: {
213+
cwd: process.cwd(),
214+
extensionsToTreatAsEsm: [],
215+
testMatch: [],
216+
testRegex: [],
217+
},
218+
} as any; // eslint-disable-line @typescript-eslint/no-explicit-any
219+
const tr = new NgJestTransformer({
220+
isolatedModules: true,
221+
});
222+
223+
packageJson.version = '1.0.0';
224+
const cacheKey1 = tr.getCacheKey('export const foo = 1', 'file1.ts', transformCfg);
225+
packageJson.version = '2.0.0';
226+
const cacheKey2 = tr.getCacheKey('export const foo = 1', 'file1.ts', transformCfg);
227+
228+
expect(cacheKey1).not.toBe(cacheKey2);
229+
});
208230
});

‎src/ng-jest-transformer.ts

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createHash } from 'node:crypto';
2+
13
import type { TransformedSource } from '@jest/transform';
24
import { LogContexts, LogLevels, type Logger, createLogger } from 'bs-logger';
35
import { transformSync } from 'esbuild';
@@ -6,6 +8,41 @@ import { type TsJestTransformerOptions, ConfigSet, TsJestTransformer, type TsJes
68
import { NgJestCompiler } from './compiler/ng-jest-compiler';
79
import { NgJestConfig } from './config/ng-jest-config';
810

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+
946
export class NgJestTransformer extends TsJestTransformer {
1047
readonly #ngJestLogger: Logger;
1148

@@ -15,8 +52,7 @@ export class NgJestTransformer extends TsJestTransformer {
1552
context: {
1653
[LogContexts.package]: 'jest-preset-angular',
1754
[LogContexts.logLevel]: LogLevels.trace,
18-
// eslint-disable-next-line @typescript-eslint/no-require-imports
19-
version: require('../package.json').version,
55+
version: this.version,
2056
},
2157
targets: process.env.NG_JEST_LOG ?? undefined,
2258
});
@@ -30,6 +66,11 @@ export class NgJestTransformer extends TsJestTransformer {
3066
this._compiler = new NgJestCompiler(configSet, cacheFS);
3167
}
3268

69+
private get version(): string {
70+
// eslint-disable-next-line @typescript-eslint/no-require-imports
71+
return require('../package.json').version;
72+
}
73+
3374
process(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): TransformedSource {
3475
// @ts-expect-error we are accessing the private cache to avoid creating new objects all the time
3576
const configSet = super._configsFor(transformOptions);
@@ -55,4 +96,8 @@ export class NgJestTransformer extends TsJestTransformer {
5596
return super.process(fileContent, filePath, transformOptions);
5697
}
5798
}
99+
100+
getCacheKey(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): string {
101+
return sha1(super.getCacheKey(fileContent, filePath, transformOptions), this.version);
102+
}
58103
}

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"noImplicitReturns": true,
1212
"skipLibCheck": true,
1313
"esModuleInterop": true,
14+
"resolveJsonModule": true,
1415
"moduleResolution": "Node",
1516
"target": "ES2015",
1617
"module": "CommonJS",

0 commit comments

Comments
 (0)
Please sign in to comment.