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 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
]
}
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
1 change: 1 addition & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"jest-matcher-utils": "^22.2.0",
"jest-message-util": "^22.2.0",
"jest-snapshot": "^22.2.0",
"source-map": "^0.6.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

newest is 0.7.1, but they changed their API to be asynchronous, so we can't use it

"source-map-support": "^0.5.0"
},
"devDependencies": {
Expand Down
55 changes: 50 additions & 5 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,48 @@ 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 {SourceMapConsumer} from 'source-map';
import JasmineReporter from './reporter';
import {install as jasmineAsyncInstall} from './jasmine_async';

const JASMINE = require.resolve('./jasmine/jasmine_light.js');

// Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158
Copy link
Member Author

@SimenB SimenB Feb 17, 2018

Choose a reason for hiding this comment

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

@rexxars thoughts on exporting extendCallsite from sourcemap-decorate-callsites?

I wanted to use your module directly, but I don't want the searching for sourcemaps - I have it already. I also need this to be sync

Copy link
Member Author

Choose a reason for hiding this comment

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

Or just expose an API where I can pass ina callsite and a sourcemap, that would be even better (as long as it's sync) 🙂

Copy link

Choose a reason for hiding this comment

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

I'd welcome the change, but I'm absolutely swamped with work - be happy if anyone would submit a PR, though.

function extendCallsite(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,
},
});

return callsite;
}

async function jasmine2(
globalConfig: GlobalConfig,
config: ProjectConfig,
Expand Down Expand Up @@ -53,6 +88,17 @@ async function jasmine2(
environment.global.it = (...args) => {
const stack = callsites()[1];
const it = originalIt(...args);
const sourceMapForFile = runtime.getSourceMapForFile(stack.getFileName());

if (sourceMapForFile) {
try {
const consumer = new SourceMapConsumer(sourceMapForFile);

extendCallsite(stack, consumer);
} catch (e) {
// ignored
}
}

it.result.__callsite = stack;

Expand Down Expand Up @@ -125,12 +171,11 @@ async function jasmine2(
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
if (runtime._sourceMapRegistry[source]) {
const sourceMap = runtime.getSourceMapForFile(source);
Copy link
Member Author

Choose a reason for hiding this comment

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

should probably use runtime.getSourceMaps() here as well and remove getSourceMapForFile

if (sourceMap) {
try {
return {
map: JSON.parse(
fs.readFileSync(runtime._sourceMapRegistry[source]),
),
map: JSON.parse(sourceMap),
url: source,
};
} catch (e) {}
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ class Runtime {
}, {});
}

getSourceMapForFile(filename: string): ?string {
const sourceMap = this._sourceMapRegistry[filename];

if (sourceMap && fs.existsSync(sourceMap)) {
return fs.readFileSync(sourceMap, 'utf8');
Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if we should cache these instead of hitting the disk every time

Copy link
Member Author

Choose a reason for hiding this comment

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

can try-catch instead of 2 fs operations, at least

}

return null;
}

setMock(
from: string,
moduleName: string,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-util/src/buffered_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default class BufferedConsole extends Console {
message: LogMessage,
level: ?number,
) {
// TODO: This callsite needs to read sourcemaps
Copy link
Member Author

Choose a reason for hiding this comment

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

The issue is here: https://github.com/facebook/jest/blob/430aebe49e9f144d29f144f5a29482315e269e4a/packages/jest-runner/src/run_test.js#L86-L108

I need to pass in runtime to the console, but environment requires testConsole and the runtime requires environment

const call = callsites()[level != null ? level : 2];
const origin = call.getFileName() + ':' + call.getLineNumber();

Expand Down