Skip to content

Commit

Permalink
chore(jest-resolve): improve types (#10239)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jul 5, 2020
1 parent e8b7f57 commit 49de731
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- `[jest-jasmine2]` Convert `PCancelable` to TypeScript ([#10215](https://github.com/facebook/jest/pull/10215))
- `[jest-jasmine2]` Refine typings of `queueRunner` ([#10215](https://github.com/facebook/jest/pull/10215))
- `[jest-resolve]` Improve types ([#10239](https://github.com/facebook/jest/pull/10239))

### Performance

Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:553:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:552:17)
at Object.require (index.js:10:1)
`;

Expand Down Expand Up @@ -70,6 +70,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:553:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:552:17)
at Object.require (index.js:10:1)
`;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ FAIL __tests__/test.js
| ^
9 |
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:308:11)
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:307:11)
at Object.require (index.js:8:18)
`;
6 changes: 3 additions & 3 deletions packages/jest-resolve/src/ModuleNotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {Config} from '@jest/types';
import slash = require('slash');

export default class ModuleNotFoundError extends Error {
code = 'MODULE_NOT_FOUND';
public code = 'MODULE_NOT_FOUND';
public hint?: string;
public requireStack?: Array<Config.Path>;
public siblingWithSimilarExtensionFound?: boolean;
Expand All @@ -31,11 +31,11 @@ export default class ModuleNotFoundError extends Error {

let message = this._originalMessage;

if (this?.requireStack?.length && this!.requireStack!.length > 1) {
if (this.requireStack?.length && this.requireStack.length > 1) {
message += `
Require stack:
${(this.requireStack as Array<string>)
${this.requireStack
.map(p => p.replace(`${rootDir}${path.sep}`, ''))
.map(slash)
.join('\n ')}
Expand Down
16 changes: 16 additions & 0 deletions packages/jest-resolve/src/__mocks__/userResolver.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import defaultResolver from '../defaultResolver';

// todo: can be replaced with jest.MockedFunction
declare const userResolver: jest.MockInstance<
ReturnType<typeof defaultResolver>,
Parameters<typeof defaultResolver>
>;

export default userResolver;
3 changes: 1 addition & 2 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import * as path from 'path';
import * as fs from 'graceful-fs';
import {ModuleMap} from 'jest-haste-map';
import Resolver = require('../');
// @ts-expect-error: js file
import userResolver from '../__mocks__/userResolver';
import nodeModulesPaths from '../nodeModulesPaths';
import defaultResolver from '../defaultResolver';
import {ResolverConfig} from '../types';
import type {ResolverConfig} from '../types';

jest.mock('../__mocks__/userResolver');

Expand Down
10 changes: 9 additions & 1 deletion packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ type ResolverOptions = {
rootDir?: Config.Path;
};

declare global {
namespace NodeJS {
export interface ProcessVersions {
// the "pnp" version named isn't in DefinitelyTyped
pnp?: unknown;
}
}
}

export default function defaultResolver(
path: Config.Path,
options: ResolverOptions,
): Config.Path {
// @ts-expect-error: the "pnp" version named isn't in DefinitelyTyped
if (process.versions.pnp) {
return pnpResolver(path, options);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ class Resolver {
});

if (!skipResolution) {
// @ts-expect-error: the "pnp" version named isn't in DefinitelyTyped
module = resolveNodeModule(moduleName, Boolean(process.versions.pnp));

if (module) {
Expand Down Expand Up @@ -471,7 +470,7 @@ const createNoMappedModuleFoundError = (
mapModuleName: (moduleName: string) => string,
mappedModuleName: string | Array<string>,
regex: RegExp,
resolver?: Function | string | null,
resolver?: ((...args: Array<unknown>) => unknown) | string | null,
) => {
const mappedAs = Array.isArray(mappedModuleName)
? JSON.stringify(mappedModuleName.map(mapModuleName), null, 2)
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-resolve/src/isBuiltinModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import module = require('module');

// "private" api
declare const process: {
binding(type: string): {};
};
declare const process: NodeJS.Process & {binding(type: string): {}};

const EXPERIMENTAL_MODULES = ['worker_threads'];

Expand Down

0 comments on commit 49de731

Please sign in to comment.