Skip to content

Commit 4f10f7e

Browse files
committed
feat(typings): emit declaration files, filtering out internals
Closes #745
1 parent d9c5b45 commit 4f10f7e

25 files changed

+233
-56
lines changed

src/cli/config/init.ts

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { Arguments } from 'yargs'
1212
import { CliCommand } from '..'
1313
import { createJestPreset } from '../../config/create-jest-preset'
1414

15+
/**
16+
* @internal
17+
*/
1518
export const run: CliCommand = async (args: Arguments /* , logger: Logger */) => {
1619
const file = args._[0] || 'jest.config.js'
1720
const filePath = join(process.cwd(), file)
@@ -94,6 +97,9 @@ Jest configuration written to "${filePath}".
9497
`)
9598
}
9699

100+
/**
101+
* @internal
102+
*/
97103
export const help: CliCommand = async () => {
98104
process.stdout.write(`
99105
Usage:

src/cli/config/migrate.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { CliCommand } from '..'
99
import { createJestPreset } from '../../config/create-jest-preset'
1010
import { backportJestConfig } from '../../util/backports'
1111

12+
/**
13+
* @internal
14+
*/
1215
export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
1316
const nullLogger = createLogger({ targets: [] })
1417
const file = args._[0]
@@ -153,6 +156,9 @@ function dedupSort(arr: any[]) {
153156
.sort((a, b) => (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0))
154157
}
155158

159+
/**
160+
* @internal
161+
*/
156162
export const help: CliCommand = async () => {
157163
process.stdout.write(`
158164
Usage:

src/cli/help.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Arguments } from 'yargs'
22

3+
/**
4+
* @internal
5+
*/
36
export const run = async (_: Arguments) => {
47
process.stdout.write(`
58
Usage:
@@ -15,4 +18,7 @@ Example:
1518
`)
1619
}
1720

18-
export { run as help }
21+
/**
22+
* @internal
23+
*/
24+
export const help = run

src/cli/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const VALID_COMMANDS = ['help', 'config:migrate', 'config:init']
88

99
const logger = rootLogger.child({ [LogContexts.namespace]: 'cli', [LogContexts.application]: 'ts-jest' })
1010

11+
/**
12+
* @internal
13+
*/
1114
export type CliCommand = (argv: Arguments, logger: Logger) => Promise<void>
1215

1316
async function cli(args: string[]): Promise<void> {
@@ -33,6 +36,9 @@ async function cli(args: string[]): Promise<void> {
3336
return cmd(parsedArgv, logger)
3437
}
3538

39+
/**
40+
* @internal
41+
*/
3642
export async function processArgv(): Promise<void> {
3743
try {
3844
await cli(process.argv.slice(2))

src/compiler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const hasOwn = Object.prototype.hasOwnProperty
4545

4646
/**
4747
* Register TypeScript compiler.
48+
* @internal
4849
*/
4950
export function createCompiler(configs: ConfigSet): TsCompiler {
5051
const logger = configs.logger.child({ namespace: 'ts-compiler' })

src/config/config-set.ts

+71-35
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@ interface ReadTsConfigResult {
5757
resolved: ParsedCommandLine
5858
}
5959

60+
/**
61+
* @internal
62+
*/
6063
// this regex MUST match nothing, it's the goal ;-)
6164
export const MATCH_NOTHING = /a^/
65+
/**
66+
* @internal
67+
*/
6268
export const IGNORE_DIAGNOSTIC_CODES = [
6369
6059, // "'rootDir' is expected to contain all source files."
6470
18002, // "The 'files' list in config file is empty."
6571
18003, // "No inputs were found in config file."
6672
]
6773

74+
/**
75+
* @internal
76+
*/
6877
// WARNING: DO NOT CHANGE THE ORDER OF CODE NAMES!
6978
// ONLY APPEND IF YOU NEED TO ADD SOME
7079
export enum DiagnosticCodes {
@@ -103,16 +112,6 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
103112
}
104113

105114
export class ConfigSet {
106-
readonly logger: Logger
107-
108-
constructor(
109-
private readonly _jestConfig: jest.ProjectConfig,
110-
readonly parentOptions?: TsJestGlobalOptions,
111-
parentLogger?: Logger,
112-
) {
113-
this.logger = parentLogger ? parentLogger.child({ [LogContexts.namespace]: 'config' }) : logger
114-
}
115-
116115
@Memoize()
117116
get jest(): jest.ProjectConfig {
118117
const config = backportJestConfig(this.logger, this._jestConfig)
@@ -234,6 +233,9 @@ export class ConfigSet {
234233
)
235234
}
236235

236+
/**
237+
* @internal
238+
*/
237239
@Memoize()
238240
private get _typescript(): ReadTsConfigResult {
239241
const {
@@ -252,6 +254,9 @@ export class ConfigSet {
252254
return result
253255
}
254256

257+
/**
258+
* @internal
259+
*/
255260
@Memoize()
256261
get raiseDiagnostics() {
257262
const {
@@ -349,6 +354,9 @@ export class ConfigSet {
349354
return {}
350355
}
351356

357+
/**
358+
* @internal
359+
*/
352360
@Memoize()
353361
get filterDiagnostics() {
354362
const {
@@ -392,6 +400,9 @@ export class ConfigSet {
392400
}
393401
}
394402

403+
/**
404+
* @internal
405+
*/
395406
@Memoize()
396407
get createTsError() {
397408
const {
@@ -471,10 +482,54 @@ export class ConfigSet {
471482
return normalize(this.jest.cwd || process.cwd())
472483
}
473484

485+
/**
486+
* @internal
487+
*/
474488
get isDoctoring() {
475489
return !!process.env.TS_JEST_DOCTOR
476490
}
477491

492+
/**
493+
* @internal
494+
*/
495+
@Memoize()
496+
get jsonValue() {
497+
const jest = { ...this.jest }
498+
const globals = (jest.globals = { ...jest.globals } as any)
499+
// we need to remove some stuff from jest config
500+
// this which does not depend on config
501+
delete jest.name
502+
delete jest.cacheDirectory
503+
// we do not need this since its normalized version is in tsJest
504+
delete globals['ts-jest']
505+
506+
return new JsonableValue({
507+
versions: this.versions,
508+
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
509+
jest,
510+
tsJest: this.tsJest,
511+
babel: this.babel,
512+
tsconfig: this.tsconfig,
513+
})
514+
}
515+
516+
get cacheKey(): string {
517+
return this.jsonValue.serialized
518+
}
519+
readonly logger: Logger
520+
/**
521+
* @internal
522+
*/
523+
private readonly _jestConfig: jest.ProjectConfig
524+
525+
constructor(jestConfig: jest.ProjectConfig, readonly parentOptions?: TsJestGlobalOptions, parentLogger?: Logger) {
526+
this._jestConfig = jestConfig
527+
this.logger = parentLogger ? parentLogger.child({ [LogContexts.namespace]: 'config' }) : logger
528+
}
529+
530+
/**
531+
* @internal
532+
*/
478533
makeDiagnostic(
479534
code: number,
480535
messageText: string,
@@ -491,6 +546,9 @@ export class ConfigSet {
491546
}
492547
}
493548

549+
/**
550+
* @internal
551+
*/
494552
readTsConfig(
495553
compilerOptions?: object,
496554
resolvedConfigFile?: string | null,
@@ -608,31 +666,9 @@ export class ConfigSet {
608666
return path
609667
}
610668

611-
@Memoize()
612-
get jsonValue() {
613-
const jest = { ...this.jest }
614-
const globals = (jest.globals = { ...jest.globals } as any)
615-
// we need to remove some stuff from jest config
616-
// this which does not depend on config
617-
delete jest.name
618-
delete jest.cacheDirectory
619-
// we do not need this since its normalized version is in tsJest
620-
delete globals['ts-jest']
621-
622-
return new JsonableValue({
623-
versions: this.versions,
624-
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
625-
jest,
626-
tsJest: this.tsJest,
627-
babel: this.babel,
628-
tsconfig: this.tsconfig,
629-
})
630-
}
631-
632-
get cacheKey(): string {
633-
return this.jsonValue.serialized
634-
}
635-
669+
/**
670+
* @internal
671+
*/
636672
toJSON() {
637673
return this.jsonValue.value
638674
}

src/index.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,53 @@ import { TsJestGlobalOptions } from './types'
55
import { VersionCheckers } from './util/version-checkers'
66

77
// tslint:disable-next-line:no-var-requires
8-
const version: string = require('../package.json').version
8+
export const version: string = require('../package.json').version
99

1010
let transformer!: TsJestTransformer
1111
function defaultTransformer(): TsJestTransformer {
1212
return transformer || (transformer = createTransformer())
1313
}
1414

15-
function createTransformer(baseConfig?: TsJestGlobalOptions) {
15+
export function createTransformer(baseConfig?: TsJestGlobalOptions) {
1616
VersionCheckers.jest.warn()
1717
return new TsJestTransformer(baseConfig)
1818
}
19-
function tsProcess(...args: any[]): any {
19+
/**
20+
* @internal
21+
*/
22+
export function process(...args: any[]): any {
2023
return (defaultTransformer().process as any)(...args)
2124
}
22-
function getCacheKey(...args: any[]): any {
25+
/**
26+
* @internal
27+
*/
28+
export function getCacheKey(...args: any[]): any {
2329
return (defaultTransformer().getCacheKey as any)(...args)
2430
}
2531

32+
/**
33+
* @internal
34+
*/
2635
// we let jest doing the instrumentation, it does it well
27-
const canInstrument = false
36+
export const canInstrument = false
2837

2938
const jestPreset = createJestPreset()
3039

40+
/**
41+
* @internal
42+
*/
3143
// for tests
3244
// tslint:disable-next-line:variable-name
33-
const __singleton = () => transformer
45+
export const __singleton = () => transformer
46+
/**
47+
* @internal
48+
*/
3449
// tslint:disable-next-line:variable-name
35-
const __resetModule = () => (transformer = undefined as any)
50+
export const __resetModule = () => (transformer = undefined as any)
3651

3752
export {
38-
version,
39-
// jest API ===============
40-
createTransformer,
41-
tsProcess as process,
42-
getCacheKey,
43-
canInstrument,
4453
// extra ==================
4554
createJestPreset,
4655
jestPreset,
4756
pathsToModuleNameMapper,
48-
// tests ==================
49-
__singleton,
50-
__resetModule,
5157
}

src/transformers/hoist-jest.ts

+7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ import { ConfigSet } from '../config/config-set'
1919
*/
2020
const HOIST_METHODS = ['mock', 'unmock']
2121

22+
/**
23+
* @internal
24+
*/
2225
export const name = 'hoisting-jest-mock'
2326
// increment this each time the code is modified
27+
/**
28+
* @internal
29+
*/
2430
export const version = 1
2531

2632
/**
2733
* The factory of hoisting transformer factory
2834
* @param cs Current jest configuration-set
35+
* @internal
2936
*/
3037
export function factory(cs: ConfigSet) {
3138
const logger = cs.logger.child({ namespace: 'ts-hoisting' })

src/transformers/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ import { AstTransformerDesc } from '../types'
22

33
import * as hoisting from './hoist-jest'
44

5+
/**
6+
* @internal
7+
*/
58
export const internals: AstTransformerDesc[] = [hoisting]

src/ts-jest-transformer.ts

+12
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ interface ConfigSetIndexItem {
2020
}
2121

2222
export class TsJestTransformer implements jest.Transformer {
23+
/**
24+
* @internal
25+
*/
2326
private static readonly _configSetsIndex: ConfigSetIndexItem[] = []
27+
/**
28+
* @internal
29+
*/
2430
private static _lastTransformerId = 0
2531
static get lastTransformerId() {
2632
return TsJestTransformer._lastTransformerId
2733
}
34+
/**
35+
* @internal
36+
*/
2837
private static get _nextTransformerId() {
2938
return ++TsJestTransformer._lastTransformerId
3039
}
@@ -43,6 +52,9 @@ export class TsJestTransformer implements jest.Transformer {
4352
this.logger.debug({ baseOptions }, 'created new transformer')
4453
}
4554

55+
/**
56+
* @internal
57+
*/
4658
/* istanbul ignore next */
4759
[INSPECT_CUSTOM]() {
4860
return `[object TsJestTransformer<#${this.id}>]`

0 commit comments

Comments
 (0)