Skip to content

Commit

Permalink
chore: type all exported functions (#9548)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 10, 2020
1 parent 327b26b commit dd64176
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const jestAdapter = async (
} = runtime.requireInternalModule(FRAMEWORK_INITIALIZER);

runtime
.requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
.requireInternalModule<typeof import('./jestExpect')>(
path.resolve(__dirname, './jestExpect.js'),
)
.default({
expand: globalConfig.expand,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class JSDOMEnvironment implements JestEnvironment {
this.fakeTimersLolex = new LolexFakeTimers({config, global});
}

async setup() {}
async setup(): Promise<void> {}

async teardown() {
async teardown(): Promise<void> {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
Expand All @@ -124,7 +124,7 @@ class JSDOMEnvironment implements JestEnvironment {
this.fakeTimersLolex = null;
}

runScript(script: Script) {
runScript<T = unknown>(script: Script): T | null {
if (this.dom) {
return this.dom.runVMScript(script) as any;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class NodeEnvironment implements JestEnvironment {
this.fakeTimersLolex = new LolexFakeTimers({config, global});
}

async setup() {}
async setup(): Promise<void> {}

async teardown() {
async teardown(): Promise<void> {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
Expand All @@ -109,14 +109,14 @@ class NodeEnvironment implements JestEnvironment {

// TS infers the return type to be `any`, since that's what `runInContext`
// returns.
runScript(script: Script) {
runScript<T = unknown>(script: Script): T | null {
if (this.context) {
return script.runInContext(this.context);
}
return null;
}

getVmContext() {
getVmContext(): Context | null {
return this.context;
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-haste-map/src/lib/FSEventsWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class FSEventsWatcher extends EventEmitter {
public readonly fsEventsWatchStopper: () => Promise<void>;
private _tracked: Set<string>;

static isSupported() {
static isSupported(): boolean {
return fsevents !== null;
}

private static normalizeProxy(
callback: (normalizedPath: string, stats: fs.Stats) => void,
) {
return (filepath: string, stats: fs.Stats) =>
return (filepath: string, stats: fs.Stats): void =>
callback(path.normalize(filepath), stats);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ class FSEventsWatcher extends EventEmitter {
/**
* End watching.
*/
close(callback?: () => void) {
close(callback?: () => void): void {
this.fsEventsWatchStopper().then(() => {
this.removeAllListeners();
if (typeof callback === 'function') {
Expand Down
14 changes: 10 additions & 4 deletions packages/jest-jasmine2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ async function jasmine2(
testPath: string,
): Promise<TestResult> {
const reporter = new JasmineReporter(globalConfig, config, testPath);
const jasmineFactory = runtime.requireInternalModule(JASMINE);
const jasmineFactory = runtime.requireInternalModule<
typeof import('./jasmine/jasmineLight')
>(JASMINE);
const jasmine = jasmineFactory.create({
process,
testPath,
Expand Down Expand Up @@ -120,7 +122,9 @@ async function jasmine2(
env.addReporter(reporter);

runtime
.requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
.requireInternalModule<typeof import('./jestExpect')>(
path.resolve(__dirname, './jestExpect.js'),
)
.default({
expand: globalConfig.expand,
});
Expand All @@ -141,7 +145,9 @@ async function jasmine2(
}

const snapshotState: SnapshotStateType = runtime
.requireInternalModule(path.resolve(__dirname, './setup_jest_globals.js'))
.requireInternalModule<typeof import('./setup_jest_globals')>(
path.resolve(__dirname, './setup_jest_globals.js'),
)
.default({
config,
globalConfig,
Expand All @@ -158,7 +164,7 @@ async function jasmine2(
const suiteMap =
globalConfig.enabledTestsMap &&
globalConfig.enabledTestsMap[spec.result.testPath];
return suiteMap && suiteMap[spec.result.fullName];
return (suiteMap && suiteMap[spec.result.fullName]) || false;
};
} else if (globalConfig.testNamePattern) {
const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function(j$: Jasmine) {
randomTests: () => boolean;
seed: (value: unknown) => unknown;
execute: (
runnablesToRun: Array<string>,
runnablesToRun?: Array<string>,
suiteTree?: Suite,
) => Promise<void>;
fdescribe: (description: string, specDefinitions: Function) => Suite;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {version: VERSION} = require('../../package.json');

const REPL_SCRIPT = require.resolve('./repl.js');

export = function() {
export = function(): void {
const argv = <Config.Argv>yargs.usage(args.usage).options(args.options).argv;

// @ts-ignore: fix this at some point
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Resolver {

static ModuleNotFoundError = ModuleNotFoundError;

static clearDefaultResolverCache() {
static clearDefaultResolverCache(): void {
clearDefaultResolverCache();
}

Expand Down Expand Up @@ -230,7 +230,7 @@ class Resolver {
);
}

getModulePath(from: Config.Path, moduleName: string) {
getModulePath(from: Config.Path, moduleName: string): Config.Path {
if (moduleName[0] !== '.' || path.isAbsolute(moduleName)) {
return moduleName;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async function runTestInternal(

// For tests
runtime
.requireInternalModule(
.requireInternalModule<typeof import('source-map-support')>(
require.resolve('source-map-support'),
'source-map-support',
)
Expand Down
59 changes: 30 additions & 29 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,20 @@ class Runtime {
});
}

static runCLI(args?: Config.Argv, info?: Array<string>) {
static runCLI(args?: Config.Argv, info?: Array<string>): Promise<void> {
return cliRun(args, info);
}

static getCLIOptions(): typeof cliOptions {
return cliOptions;
}

requireModule(
requireModule<T = unknown>(
from: Config.Path,
moduleName?: string,
options?: InternalModuleOptions,
isRequireActual?: boolean | null,
) {
): T {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
Expand Down Expand Up @@ -370,15 +370,15 @@ class Runtime {
return localModule.exports;
}

requireInternalModule(from: Config.Path, to?: string) {
requireInternalModule<T = unknown>(from: Config.Path, to?: string): T {
return this.requireModule(from, to, {isInternalModule: true});
}

requireActual(from: Config.Path, moduleName: string) {
requireActual<T = unknown>(from: Config.Path, moduleName: string): T {
return this.requireModule(from, moduleName, undefined, true);
}

requireMock(from: Config.Path, moduleName: string) {
requireMock<T = unknown>(from: Config.Path, moduleName: string): T {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
Expand All @@ -399,7 +399,7 @@ class Runtime {
if (moduleID in this._mockFactories) {
const module = this._mockFactories[moduleID]();
mockRegistry.set(moduleID, module);
return module;
return module as T;
}

const manualMockOrStub = this._resolver.getMockModule(from, moduleName);
Expand Down Expand Up @@ -508,7 +508,7 @@ class Runtime {
};
}

requireModuleOrMock(from: Config.Path, moduleName: string) {
requireModuleOrMock(from: Config.Path, moduleName: string): unknown {
try {
if (this._shouldMock(from, moduleName)) {
return this.requireMock(from, moduleName);
Expand All @@ -531,7 +531,7 @@ class Runtime {
}
}

isolateModules(fn: () => void) {
isolateModules(fn: () => void): void {
if (this._isolatedModuleRegistry || this._isolatedMockRegistry) {
throw new Error(
'isolateModules cannot be nested inside another isolateModules.',
Expand All @@ -547,7 +547,7 @@ class Runtime {
}
}

resetModules() {
resetModules(): void {
this._isolatedModuleRegistry = null;
this._isolatedMockRegistry = null;
this._mockRegistry.clear();
Expand All @@ -574,13 +574,13 @@ class Runtime {
}
}

async collectV8Coverage() {
async collectV8Coverage(): Promise<void> {
this._v8CoverageInstrumenter = new CoverageInstrumenter();

await this._v8CoverageInstrumenter.startInstrumenting();
}

async stopCollectingV8Coverage() {
async stopCollectingV8Coverage(): Promise<void> {
if (!this._v8CoverageInstrumenter) {
throw new Error('You need to call `collectV8Coverage` first.');
}
Expand Down Expand Up @@ -616,19 +616,20 @@ class Runtime {
});
}

getSourceMapInfo(coveredFiles: Set<string>) {
return Object.keys(this._sourceMapRegistry).reduce<{
[path: string]: string;
}>((result, sourcePath) => {
if (
coveredFiles.has(sourcePath) &&
this._needsCoverageMapped.has(sourcePath) &&
fs.existsSync(this._sourceMapRegistry[sourcePath])
) {
result[sourcePath] = this._sourceMapRegistry[sourcePath];
}
return result;
}, {});
getSourceMapInfo(coveredFiles: Set<string>): Record<string, string> {
return Object.keys(this._sourceMapRegistry).reduce<Record<string, string>>(
(result, sourcePath) => {
if (
coveredFiles.has(sourcePath) &&
this._needsCoverageMapped.has(sourcePath) &&
fs.existsSync(this._sourceMapRegistry[sourcePath])
) {
result[sourcePath] = this._sourceMapRegistry[sourcePath];
}
return result;
},
{},
);
}

getSourceMaps(): SourceMapRegistry {
Expand All @@ -640,7 +641,7 @@ class Runtime {
moduleName: string,
mockFactory: () => unknown,
options?: {virtual?: boolean},
) {
): void {
if (options && options.virtual) {
const mockPath = this._resolver.getModulePath(from, moduleName);
this._virtualMocks[mockPath] = true;
Expand All @@ -654,15 +655,15 @@ class Runtime {
this._mockFactories[moduleID] = mockFactory;
}

restoreAllMocks() {
restoreAllMocks(): void {
this._moduleMocker.restoreAllMocks();
}

resetAllMocks() {
resetAllMocks(): void {
this._moduleMocker.resetAllMocks();
}

clearAllMocks() {
clearAllMocks(): void {
this._moduleMocker.clearAllMocks();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ function createIndent(indent: number): string {
* @param options Custom settings
*/
function prettyFormat(
val: any,
val: unknown,
options?: PrettyFormat.OptionsReceived,
): string {
if (options) {
Expand Down

0 comments on commit dd64176

Please sign in to comment.