Skip to content

Commit 051d524

Browse files
committed
fix: handle processWithEsbuild transform option
When we migrate from `globals` to transform option, the object `ngJest` wasn't copied by `ts-jest` parent class `ConfigSet`. This fix adds the handling option `processWithEsbuild` directly to `NgJestTransformer` class to manage it properly and add a deprecation warning to `globals.ngJest.processWithEsbuild` option
1 parent 78e92bf commit 051d524

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/config/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { TsJestTransformerOptions } from 'ts-jest';
2+
3+
export type NgJestTransformerOptions = TsJestTransformerOptions & {
4+
processWithEsbuild?: string[];
5+
};

src/config/ng-jest-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export class NgJestConfig extends ConfigSet {
1414
constructor(jestConfig: TsJestTransformOptions['config'] | undefined, parentLogger?: Logger | undefined) {
1515
super(jestConfig, parentLogger);
1616
const jestGlobalsConfig = jestConfig?.globals?.ngJest ?? Object.create(null);
17+
if (jestGlobalsConfig.processWithEsbuild) {
18+
this.logger.warn(
19+
'Specifying `processWithEsbuild` in `ngJest` config is deprecated and will be removed in the next major version. Please follow this documentation https://thymikee.github.io/jest-preset-angular/docs/getting-started/options#processing-with-esbuild instead.',
20+
);
21+
}
1722
this.processWithEsbuild = globsToMatcher([
1823
...(jestGlobalsConfig.processWithEsbuild ?? []),
1924
...defaultProcessWithEsbuildPatterns,

src/ng-jest-transformer.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ describe('NgJestTransformer', () => {
9898
expect(mockedTransformSync).not.toHaveBeenCalled();
9999
});
100100

101+
test('should use esbuild to process files defined via "processWithEsbuild" transform option', () => {
102+
const tr = new NgJestTransformer({
103+
processWithEsbuild: ['foo.js'],
104+
});
105+
tr.process(
106+
`
107+
const pi = parseFloat(3.124);
108+
109+
export { pi };
110+
`,
111+
'foo.js',
112+
{
113+
config: {
114+
cwd: process.cwd(),
115+
extensionsToTreatAsEsm: [],
116+
testMatch: [],
117+
testRegex: [],
118+
},
119+
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any,
120+
);
121+
122+
expect(mockedTransformSync).toHaveBeenCalled();
123+
124+
mockedTransformSync.mockClear();
125+
});
126+
101127
test.each([
102128
{
103129
sourceMap: false,

src/ng-jest-transformer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { createHash } from 'node:crypto';
33
import type { TransformedSource } from '@jest/transform';
44
import { LogContexts, LogLevels, type Logger, createLogger } from 'bs-logger';
55
import { transformSync } from 'esbuild';
6-
import { type TsJestTransformerOptions, ConfigSet, TsJestTransformer, type TsJestTransformOptions } from 'ts-jest';
6+
import { globsToMatcher } from 'jest-util';
7+
import { ConfigSet, TsJestTransformer, type TsJestTransformOptions } from 'ts-jest';
78
import { updateOutput } from 'ts-jest/dist/legacy/compiler/compiler-utils';
89
import type { TranspileOutput } from 'typescript';
910

1011
import { NgJestCompiler } from './compiler/ng-jest-compiler';
12+
import type { NgJestTransformerOptions } from './config/config';
1113
import { NgJestConfig } from './config/ng-jest-config';
1214

1315
// stores hashes made out of only one argument being a string
@@ -47,8 +49,10 @@ const sha1 = (...data: DataItem[]): string => {
4749

4850
export class NgJestTransformer extends TsJestTransformer {
4951
readonly #ngJestLogger: Logger;
52+
readonly #processWithEsbuild: ReturnType<typeof globsToMatcher>;
5053

51-
constructor(tsJestConfig?: TsJestTransformerOptions) {
54+
constructor(transformerConfig?: NgJestTransformerOptions) {
55+
const { processWithEsbuild, ...tsJestConfig } = transformerConfig ?? {};
5256
super(tsJestConfig);
5357
this.#ngJestLogger = createLogger({
5458
context: {
@@ -58,6 +62,7 @@ export class NgJestTransformer extends TsJestTransformer {
5862
},
5963
targets: process.env.NG_JEST_LOG ?? undefined,
6064
});
65+
this.#processWithEsbuild = globsToMatcher(processWithEsbuild ?? []);
6166
}
6267

6368
protected _createConfigSet(config: TsJestTransformOptions['config'] | undefined): ConfigSet {
@@ -75,7 +80,7 @@ export class NgJestTransformer extends TsJestTransformer {
7580
process(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): TransformedSource {
7681
// @ts-expect-error we are accessing the private cache to avoid creating new objects all the time
7782
const configSet = super._configsFor(transformOptions);
78-
if (configSet.processWithEsbuild(filePath)) {
83+
if (configSet.processWithEsbuild(filePath) || this.#processWithEsbuild(filePath)) {
7984
this.#ngJestLogger.debug({ filePath }, 'process with esbuild');
8085

8186
const compilerOpts = configSet.parsedTsConfig.options;

0 commit comments

Comments
 (0)