Skip to content

Commit c7349e4

Browse files
committed
chore: comit-al before testing with ts-node API
1 parent 07c4583 commit c7349e4

7 files changed

Lines changed: 44 additions & 52 deletions

File tree

src/__helpers__/fakers.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TsJestGlobalOptions, BabelConfig } from '../types';
1+
import { TsJestGlobalOptions, BabelConfig, TsJestConfig } from '../types';
22
import { resolve } from 'path';
33
import { ImportReasons } from '../utils/messages';
44

@@ -52,10 +52,14 @@ describe('hello', () => {
5252
`;
5353
}
5454

55-
export function tsJestConfig<T extends TsJestGlobalOptions>(
56-
options?: TsJestGlobalOptions,
57-
): T {
58-
return { ...options } as any;
55+
export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
56+
return {
57+
babelJest: undefined,
58+
tsConfig: undefined,
59+
diagnostics: [],
60+
stringifyContentPathRegex: undefined,
61+
...options,
62+
};
5963
}
6064

6165
export function jestConfig<T extends jest.ProjectConfig>(
@@ -68,7 +72,7 @@ export function jestConfig<T extends jest.ProjectConfig>(
6872
...options,
6973
} as any;
7074
if (tsJestOptions) {
71-
res.globals['ts-jest'] = tsJestConfig(tsJestOptions);
75+
res.globals['ts-jest'] = tsJestOptions;
7276
}
7377
return res;
7478
}

src/ts-jest-transformer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import importer from './utils/importer';
1818
import { Errors, ImportReasons } from './utils/messages';
1919

2020
export default class TsJestTransformer implements jest.Transformer {
21+
constructor(protected _baseOptions: TsJestGlobalOptions = {}) {}
22+
2123
@Memoize()
2224
get hooks(): TsJestHooksMap {
2325
let hooksFile = process.env.__TS_JEST_HOOKS;
@@ -58,7 +60,7 @@ export default class TsJestTransformer implements jest.Transformer {
5860
babelJestConfigFor(jestConfig: jest.ProjectConfig): BabelConfig | undefined {
5961
const config = this.configFor(jestConfig);
6062
const rootDir = jestRootDir(jestConfig);
61-
if (config.babelJest === false) {
63+
if (!config.babelJest) {
6264
return;
6365
}
6466

@@ -81,9 +83,9 @@ export default class TsJestTransformer implements jest.Transformer {
8183
const parsedConfig = backportJestConfig(jestConfig);
8284
const { globals = {} } = parsedConfig as any;
8385
const options: TsJestGlobalOptions = { ...globals['ts-jest'] };
84-
let { stringifyContentPathRegex: stringifyRegEx } = options;
8586

8687
// stringifyContentPathRegex option
88+
let { stringifyContentPathRegex: stringifyRegEx } = options;
8789
if (typeof stringifyRegEx === 'string') {
8890
try {
8991
stringifyRegEx = RegExp(stringifyRegEx);
@@ -108,8 +110,8 @@ export default class TsJestTransformer implements jest.Transformer {
108110

109111
// parsed options
110112
return {
111-
inputOptions: options,
112-
babelJest: options.babelJest || false,
113+
tsConfig: options.tsConfig || undefined,
114+
babelJest: options.babelJest || undefined,
113115
diagnostics: normalizeDiagnosticTypes(options.diagnostics),
114116
stringifyContentPathRegex: stringifyRegEx as RegExp | undefined,
115117
};

src/ts-program.spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ const content = fakers.typescriptSource();
77

88
describe('hoisting', () => {
99
describe('without babel', () => {
10-
const prog = new TsProgram(resolve(__dirname, '..'), {
11-
babelJest: false,
12-
inputOptions: {},
13-
diagnostics: [],
14-
});
10+
const prog = new TsProgram(resolve(__dirname, '..'), fakers.tsJestConfig());
1511

1612
it('should hoist jest.mock() calls', () => {
1713
const result = prog.transpileModule(path, content, undefined, {
@@ -22,11 +18,10 @@ describe('hoisting', () => {
2218
});
2319

2420
describe('with babel', () => {
25-
const prog = new TsProgram(resolve(__dirname, '..'), {
26-
babelJest: true,
27-
inputOptions: {},
28-
diagnostics: [],
29-
});
21+
const prog = new TsProgram(
22+
resolve(__dirname, '..'),
23+
fakers.tsJestConfig({ babelJest: {} }),
24+
);
3025

3126
it('should not hoist jest.mock() calls', () => {
3227
const result = prog.transpileModule(path, content, undefined, {

src/ts-program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class TsProgram implements TsJestProgram {
3535

3636
@Memoize()
3737
get configFile(): string | null {
38-
const given = this.tsJestConfig.inputOptions.tsConfig;
38+
const given = this.tsJestConfig.tsConfig;
3939
let resolved: string | undefined;
4040
if (typeof given === 'string') {
4141
// we got a path to a custom (or not) tsconfig
@@ -83,7 +83,7 @@ export default class TsProgram implements TsJestProgram {
8383
const { config, error } = configFile
8484
? ts.readConfigFile(configFile, sys.readFile)
8585
: {
86-
config: { compilerOptions: this.tsJestConfig.inputOptions.tsConfig },
86+
config: { compilerOptions: this.tsJestConfig.tsConfig },
8787
error: undefined,
8888
};
8989
if (error) throw error; // tslint:disable-line:curly

src/types.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,21 @@ export enum DiagnosticTypes {
2323
global = 'global',
2424
semantic = 'sementic',
2525
}
26-
export enum DiagnosticSets {
27-
none = 'none',
28-
full = 'full',
29-
default = 'default',
30-
}
31-
export const diagnosticSets = {
32-
full: [
33-
DiagnosticTypes.global,
34-
DiagnosticTypes.syntactic,
35-
DiagnosticTypes.semantic,
36-
DiagnosticTypes.options,
37-
],
38-
default: [DiagnosticTypes.syntactic, DiagnosticTypes.options],
39-
none: [],
40-
};
26+
export type DiagnosticFilter = DiagnosticTypes[] | DiagnosticTypes;
27+
export type TsTransformerFactory =
28+
| _ts.TransformerFactory<_ts.SourceFile>
29+
| string;
4130

4231
// FIXME: find the right typing for this
4332
export type BabelConfig = _babel.TransformOptions;
4433

4534
export interface TsJestGlobalOptions {
4635
// either a path to a tsconfig json file, or inline compiler options
4736
tsConfig?: string | _ts.CompilerOptions;
37+
4838
// what kind of diagnostics to report
49-
diagnostics?: DiagnosticTypes[] | DiagnosticTypes | DiagnosticSets | boolean;
39+
diagnostics?: DiagnosticFilter | boolean | undefined;
40+
5041
// whether to use babel jest under the hood or not
5142
// it can be:
5243
// - a path to a babelrc (<rootDir> can be used)
@@ -60,12 +51,12 @@ export interface TsJestGlobalOptions {
6051
}
6152

6253
export interface TsJestConfig {
63-
inputOptions: TsJestGlobalOptions;
64-
babelJest: BabelConfig | string | false;
54+
tsConfig: _ts.CompilerOptions | string | undefined;
55+
babelJest: BabelConfig | string | undefined;
6556
diagnostics: DiagnosticTypes[];
6657

6758
// to deprecate / deprecated === === ===
68-
stringifyContentPathRegex?: RegExp | undefined;
59+
stringifyContentPathRegex: RegExp | undefined;
6960
}
7061

7162
export interface TsJestProgram {

src/utils/diagnostics.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { DiagnosticTypes, DiagnosticSets, diagnosticSets } from '../types';
1+
import { DiagnosticTypes, TsJestGlobalOptions, TsJestConfig } from '../types';
22
import { interpolate, Errors } from './messages';
3+
import { inspect } from 'util';
34

45
export const isDiagnosticType = (val: any): val is DiagnosticTypes => {
56
return val && DiagnosticTypes[val] === val;
67
};
78

8-
export const isDiagnosticSet = (val: any): val is DiagnosticSets => {
9-
return val && DiagnosticSets[val] === val;
10-
};
11-
129
export const normalizeDiagnosticTypes = (
13-
val?: DiagnosticTypes[] | DiagnosticTypes | DiagnosticSets | boolean,
10+
val?: DiagnosticTypes[] | DiagnosticTypes | boolean,
1411
): DiagnosticTypes[] => {
1512
let res!: DiagnosticTypes[];
1613
if (typeof val === 'string') {
1714
// string
1815
if (isDiagnosticType(val)) {
1916
res = [val];
20-
} else if (isDiagnosticSet(val)) {
21-
res = diagnosticSets[val];
2217
}
2318
} else if (Array.isArray(val)) {
2419
// array
@@ -30,11 +25,16 @@ export const normalizeDiagnosticTypes = (
3025
res = [];
3126
} else if (val) {
3227
// true
33-
res = diagnosticSets.default;
28+
res = [
29+
DiagnosticTypes.global,
30+
DiagnosticTypes.options,
31+
DiagnosticTypes.semantic,
32+
DiagnosticTypes.syntactic,
33+
];
3434
}
3535
if (!res) {
3636
throw new TypeError(
37-
interpolate(Errors.InvalidDiagnosticsOption, { value: val }),
37+
interpolate(Errors.InvalidDiagnosticsOption, { value: inspect(val) }),
3838
);
3939
}
4040
// ensure we have another instance of array with unique items

src/utils/importer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('patcher', () => {
5353
});
5454

5555
describe('babelCore', () => {
56-
it('should prefers babel 7', () => {
56+
it('should prefer babel 7', () => {
5757
mockThese(['@babel/core', 'babel-core']);
5858
expect(importer().babelCore(fakers.importReason())).toBe('@babel/core');
5959
});

0 commit comments

Comments
 (0)