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

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Feb 17, 2018

Summary

Pushing up what I've got for now. Tests are failing because callsites does not look up sourcemaps. I've added a TODO in the code where the cause of the failing tests are.

Only outstanding issue is with columns after looking up a sourcemap. See #5594 (comment). Help appreciated!

Fixes #5326.

Test plan

Old tests should pass, even with no retainLines

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.

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

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

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

@codecov-io
Copy link

codecov-io commented Feb 17, 2018

Codecov Report

Merging #5594 into master will increase coverage by 1.34%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5594      +/-   ##
==========================================
+ Coverage   60.94%   62.28%   +1.34%     
==========================================
  Files         215      216       +1     
  Lines        7335     7898     +563     
  Branches        3        4       +1     
==========================================
+ Hits         4470     4919     +449     
- Misses       2864     2978     +114     
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-util/src/index.js 90.47% <ø> (+1%) ⬆️
packages/jest-runner/src/run_test.js 2.17% <0%> (-0.05%) ⬇️
packages/jest-util/src/get_callsite.js 75% <100%> (ø)
packages/jest-runtime/src/index.js 73.59% <100%> (+0.18%) ⬆️
packages/jest-jasmine2/src/index.js 17.2% <60%> (+11.72%) ⬆️
packages/jest-util/src/buffered_console.js 63.15% <66.66%> (+2.04%) ⬆️
packages/jest-jasmine2/src/jasmine_async.js 11.86% <0%> (-21.47%) ⬇️
packages/jest-config/src/validate_pattern.js 85.71% <0%> (-14.29%) ⬇️
packages/jest-config/src/get_max_workers.js 90% <0%> (-10%) ⬇️
packages/jest-config/src/defaults.js 92.85% <0%> (-7.15%) ⬇️
... and 73 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7bcdc8c...736eb2b. Read the comment docs.


// Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158
const addSourceMapConsumer = (callsite, consumer) => {
try {
Copy link
Member Author

Choose a reason for hiding this comment

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

this try can be removed, it's within another try


return stack;
} catch (e) {
return null;
Copy link
Member Author

Choose a reason for hiding this comment

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

it should return stack at the end either way

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

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

@@ -125,12 +124,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

try {
if (sourceMapFileName) {
const sourceMap = fs.readFileSync(sourceMapFileName, 'utf8');
if (sourceMap) {
Copy link
Member Author

Choose a reason for hiding this comment

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

is this needed? readFileSync will just throw

@SimenB
Copy link
Member Author

SimenB commented Feb 17, 2018

Merging #5594 into master will increase coverage by 1.02%.

woah, what's up?

EDIT: Seemingly bringing the coverage back from #5177 (which was minus 1.10%)

https://codecov.io/gh/facebook/jest/pull/5177

@SimenB SimenB requested a review from thymikee February 17, 2018 23:12
@@ -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

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

CHANGELOG.md Outdated
@@ -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"

@@ -90,7 +98,7 @@ async function runTestInternal(
} else if (globalConfig.verbose) {
testConsole = new Console(consoleOut, process.stderr, consoleFormatter);
} else {
testConsole = new BufferedConsole();
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
Copy link
Member Author

Choose a reason for hiding this comment

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

went with a getter as consoleFormatter seemed like it did too much

Copy link
Member

Choose a reason for hiding this comment

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

looks great

fwiw i don't think there's an integration test for this path

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


describe('getCallsite', () => {
beforeEach(() => {
fs.readFileSync = jest.fn();
Copy link
Member Author

Choose a reason for hiding this comment

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

When you do jest.mock without a factory, all exports are mocks by default. So this beforeEach shouldn't be necessary

Copy link
Member Author

Choose a reason for hiding this comment

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

You can do jest.clearMocks if needed

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 test also fails CI?

Copy link
Member

Choose a reason for hiding this comment

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

Fixed, not sure what I was thinking

@SimenB SimenB force-pushed the remove-retainlines branch from 8347bb5 to 736eb2b Compare February 21, 2018 06:46
@SimenB
Copy link
Member Author

SimenB commented Feb 21, 2018

@cpojer This is ready except for the comments here: https://github.com/facebook/jest/pull/5594/files#r168943134

Thoughts?

@cpojer cpojer merged commit 23eec74 into jestjs:master Feb 21, 2018
@cpojer
Copy link
Member

cpojer commented Feb 21, 2018

This is really good work. I like how it actually simplifies things in Jest and makes it more correct. The one offset that's wrong is fine for now, let's just make sure this is properly reported in the right repo and eventually fixed upstream :)

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[babel-jest] Failures on decorators with async methods
7 participants