-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathng-jest-transformer.ts
75 lines (66 loc) · 2.79 KB
/
ng-jest-transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import path from 'path';
import type { TransformedSource } from '@jest/transform';
import type { Config } from '@jest/types';
import { LogContexts, LogLevels, Logger, createLogger } from 'bs-logger';
import { transformSync } from 'esbuild';
import { ConfigSet } from 'ts-jest/dist/config/config-set';
import { TsJestTransformer } from 'ts-jest/dist/ts-jest-transformer';
import type { ProjectConfigTsJest, TransformOptionsTsJest } from 'ts-jest/dist/types';
import { NgJestCompiler } from './compiler/ng-jest-compiler';
import { NgJestConfig } from './config/ng-jest-config';
export class NgJestTransformer extends TsJestTransformer {
#ngJestLogger: Logger;
constructor() {
super();
this.#ngJestLogger = createLogger({
context: {
[LogContexts.package]: 'ts-jest',
[LogContexts.logLevel]: LogLevels.trace,
// eslint-disable-next-line @typescript-eslint/no-var-requires
version: require('../package.json').version,
},
targets: process.env.NG_JEST_LOG ?? undefined,
});
}
protected _createConfigSet(config: ProjectConfigTsJest | undefined): ConfigSet {
return new NgJestConfig(config);
}
protected _createCompiler(configSet: ConfigSet, cacheFS: Map<string, string>): void {
this._compiler = new NgJestCompiler(configSet, cacheFS);
}
process(
fileContent: string,
filePath: Config.Path,
transformOptions: TransformOptionsTsJest,
): TransformedSource | string {
const configSet = this._createConfigSet(transformOptions.config);
/**
* TypeScript < 4.5 doesn't support compiling `.mjs` file by default when running `tsc` which throws error. Also we
* transform `js` files from `node_modules` assuming that `node_modules` contains compiled files to speed up compilation.
* IMPORTANT: we exclude `tslib` from compilation because it has issue with compilation. The original `tslib.js` or
* `tslib.es6.js` works well with Jest without extra compilation
*/
if (
path.extname(filePath) === '.mjs' ||
(/node_modules\/(.*.js$)/.test(filePath.replace(/\\/g, '/')) && !filePath.includes('tslib'))
) {
this.#ngJestLogger.debug({ filePath }, 'process with esbuild');
const compilerOpts = configSet.parsedTsConfig.options;
const { code, map } = transformSync(fileContent, {
loader: 'js',
format: transformOptions.supportsStaticESM && configSet.useESM ? 'esm' : 'cjs',
target: compilerOpts.target === configSet.compilerModule.ScriptTarget.ES2015 ? 'es2015' : 'es2016',
sourcemap: compilerOpts.sourceMap,
sourcefile: filePath,
sourcesContent: true,
sourceRoot: compilerOpts.sourceRoot,
});
return {
code,
map,
};
} else {
return super.process(fileContent, filePath, transformOptions);
}
}
}