Skip to content

Commit

Permalink
hmm
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Molinaro committed Aug 14, 2021
1 parent fdc74af commit 807c48c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@

### Performance

- `[jest-resolve]` Skip expensive error creation on not found stat calls. [#11749](https://github.com/facebook/jest/pull/11749)

## 27.0.7

### Chore & Maintenance
Expand Down
10 changes: 9 additions & 1 deletion packages/jest-resolve/src/__tests__/resolve.test.ts
Expand Up @@ -11,7 +11,7 @@ import * as fs from 'graceful-fs';
import {sync as resolveSync} from 'resolve';
import {ModuleMap} from 'jest-haste-map';
import userResolver from '../__mocks__/userResolver';
import defaultResolver from '../defaultResolver';
import defaultResolver, {ResolverOptions} from '../defaultResolver';
import nodeModulesPaths from '../nodeModulesPaths';
import Resolver from '../resolver';
import type {ResolverConfig} from '../types';
Expand Down Expand Up @@ -323,3 +323,11 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
});
});

describe('defaultResolver', () => {
it('does not change outwardly stackTraceLimit', () => {
const oldStackTraceValue = Error.stackTraceLimit;
expect(() => defaultResolver('test', {} as ResolverOptions)).toThrow();
expect(oldStackTraceValue).toBe(Error.stackTraceLimit);
});
});
11 changes: 10 additions & 1 deletion packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -11,7 +11,7 @@ import {sync as resolveSync} from 'resolve';
import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

type ResolverOptions = {
export type ResolverOptions = {
basedir: Config.Path;
browser?: boolean;
defaultResolver: typeof defaultResolver;
Expand Down Expand Up @@ -78,12 +78,21 @@ function statSyncCached(path: string): IPathType {
}

let stat;
const oldTrace = Object.getOwnPropertyDescriptors(Error).stackTraceLimit;
try {
if(oldTrace.writable) {
Object.defineProperty(Error, 'stackTraceLimit', {
value: 0,
writable: false,
});
}
stat = fs.statSync(path);
} catch (e) {
if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
throw e;
}
} finally {
Object.defineProperty(Error, 'stackTraceLimit', oldTrace);
}

if (stat) {
Expand Down

0 comments on commit 807c48c

Please sign in to comment.