Skip to content

Commit

Permalink
fix: include version from package.json into cache key
Browse files Browse the repository at this point in the history
Fixes #2844
  • Loading branch information
ahnpnl committed Nov 20, 2024
1 parent 07f0b3a commit c6b28e5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
e2e/**/.yarn/*
examples/**/.yarn/*
website/.yarn/*
tmp
.idx
.vscode
!.yarn/patches
Expand Down
24 changes: 12 additions & 12 deletions src/__snapshots__/ng-jest-transformer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -23,7 +23,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -42,7 +42,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -61,7 +61,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -80,7 +80,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -99,7 +99,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -118,7 +118,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -137,7 +137,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -156,7 +156,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -175,7 +175,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -194,7 +194,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand All @@ -213,7 +213,7 @@ exports[`NgJestTransformer should use esbuild to process mjs or \`node_modules\`
[
"
const pi = parseFloat(3.124);
export { pi };
",
{
Expand Down
34 changes: 28 additions & 6 deletions src/ng-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { transformSync } from 'esbuild';
import { TsJestTransformer } from 'ts-jest';

import packageJson from '../package.json';
import { NgJestCompiler } from './compiler/ng-jest-compiler';
import { NgJestConfig } from './config/ng-jest-config';
import { NgJestTransformer } from './ng-jest-transformer';
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'foo.js',
Expand All @@ -73,7 +74,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'node_modules/tslib.es6.js',
Expand Down Expand Up @@ -125,7 +126,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'foo.mjs',
Expand All @@ -134,7 +135,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'node_modules/foo.js',
Expand Down Expand Up @@ -184,7 +185,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'foo.mjs',
Expand All @@ -193,7 +194,7 @@ describe('NgJestTransformer', () => {
tr.process(
`
const pi = parseFloat(3.124);
export { pi };
`,
'node_modules/foo.js',
Expand All @@ -205,4 +206,25 @@ describe('NgJestTransformer', () => {

mockedTransformSync.mockClear();
});

it('should include version from package.json', async () => {
const transformCfg = {
config: {
cwd: process.cwd(),
extensionsToTreatAsEsm: [],
testMatch: [],
testRegex: [],
},
} as any; // eslint-disable-line @typescript-eslint/no-explicit-any
const tr = new NgJestTransformer({
isolatedModules: true,
});

packageJson.version = '1.0.0';
const cacheKey1 = tr.getCacheKey('export const foo = 1', 'file1.ts', transformCfg);
packageJson.version = '2.0.0';
const cacheKey2 = tr.getCacheKey('export const foo = 1', 'file1.ts', transformCfg);

expect(cacheKey1).not.toBe(cacheKey2);
});
});
49 changes: 47 additions & 2 deletions src/ng-jest-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createHash } from 'node:crypto';

import type { TransformedSource } from '@jest/transform';
import { LogContexts, LogLevels, type Logger, createLogger } from 'bs-logger';
import { transformSync } from 'esbuild';
Expand All @@ -6,6 +8,41 @@ import { type TsJestTransformerOptions, ConfigSet, TsJestTransformer, type TsJes
import { NgJestCompiler } from './compiler/ng-jest-compiler';
import { NgJestConfig } from './config/ng-jest-config';

// stores hashes made out of only one argument being a string
const cache: Record<string, string> = {};

type DataItem = string | Buffer;

const sha1 = (...data: DataItem[]): string => {
const canCache = data.length === 1 && typeof data[0] === 'string';
// caching
let cacheKey!: string;
if (canCache) {
cacheKey = data[0] as string;
if (cacheKey in cache) {
return cache[cacheKey];
}
}

// we use SHA1 because it's the fastest provided by node
// and we are not concerned about security here
const hash = createHash('sha1');
data.forEach((item) => {
if (typeof item === 'string') {
hash.update(item, 'utf8');
} else {
hash.update(item);
}
});
const res = hash.digest('hex').toString();

if (canCache) {
cache[cacheKey] = res;
}

return res;
};

export class NgJestTransformer extends TsJestTransformer {
readonly #ngJestLogger: Logger;

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

private get version(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('../package.json').version;
}

process(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): TransformedSource {
// @ts-expect-error we are accessing the private cache to avoid creating new objects all the time
const configSet = super._configsFor(transformOptions);
Expand All @@ -55,4 +96,8 @@ export class NgJestTransformer extends TsJestTransformer {
return super.process(fileContent, filePath, transformOptions);
}
}

getCacheKey(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): string {
return sha1(super.getCacheKey(fileContent, filePath, transformOptions), this.version);
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"noImplicitReturns": true,
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "Node",
"target": "ES2015",
"module": "CommonJS",
Expand Down

0 comments on commit c6b28e5

Please sign in to comment.