Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jest-resolve): improve types #10239

Merged
merged 1 commit into from Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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)
`;
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.requireStack?.length && this.requireStack.length > 1) {
if (this.requireStack?.length > 1) {

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> requires both sides to be a number, so you can't do undefined > 1 in TS - I thought about merging these but decided against it as I think the alternatives don't really add much while not being as clean, i.e (this.requireStack?.length ?? 0) > 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
@@ -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
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
Expand Up @@ -21,11 +21,19 @@ type ResolverOptions = {
rootDir?: Config.Path;
};

declare global {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice solution

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
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
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