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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create new @jest/source-maps package #8029

Merged
merged 1 commit into from Mar 3, 2019
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 @@ -78,6 +78,7 @@
- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991))
- `[docs]`: Update CONTRIBUTING.md to add information about running jest with `jest-circus` locally ([#8013](https://github.com/facebook/jest/pull/8013)).
- `[@jest/core]`: Migrate to TypeScript ([#7998](https://github.com/facebook/jest/pull/7998))
- `[@jest/source-map]`: Extract `getCallsite` function from `jest-util` into a new separate package ([#8029](https://github.com/facebook/jest/pull/8029))

### Performance

Expand Down
Expand Up @@ -15,6 +15,6 @@ PASS __tests__/console.test.js
15 | });
16 |

at BufferedConsole.log (../../packages/jest-util/build/BufferedConsole.js:172:10)
at BufferedConsole.log (../../packages/jest-util/build/BufferedConsole.js:180:10)
at log (__tests__/console.test.js:13:13)
`;
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Expand Up @@ -11,6 +11,7 @@
"types": "build/index.d.ts",
"dependencies": {
"@jest/environment": "^24.1.0",
"@jest/source-map": "^24.1.0",
"@jest/transform": "^24.1.0",
"@jest/types": "^24.1.0",
"@types/yargs": "^12.0.2",
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -6,13 +6,14 @@
*/

import path from 'path';
import {Config, SourceMaps} from '@jest/types';
import {Config} from '@jest/types';
import {
Jest,
JestEnvironment,
LocalModuleRequire,
Module,
} from '@jest/environment';
import {SourceMapRegistry} from '@jest/source-map';
import jestMock, {MockFunctionMetadata} from 'jest-mock';
import HasteMap, {ModuleMap} from 'jest-haste-map';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
Expand Down Expand Up @@ -102,7 +103,7 @@ class Runtime {
private _shouldAutoMock: boolean;
private _shouldMockModuleCache: BooleanObject;
private _shouldUnmockTransitiveDependenciesCache: BooleanObject;
private _sourceMapRegistry: SourceMaps.SourceMapRegistry;
private _sourceMapRegistry: SourceMapRegistry;
private _scriptTransformer: ScriptTransformer;
private _transitiveShouldMock: BooleanObject;
private _unmockList: RegExp | undefined;
Expand Down Expand Up @@ -523,7 +524,7 @@ class Runtime {
}, {});
}

getSourceMaps(): SourceMaps.SourceMapRegistry {
getSourceMaps(): SourceMapRegistry {
return this._sourceMapRegistry;
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/tsconfig.json
Expand Up @@ -14,6 +14,7 @@
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
{"path": "../jest-snapshot"},
{"path": "../jest-source-map"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-source-map/.npmignore
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
24 changes: 24 additions & 0 deletions packages/jest-source-map/package.json
@@ -0,0 +1,24 @@
{
"name": "@jest/source-map",
"version": "24.1.0",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-source-map"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"callsites": "^3.0.0",
"graceful-fs": "^4.1.15",
"source-map": "^0.6.0"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.2"
},
"engines": {
"node": ">= 6"
},
"gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60"
}
Expand Up @@ -8,7 +8,7 @@
import fs from 'graceful-fs';
import callsites, {CallSite} from 'callsites';
import {SourceMapConsumer} from 'source-map';
import {SourceMaps} from '@jest/types';
import {SourceMapRegistry} from './types';

// Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158
const addSourceMapConsumer = (
Expand Down Expand Up @@ -46,10 +46,7 @@ const addSourceMapConsumer = (
});
};

export default (
level: number,
sourceMaps?: SourceMaps.SourceMapRegistry | null,
) => {
export default (level: number, sourceMaps?: SourceMapRegistry | null) => {
const levelAfterThisCall = level + 1;
const stack = callsites()[levelAfterThisCall];
const sourceMapFileName = sourceMaps && sourceMaps[stack.getFileName() || ''];
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-source-map/src/index.ts
@@ -0,0 +1,9 @@
/**
* 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.
*/

export {default as getCallsite} from './getCallsite';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a thought, but maybe worth to only use named exports everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

export {SourceMapRegistry} from './types';
7 changes: 7 additions & 0 deletions packages/jest-source-map/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
}
3 changes: 1 addition & 2 deletions packages/jest-types/src/index.ts
Expand Up @@ -8,8 +8,7 @@
import * as Config from './Config';
import * as Console from './Console';
import * as Matchers from './Matchers';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
import * as Global from './Global';

export {Config, Console, Matchers, SourceMaps, TestResult, Global};
export {Config, Console, Matchers, TestResult, Global};
1 change: 1 addition & 0 deletions packages/jest-util/package.json
Expand Up @@ -11,6 +11,7 @@
"types": "build/index.d.ts",
"dependencies": {
"@jest/fake-timers": "^24.1.0",
"@jest/source-map": "^24.1.0",
"@jest/types": "^24.1.0",
"@types/node": "*",
"callsites": "^3.0.0",
Expand Down
12 changes: 5 additions & 7 deletions packages/jest-util/src/BufferedConsole.ts
Expand Up @@ -9,19 +9,17 @@ import assert from 'assert';
import {Console} from 'console';
import {format} from 'util';
import chalk from 'chalk';
import {Console as ConsoleType, SourceMaps} from '@jest/types';
import getCallsite from './getCallsite';
import {Console as ConsoleType} from '@jest/types';
import {getCallsite, SourceMapRegistry} from '@jest/source-map';

export default class BufferedConsole extends Console {
private _buffer: ConsoleType.ConsoleBuffer;
private _counters: ConsoleType.LogCounters;
private _timers: ConsoleType.LogTimers;
private _groupDepth: number;
private _getSourceMaps: () => SourceMaps.SourceMapRegistry | null | undefined;
private _getSourceMaps: () => SourceMapRegistry | null | undefined;

constructor(
getSourceMaps: () => SourceMaps.SourceMapRegistry | null | undefined,
) {
constructor(getSourceMaps: () => SourceMapRegistry | null | undefined) {
const buffer: ConsoleType.ConsoleBuffer = [];
super({
write: (message: string) => {
Expand All @@ -42,7 +40,7 @@ export default class BufferedConsole extends Console {
type: ConsoleType.LogType,
message: ConsoleType.LogMessage,
level?: number | null,
sourceMaps?: SourceMaps.SourceMapRegistry | null,
sourceMaps?: SourceMapRegistry | null,
) {
const callsite = getCallsite(level != null ? level : 2, sourceMaps);
const origin = callsite.getFileName() + ':' + callsite.getLineNumber();
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-util/src/index.ts
Expand Up @@ -7,6 +7,7 @@

// TODO: Remove this export in the next major
import {JestFakeTimers as FakeTimers} from '@jest/fake-timers';
import {getCallsite} from '@jest/source-map';
import BufferedConsole from './BufferedConsole';
import clearLine from './clearLine';
import CustomConsole from './CustomConsole';
Expand All @@ -18,7 +19,6 @@ import getConsoleOutput from './getConsoleOutput';
import installCommonGlobals from './installCommonGlobals';
import NullConsole from './NullConsole';
import isInteractive from './isInteractive';
import getCallsite from './getCallsite';
import setGlobal from './setGlobal';
import deepCyclicCopy from './deepCyclicCopy';
import convertDescriptorToString from './convertDescriptorToString';
Expand Down
1 change: 1 addition & 0 deletions packages/jest-util/tsconfig.json
Expand Up @@ -6,6 +6,7 @@
},
"references": [
{"path": "../jest-fake-timers"},
{"path": "../jest-source-map"},
{"path": "../jest-types"}
]
}
10 changes: 1 addition & 9 deletions yarn.lock
Expand Up @@ -1784,14 +1784,6 @@
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/readable-stream@^2.3.0":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@types/readable-stream/-/readable-stream-2.3.1.tgz#59d458b51c84c585caea06e296e2225057c9ea8e"
integrity sha512-Dp6t95yGEOm2y669mQrSl0kUg+oL+bJEiCWMyDv0Yq+FcVvjzNRLTAqJks2LDBYYrazZXNI7lZXq3lp7MOvt4A==
dependencies:
"@types/node" "*"
safe-buffer "*"

"@types/resolve@*":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
Expand Down Expand Up @@ -11516,7 +11508,7 @@ rxjs@^6.4.0:
dependencies:
tslib "^1.9.0"

safe-buffer@*, safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
Expand Down