Skip to content

Commit 9213bc1

Browse files
authored
feat: add ability to specify which typescript to use (#49)
* feat: add ability to specify which typescript to use * refactor: fix coverage
1 parent 683d943 commit 9213bc1

20 files changed

+293
-174
lines changed

src/__tests__/remap-snapshot.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import * as ts from 'typescript';
34
import { remap_snapshot } from '../remap-snapshot';
45

56
const load_content = (relative_path: string) =>
@@ -11,7 +12,9 @@ const load_content = (relative_path: string) =>
1112
it('should remap correctly with snapshot-content string', () => {
1213
const source_content = load_content('example.ts');
1314
const snapshot_content = load_content('__snapshots__/example.ts.snap');
14-
expect(remap_snapshot(snapshot_content, source_content)).toMatchSnapshot();
15+
expect(
16+
remap_snapshot(snapshot_content, source_content, undefined, ts),
17+
).toMatchSnapshot();
1518
});
1619

1720
it('should remap correctl with snapshot-content object', () => {
@@ -22,5 +25,7 @@ it('should remap correctl with snapshot-content object', () => {
2225
const snapshot_content = {
2326
'Math.max(1, 2, 3) 1': '"number"',
2427
};
25-
expect(remap_snapshot(snapshot_content, source_content)).toMatchSnapshot();
28+
expect(
29+
remap_snapshot(snapshot_content, source_content, undefined, ts),
30+
).toMatchSnapshot();
2631
});

src/__tests__/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ it('should setup correctly', () => {
1515

1616
it('should setup correctly with specified tsconfig', () => {
1717
const raw_config: RawConfig = {
18-
tsconfig: '<cwd>/fixtures/snapshots/tsconfig.json',
18+
tsconfig: '<rootDir>/fixtures/snapshots/tsconfig.json',
1919
};
2020
expect(setup(filename, raw_config, targets)).toMatchSnapshot();
2121
});

src/__tests__/transform-actual.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const transform_fixture = (
1212
const source_filename = path.resolve(__dirname, relative_path);
1313
const source_text = fs.readFileSync(source_filename, 'utf8');
1414
const jest_config: JestConfig = {
15+
rootDir: process.cwd(),
1516
globals: { _dts_jest_: { tsconfig } },
16-
_dts_jest_debug_: debug,
17+
_dts_jest_internal_test_: debug,
1718
};
18-
return transform_actual(source_text, source_filename, jest_config);
19+
return transform_actual(source_text, source_filename, jest_config as any);
1920
};
2021

2122
it('should transform correctly', () => {

src/__tests__/transform.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import { JestConfig } from '../definitions';
34
import { transform } from '../transform';
45

56
const transform_fixture = (relative_path: string) => {
67
const source_filename = path.resolve(__dirname, relative_path);
78
const source_text = fs.readFileSync(source_filename, 'utf8');
8-
return transform(source_text, source_filename);
9+
const jest_config: JestConfig = {
10+
rootDir: process.cwd(),
11+
globals: {},
12+
};
13+
return transform(source_text, source_filename, jest_config as any);
914
};
1015

1116
it('should transform correctly', () => {

src/definitions.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import * as ts from 'typescript';
1+
import * as _ts from 'typescript';
22

33
// tslint:disable-next-line:no-var-requires
44
export const package_name = require('../package.json').name;
55

66
export const config_namespace = '_dts_jest_';
77
export const runtime_namespace = '_dts_jest_runtime_';
88

9+
export const env_root_dir = 'DTS_JEST_ROOT_DIR';
10+
911
export const trigger_regex = /^\s*\/\/\s*@dts-jest\b(:?\S*)\s*(.+)?\s*$/;
1012
export enum TriggerMatchIndex {
1113
Input,
@@ -78,11 +80,20 @@ export interface Expected extends Trigger {
7880
export interface Result extends Target, Snapshot {}
7981

8082
export interface JestConfig {
83+
rootDir: string;
8184
globals: { [K in typeof config_namespace]?: RawConfig };
82-
_dts_jest_debug_?: boolean;
85+
_dts_jest_internal_test_?: boolean;
8386
}
8487

8588
export interface RawConfig {
86-
tsconfig?: string | ts.CompilerOptions;
87-
type_format?: ts.TypeFormatFlags;
89+
tsconfig?: string | _ts.CompilerOptions;
90+
type_format?: _ts.TypeFormatFlags;
91+
typescript?: string;
92+
}
93+
94+
export interface FormattedConfig {
95+
tsconfig: _ts.CompilerOptions;
96+
type_format: _ts.TypeFormatFlags;
97+
typescript: typeof _ts;
98+
typescript_path: string;
8899
}

src/remap-snapshot-cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import * as ts from 'typescript';
34
import { remap_snapshot } from './remap-snapshot';
45

56
export = (args: string[]) => {
@@ -17,5 +18,7 @@ export = (args: string[]) => {
1718
),
1819
'utf8',
1920
);
20-
process.stdout.write(remap_snapshot(snapshot_content, source_content));
21+
process.stdout.write(
22+
remap_snapshot(snapshot_content, source_content, undefined, ts),
23+
);
2124
};

src/remap-snapshot.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import require_from_string = require('require-from-string');
2-
import * as ts from 'typescript';
2+
import * as _ts from 'typescript';
33
import { Trigger } from './definitions';
44
import { create_triggers } from './utils/create-triggers';
55
import { default_to } from './utils/default-to';
@@ -8,7 +8,8 @@ import { get_formatted_description } from './utils/get-formatted-description';
88
export const remap_snapshot = (
99
snapshot_content: string | Record<string, string>,
1010
source_content: string,
11-
snapshot_filename?: string,
11+
snapshot_filename: string | undefined,
12+
ts: typeof _ts,
1213
) => {
1314
const snapshot_data =
1415
typeof snapshot_content === 'string'
@@ -24,7 +25,7 @@ export const remap_snapshot = (
2425
ts.ScriptTarget.Latest,
2526
false,
2627
);
27-
const triggers = create_triggers(source_file);
28+
const triggers = create_triggers(source_file, ts);
2829

2930
const source_content_lines = source_content.split('\n');
3031

src/setup.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import * as ts from 'typescript';
21
import { RawConfig, Result, Target } from './definitions';
32
import { Runtime } from './runtime';
43
import { create_snapshots } from './utils/create-snapshots';
54
import { default_to } from './utils/default-to';
6-
import { get_tsconfig } from './utils/get-tsconfig';
5+
import { get_config } from './utils/get-config';
6+
import { get_root_dir } from './utils/root-dir';
77

88
export const setup = (
99
filename: string,
1010
raw_config: RawConfig,
1111
targets: Target[],
1212
) => {
13+
const { typescript: ts, tsconfig } = get_config(raw_config, get_root_dir());
14+
1315
const lines = targets.map(target => target.line);
1416

1517
const flag = default_to(
1618
raw_config.type_format,
1719
ts.TypeFormatFlags.NoTruncation,
1820
);
19-
const program = ts.createProgram([filename], get_tsconfig(raw_config));
21+
const program = ts.createProgram([filename], tsconfig);
2022

21-
const snapshots = create_snapshots(program, filename, lines, flag);
23+
const snapshots = create_snapshots(program, filename, lines, flag, ts);
2224
const results = targets.reduce<{ [line: number]: Result }>(
2325
(current_results, target) => ({
2426
...current_results,

src/transform-actual.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
import * as ts from 'typescript';
2-
import { AssertionFlag, JestConfig, RawConfig } from './definitions';
1+
import { config_namespace, AssertionFlag, JestConfig } from './definitions';
32
import { create_expecteds } from './utils/create-expecteds';
43
import { create_triggers } from './utils/create-triggers';
5-
import { default_to } from './utils/default-to';
4+
import { get_config } from './utils/get-config';
65
import { get_formatted_description } from './utils/get-formatted-description';
7-
import { get_tsconfig } from './utils/get-tsconfig';
86
import { rewrite_expecteds_method } from './utils/rewrite-expecteds-method';
97

10-
export const transform_actual = (
11-
source_text: string,
12-
source_filename: string,
8+
export const transform_actual: jest.Transformer['process'] = (
9+
source_text,
10+
source_filename,
1311
jest_config: JestConfig,
1412
) => {
13+
const { typescript: ts, tsconfig } = get_config(
14+
jest_config.globals[config_namespace],
15+
jest_config.rootDir,
16+
);
17+
1518
const source_file = ts.createSourceFile(
1619
source_filename,
1720
source_text,
1821
ts.ScriptTarget.Latest,
1922
false,
2023
);
21-
const triggers = create_triggers(source_file);
24+
const triggers = create_triggers(source_file, ts);
2225

23-
const expecteds = create_expecteds(triggers, source_file);
26+
const expecteds = create_expecteds(triggers, source_file, ts);
2427
rewrite_expecteds_method(expecteds);
2528

2629
let transformed = source_text;
@@ -44,13 +47,16 @@ export const transform_actual = (
4447
)}`;
4548
});
4649

47-
if (jest_config._dts_jest_debug_ === true) {
50+
if (jest_config._dts_jest_internal_test_ === true) {
4851
return transformed;
4952
}
5053

51-
const tsconfig: ts.CompilerOptions = {
52-
...get_tsconfig(default_to<RawConfig>(jest_config.globals._dts_jest_, {})),
53-
inlineSourceMap: true,
54-
};
55-
return ts.transpile(transformed, tsconfig, source_filename);
54+
return ts.transpile(
55+
transformed,
56+
{
57+
...tsconfig,
58+
inlineSourceMap: true,
59+
},
60+
source_filename,
61+
);
5662
};

src/transform.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import * as ts from 'typescript';
2-
import { Group } from './definitions';
1+
import { config_namespace, Group, JestConfig } from './definitions';
32
import { create_group_expression } from './utils/create-group-expression';
43
import { create_setup_expression } from './utils/create-setup-expression';
54
import { create_test_expression } from './utils/create-test-expression';
65
import { create_triggers } from './utils/create-triggers';
6+
import { get_config } from './utils/get-config';
7+
import { set_root_dir } from './utils/root-dir';
78

8-
export const transform = (
9-
source_text: string,
10-
source_filename: string,
11-
_jest_config?: any,
9+
export const transform: jest.Transformer['process'] = (
10+
source_text,
11+
source_filename,
12+
jest_config: JestConfig,
1213
) => {
14+
const { typescript: ts } = get_config(
15+
jest_config.globals[config_namespace],
16+
jest_config.rootDir,
17+
);
18+
19+
set_root_dir(jest_config.rootDir);
20+
1321
const source_file = ts.createSourceFile(
1422
source_filename,
1523
source_text,
@@ -18,7 +26,7 @@ export const transform = (
1826
);
1927

2028
let last_group: Group | undefined;
21-
const triggers = create_triggers(source_file);
29+
const triggers = create_triggers(source_file, ts);
2230
return triggers
2331
.reduce<string[]>((transformed_line_texts, trigger, index) => {
2432
const is_diff_group = trigger.group !== last_group;

0 commit comments

Comments
 (0)