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

Refactor configs & transform #3376

Merged
merged 3 commits into from
Apr 27, 2017
Merged

Refactor configs & transform #3376

merged 3 commits into from
Apr 27, 2017

Conversation

cpojer
Copy link
Member

@cpojer cpojer commented Apr 26, 2017

Summary

This diff starts to actually separate configs into two individual ones with distinct keys rather than overlaps. It requires a bunch of rewrites because some config fields were passed into the wrong places in the first place. For example, I made it so we passed "watch" to babel-jest, which should not know anything about how it is being used. I had to rewrite the entire transform pipeline (transform.js to ScriptTransformer.js) to cache transform specific data (like the location of .babelrc) per test rather than globally. However, I think it was worth it. The new ScriptTransformer makes much more sense than the spaghetti file we had there before.

There'll be more diffs on top of this, I recommend reviewing this in the order of the commits, with the last one being the biggest one. There'll be many more diffs to separate configs more.

Test plan

jest

@codecov-io
Copy link

codecov-io commented Apr 26, 2017

Codecov Report

Merging #3376 into master will decrease coverage by <.01%.
The diff coverage is 79.41%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #3376      +/-   ##
=========================================
- Coverage    64.2%   64.2%   -0.01%     
=========================================
  Files         176     176              
  Lines        6546    6532      -14     
  Branches        4       4              
=========================================
- Hits         4203    4194       -9     
+ Misses       2342    2337       -5     
  Partials        1       1
Impacted Files Coverage Δ
packages/jest-config/src/index.js 26.08% <ø> (ø) ⬆️
packages/jest-jasmine2/src/setup-jest-globals.js 0% <0%> (ø) ⬆️
packages/jest-jasmine2/src/index.js 0% <0%> (ø) ⬆️
packages/jest-cli/src/reporters/SummaryReporter.js 26.15% <0%> (ø) ⬆️
packages/jest-cli/src/runJest.js 0% <0%> (ø) ⬆️
packages/jest-cli/src/runTest.js 15.55% <0%> (ø) ⬆️
packages/jest-cli/src/lib/isValidPath.js 100% <100%> (ø) ⬆️
packages/jest-cli/src/generateEmptyCoverage.js 88.88% <100%> (ø) ⬆️
packages/jest-cli/src/watch.js 76.11% <50%> (-0.18%) ⬇️
packages/jest-runtime/src/index.js 75.87% <63.63%> (+0.87%) ⬆️
... and 3 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 55c2eff...bea6547. Read the comment docs.

@cpojer cpojer changed the title Refactor configs Refactor configs & transform Apr 27, 2017
.update('\0', 'utf8')
// Don't use the in-memory cache in watch mode because the .babelrc
// file may be modified.
.update(getBabelRC(filename, {useCache: !watch}))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did it even work earlier?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yap this worked earlier but the code was terrible!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, what's the idea behind moving getBabelRC into createTransformer? Just curious

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the reason I rewrote transform into ScriptTransformer. What we do now when we get a transformer, we check if createTransformer exists. If it does, we call that. We do this now once per Runtime instance (once per Test). This means every test will have its own babelrc file cache instead of having a single cache for the entirety of Jest's lifetime. Does that make sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Totally

}

if (globalConfig.updateSnapshot === true) {
setConfig(contexts, {updateSnapshot: true});
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 the less config mutations the better

let vm;

describe('transform', () => {
describe('ScriptTransformer', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's also rename the test file (and corresponding snapshot) to ScriptTransformer-test.js for consistency.

@@ -157,41 +158,39 @@ describe('transform', () => {
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');

// in-memory cache
const response2 = transform('/fruits/banana.js', config).script;
const response2 = scriptTransformer.transform('/fruits/banana.js').script;
expect(response2).toBe(response);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we make sure that the fs is not called again?

// in-memory cache
fs.readFileSync.mockClear();
const response2 = scriptTransformer.transform('/fruits/banana.js').script;
expect(response2).toBe(response);
expect(fs.readFileSync).not.toHaveBeenCalled();

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 can assume if we have referential equality, that we didn't go to the filesystem and create another vm.Script instance.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, right 🤦‍♂️ 😄

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Got all the way through, it really starts shaping up! I was going to mention something about ProjectConfig and GlobalConfig having duplicate keys, but I see it's resolved in #3388

@cpojer cpojer merged commit f5eca01 into jestjs:master Apr 27, 2017
@cpojer
Copy link
Member Author

cpojer commented Apr 27, 2017

I renamed the transform-test.js file. Thanks for that recommendation and the thorough review!

@felipeochoa felipeochoa mentioned this pull request Apr 29, 2017
@cpojer cpojer deleted the refactor-configs branch May 4, 2017 15:44
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Remove “updateSnapshot” from `ProjectConfig`, fix watch mode behavior.

* Remove `verbose`, `watch`, `watchman`, `testNamePattern` from ProjectConfig.

* Refactor transform.js into a ScriptTransformer
@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 13, 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.

4 participants