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

[WIP] Remove usage of retainLines #5594

Merged
merged 19 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
"transform-async-to-generator",
"transform-strict-mode",
["transform-es2015-modules-commonjs", {"allowTopLevelThis": true}]
],
"retainLines": true
]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
([#5560](https://github.com/facebook/jest/pull/5560))
* `[jest-config]` Make it possible to merge `transform` option with preset
([#5505](https://github.com/facebook/jest/pull/5505))
* `[babel-jest]` Remove `retainLines` argument to babel.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"...from babel", or just "Remove usage of retainLines"

Copy link
Member Author

Choose a reason for hiding this comment

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

My thinking was that people would go "what's retainLines, I've never passed that to jest before"

([#5594](https://github.com/facebook/jest/pull/5594))

### Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ exports[`works with async failures 1`] = `
+ \\"foo\\": \\"bar\\",
}

at ../../packages/expect/build/index.js:145:57
at ../../packages/expect/build/index.js:104:76
Copy link
Member Author

Choose a reason for hiding this comment

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

we should probably remove this line entirely

Copy link
Collaborator

Choose a reason for hiding this comment

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

Feel free to remove the lilne:column mapping in a followup


"
`;
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/__tests__/location_in_results.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ it('adds correct location info when provided with flag', () => {
const assertions = result.testResults[0].assertionResults;
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(2);
expect(assertions[0].location).toEqual({column: 1, line: 9});
expect(assertions[1].location).toEqual({column: 3, line: 14});
expect(assertions[0].location).toEqual({column: 1, line: 10});
expect(assertions[1].location).toEqual({column: 2, line: 15});
Copy link
Member Author

Choose a reason for hiding this comment

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

@thymikee your comment disappeared, but here's my response:

I'm a bit unsure about the column change but line wise it was correct earlier as well up until I messed up the 'use strict';, which meant that babel changes the file. Then it was wrong and pointed to the wrong lines.

location without passing it through source map:

{"column": 1, "line": 12}
{"column": 3, "line": 17}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, looking at the source:

screen shot 2018-02-18 at 09 22 54

shouldn't this be:

{"column": 3, "line": 11}
{"column": 5, "line": 16}

?

Copy link
Member Author

@SimenB SimenB Feb 18, 2018

Choose a reason for hiding this comment

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

No, it's the its, not the expects. I do think column should be 3 and not 2 in the second one, though. Unsure why it's not.

Unless it's zero-indexed, in which case the first one should be 0 and not 1...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah pardon me. Then te second column should be 3 instead of 2.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, it should (I just tested with normal js - it's not zero-indexed)

Copy link
Member Author

@SimenB SimenB Feb 18, 2018

Choose a reason for hiding this comment

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

This is a bug in either babel or source-map.

See this example:

'use strict';

const fs = require('fs');
const {transform} = require('babel-core');
const {SourceMapConsumer} = require('source-map');

const fileContent = fs.readFileSync(
  './integration-tests/location-in-results/__tests__/test.js',
  'utf8',
);

const res = transform(fileContent, {
  plugins: ['transform-strict-mode'],
  sourceMaps: 'both',
});

const consumer = new SourceMapConsumer(JSON.stringify(res.map));

console.log(
  // 17:3 in `res.code` has the `it('also works', () => {`
  consumer.originalPositionFor({
    column: 3,
    line: 17,
  }),
);

Logs { source: 'unknown', line: 15, column: 2, name: 'it' }

Copy link
Member Author

Choose a reason for hiding this comment

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

Created https://github.com/SimenB/sourcemap-issues with the reproduction.

@jwbay any ideas here? Should we go to babel or source-map and report it? Or am I doing something really weird?

});
3 changes: 2 additions & 1 deletion integration-tests/location-in-results/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// This file is missing 'use strict' to force babel into doing something
// as we have `transform-strict-mode`

it('no ancestors', () => {
expect(true).toBeTruthy();
Expand Down
1 change: 0 additions & 1 deletion packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const createTransformer = (options: any): Transformer => {
options = Object.assign({}, options, {
plugins: (options && options.plugins) || [],
presets: ((options && options.presets) || []).concat([jestPreset]),
retainLines: true,
sourceMaps: 'both',
});
delete options.cacheDirectory;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"callsites": "^2.0.0",
"chalk": "^2.0.1",
"co": "^4.6.0",
"expect": "^22.3.0",
Expand All @@ -18,6 +17,7 @@
"jest-matcher-utils": "^22.2.0",
"jest-message-util": "^22.2.0",
"jest-snapshot": "^22.2.0",
"jest-util": "^22.3.0",
"source-map-support": "^0.5.0"
},
"devDependencies": {
Expand Down
15 changes: 8 additions & 7 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import type {TestResult} from 'types/TestResult';
import type Runtime from 'jest-runtime';

import path from 'path';
import fs from 'fs';
import callsites from 'callsites';
import fs from 'graceful-fs';
import {getCallsite} from 'jest-util';
import JasmineReporter from './reporter';
import {install as jasmineAsyncInstall} from './jasmine_async';

Expand Down Expand Up @@ -51,7 +51,7 @@ async function jasmine2(
if (config.testLocationInResults === true) {
const originalIt = environment.global.it;
environment.global.it = (...args) => {
const stack = callsites()[1];
const stack = getCallsite(1, runtime.getSourceMaps());
const it = originalIt(...args);

it.result.__callsite = stack;
Expand Down Expand Up @@ -125,12 +125,13 @@ async function jasmine2(
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
if (runtime._sourceMapRegistry[source]) {
const sourceMaps = runtime.getSourceMaps();
const sourceMapSource = sourceMaps && sourceMaps[source];

if (sourceMapSource) {
try {
return {
map: JSON.parse(
fs.readFileSync(runtime._sourceMapRegistry[source]),
),
map: JSON.parse(fs.readFileSync(sourceMapSource)),
url: source,
};
} catch (e) {}
Expand Down
13 changes: 11 additions & 2 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,21 @@ async function runTestInternal(
RuntimeClass,
>);

let runtime = undefined;

const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
const consoleFormatter = (type, message) =>
getConsoleOutput(
config.cwd,
!!globalConfig.verbose,
// 4 = the console call is buried 4 stack frames deep
BufferedConsole.write([], type, message, 4),
BufferedConsole.write(
Copy link
Member Author

Choose a reason for hiding this comment

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

oh, I didn't know this was the way it's hooked up. practical!

Copy link
Member Author

Choose a reason for hiding this comment

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

We should probably pass it to the BufferedConsole as well

[],
type,
message,
4,
runtime && runtime.getSourceMaps(),
),
);

let testConsole;
Expand All @@ -90,6 +98,7 @@ async function runTestInternal(
} else if (globalConfig.verbose) {
testConsole = new Console(consoleOut, process.stderr, consoleFormatter);
} else {
// TODO: Should `BufferedConsole` receive `consoleFormatter` as well?
Copy link
Member Author

Choose a reason for hiding this comment

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

@rickhanlonii I think so - the internal _log call in BufferedConsole is without sourcemaps now

testConsole = new BufferedConsole();
}

Expand All @@ -101,7 +110,7 @@ async function runTestInternal(
const cacheFS = {[path]: testSource};
setGlobal(environment.global, 'console', testConsole);

const runtime = new Runtime(config, environment, resolver, cacheFS, {
runtime = new Runtime(config, environment, resolver, cacheFS, {
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {Context} from 'types/Context';
import type {Jest, LocalModuleRequire} from 'types/Jest';
import type {ModuleMap} from 'jest-haste-map';
import type {MockFunctionMetadata, ModuleMocker} from 'types/Mock';
import type {SourceMapRegistry} from 'types/SourceMaps';

import path from 'path';
import HasteMap from 'jest-haste-map';
Expand Down Expand Up @@ -100,7 +101,7 @@ class Runtime {
_shouldAutoMock: boolean;
_shouldMockModuleCache: BooleanObject;
_shouldUnmockTransitiveDependenciesCache: BooleanObject;
_sourceMapRegistry: {[key: string]: string, __proto__: null};
_sourceMapRegistry: SourceMapRegistry;
_scriptTransformer: ScriptTransformer;
_transitiveShouldMock: BooleanObject;
_unmockList: ?RegExp;
Expand Down Expand Up @@ -449,6 +450,10 @@ class Runtime {
}, {});
}

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

setMock(
from: string,
moduleName: string,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"graceful-fs": "^4.1.11",
"is-ci": "^1.0.10",
"jest-message-util": "^22.2.0",
"mkdirp": "^0.5.1"
"mkdirp": "^0.5.1",
"source-map": "^0.6.0"
},
"devDependencies": {
"jest-mock": "^22.2.0"
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-util/src/buffered_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import type {
LogTimers,
} from 'types/Console';

import type {SourceMapRegistry} from 'types/SourceMaps';

import {Console} from 'console';
import {format} from 'util';
import chalk from 'chalk';
import callsites from 'callsites';
import getCallsite from './get_callsite';

export default class BufferedConsole extends Console {
_buffer: ConsoleBuffer;
Expand All @@ -40,9 +42,10 @@ export default class BufferedConsole extends Console {
type: LogType,
message: LogMessage,
level: ?number,
sourceMaps: ?SourceMapRegistry,
) {
const call = callsites()[level != null ? level : 2];
const origin = call.getFileName() + ':' + call.getLineNumber();
const callsite = getCallsite(level != null ? level : 2, sourceMaps);
const origin = callsite.getFileName() + ':' + callsite.getLineNumber();

buffer.push({
message,
Expand Down
64 changes: 64 additions & 0 deletions packages/jest-util/src/get_callsite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
Copy link
Member Author

Choose a reason for hiding this comment

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

@rickhanlonii would you mind adding a couple of unit tests to this?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, will do

Copy link
Member

Choose a reason for hiding this comment

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

Done

* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/

import type {SourceMapRegistry} from 'types/SourceMaps';

import fs from 'graceful-fs';
import callsites from 'callsites';
import {SourceMapConsumer} from 'source-map';

// Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158
const addSourceMapConsumer = (callsite, consumer) => {
const getLineNumber = callsite.getLineNumber;
const getColumnNumber = callsite.getColumnNumber;
let position = null;

function getPosition() {
if (!position) {
position = consumer.originalPositionFor({
column: getColumnNumber.call(callsite),
line: getLineNumber.call(callsite),
});
}

return position;
}

Object.defineProperties(callsite, {
getColumnNumber: {
value() {
return getPosition().column || getColumnNumber.call(callsite);
},
writable: false,
},
getLineNumber: {
value() {
return getPosition().line || getLineNumber.call(callsite);
},
writable: false,
},
});
};

export default (level: number, sourceMaps: ?SourceMapRegistry) => {
const levelAfterThisCall = level + 1;
const stack = callsites()[levelAfterThisCall];
const sourceMapFileName = sourceMaps && sourceMaps[stack.getFileName()];
Copy link
Member Author

Choose a reason for hiding this comment

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

Either add a ? to the type declaration, or drop the sourceMaps && part


if (sourceMapFileName) {
try {
const sourceMap = fs.readFileSync(sourceMapFileName, 'utf8');
addSourceMapConsumer(stack, new SourceMapConsumer(sourceMap));
} catch (e) {
// ignore
}
}

return stack;
};
2 changes: 2 additions & 0 deletions packages/jest-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import getConsoleOutput from './get_console_output';
import installCommonGlobals from './install_common_globals';
import NullConsole from './null_console';
import isInteractive from './is_interative';
import getCallsite from './get_callsite';
import setGlobal from './set_global';
import deepCyclicCopy from './deep_cyclic_copy';

Expand All @@ -41,6 +42,7 @@ module.exports = {
createDirectory,
deepCyclicCopy,
formatTestResults,
getCallsite,
getConsoleOutput,
getFailedSnapshotTests,
installCommonGlobals,
Expand Down
10 changes: 10 additions & 0 deletions types/SourceMaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/

export type SourceMapRegistry = {[key: string]: string, __proto__: null};