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

feat: extract 'createTransformer' and use type predicates #12407

Merged
merged 21 commits into from
Apr 4, 2022

Conversation

fatso83
Copy link
Contributor

@fatso83 fatso83 commented Feb 16, 2022

Summary

See discussion in #12403. These changes make the interfaces more self-documenting and the intent clearer.

Test plan

Type checks

$ npm run test-types

> @jest/[email protected] test-types /home/carlerik/code/jest
> yarn jest --config jest.config.tsd.js

 PASS   types  packages/jest-types/__typetests__/config.test.ts
 PASS   types  packages/jest-types/__typetests__/jest.test.ts
 PASS   types  packages/expect/__typetests__/expect.test.ts
 PASS   types  packages/jest-types/__typetests__/globals.test.ts
 PASS   types  packages/jest-types/__typetests__/expect.test.ts

Test Suites: 5 passed, 5 total
Tests:       412 passed, 412 total
Snapshots:   0 total
Time:        4.334 s
Ran all test suites.

Unit tests
This failed on some integration tests (differed on runs), but not due to my changes?

$ npm run test

> @jest/[email protected] test /home/carlerik/code/jest
> yarn lint && yarn jest

Summary of all failing tests
 FAIL  e2e/__tests__/complexItemsInErrors.ts (6.038 s)
  ● handles functions that close over outside variables

    Timed out

      at timeoutKill (node_modules/execa/lib/kill.js:65:23)

  ● handles functions that close over outside variables

    Process exited

      51 |   );
      52 |
    > 53 |   await waitUntil(({stderr}) => stderr.includes('Ran all test suites.'));
         |         ^
      54 |
      55 |   const {stderr} = await end();
      56 |

      at Object.waitUntil (e2e/__tests__/complexItemsInErrors.ts:53:9)


Test Suites: 1 failed, 1 skipped, 426 passed, 427 of 428 total
Tests:       1 failed, 38 skipped, 4515 passed, 4554 total
Snapshots:   1748 passed, 1748 total
Time:        320.018 s

@fatso83 fatso83 force-pushed the 12403-transformer-types-update branch from 82013ba to 2ac8aca Compare February 16, 2022 15:35
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

yay!

docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
@fatso83
Copy link
Contributor Author

fatso83 commented Feb 16, 2022

Hmm ... after seeing babel-jest breaking due to a type issue, I tried fixing it, but I am having difficulty getting it to recognize that the type is indeed exported. When running npm run watch:ts I get the same error as the CI:

packages/babel-jest/src/index.ts:22:3 - error TS2305: Module '"@jest/transform"' has no exported member 'TransformerCreator'.

The transform package does (now) have that type exported and I can see it in node_modules/\@jest/transform/build/types.d.ts, so not sure what is going on here. Hoping that pushing a commit fixes 🤷 Seems to be some kind of build ordering issue.

@SimenB
Copy link
Member

SimenB commented Feb 16, 2022

Note that you need to export TransformerCreator from index.ts for it to be visible outside of the module it's declared in

fatso83 added a commit to fatso83/jest that referenced this pull request Feb 17, 2022
fatso83 added a commit to fatso83/jest that referenced this pull request Feb 17, 2022
@fatso83 fatso83 force-pushed the 12403-transformer-types-update branch from ad4a528 to 4bcdf9a Compare February 17, 2022 09:25
@fatso83 fatso83 force-pushed the 12403-transformer-types-update branch 2 times, most recently from 04aebac to e8125a8 Compare February 17, 2022 14:40
@fatso83
Copy link
Contributor Author

fatso83 commented Feb 17, 2022

Finally builds every (*) integration test 🥳 Trang fødsel, as we say. This "simple doc update" did end up touching the transform, repl and babel-jest projects, along with various e2e tests, so you might want to have a look to see if you agree with the changes done to the tests.

I think the only thing that might validate a change is my question on the wording of what is required to implement, as I was a bit unsure myself what you really meant here.

(*) "handles functions that close over outside variables" times out on the Windows builds and "notify › does not report --notify flag" fails on macOS due to using too much time, otherwise it should look pretty good. Pretty sure none of these are related to my changes (?).

@fatso83 fatso83 force-pushed the 12403-transformer-types-update branch from e8125a8 to 91a8f0e Compare February 17, 2022 15:14
@SimenB
Copy link
Member

SimenB commented Feb 17, 2022

Trang fødsel, indeed 😀

Both of those tests are somewhat flaky (I just pushed some retrying to main in the hopes to stabilize at least one of them).

docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
@@ -9,6 +9,8 @@ import type {TransformOptions as BabelTransformOptions} from '@babel/core';
import type {TransformOptions as JestTransformOptions} from '@jest/transform';
import babelJest from '../index';

const {getCacheKey} = babelJest.createTransformer();
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it was either that or const babelJestTransformer = babelJest.createTransformer(); and babelJestTransformer.getCacheKey(...) (or something similar).

Since we have no other exports than createTransformer, you would otherwise need to write babelJest.createTransformer().getCacheKey(...) in every location that used babelJest.getCacheKey(...) previously.

Not sure what you prefer. Since the transformer was not used for anything besides calling its getCacheKey() method, I thought removing it and just keeping the method through destructuring was fine.

Copy link
Member

Choose a reason for hiding this comment

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

why not babelJest.getCacheKey ? It should be on the default export, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since removing all but the factory method was part of this change.

I can of course revert it, but then those exports would just be for the sake of the tests (AFAIK?); the jest-transform package will not touch any of those exports when it sees createTransformer and neither will the CLI, so I thought keeping test-only exports did not make sense from that perspective. I might be wrong, of course 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hope to have addressed all non-code related feedback. Mostly used your suggested prose, apart from introducing some full stops and minor adjustments to make the sentences shorter and easier to read.

With regards to the code changes you wondered about, which mostly stems from the fact that exports that are not used outside of tests have been removed, I am not sure if you think I should revert any of those or if it made sense.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! What I'm alluding to here is that babelJest.getCacheKey should work fine, there's not need to call createTransformer first (I think!)

Copy link
Contributor Author

@fatso83 fatso83 Feb 25, 2022

Choose a reason for hiding this comment

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

And I am saying that functionality was removed in this PR 😄 Sorry if being unclear! See https://github.com/facebook/jest/pull/12407/files#diff-5f463bc5dbe428998302335614e9bbed3b4eb3310a6075767e1b432c161d9e71L273

It used to be:

const transformer: SyncTransformer<TransformOptions> = {
  ...createTransformer(),
  createTransformer
}

export default transformer;

Note the ...createTransformer(). That is what ensures that babelJest.getCacheKey exists.

All of the fields from ...createTransformer() would be ignored by Jest itself when it sees createTransformer. Since they are not used in any actual non-test code, I thought it did not make sense to expose them, since you could always just instantiate a new transformer in the tests. Once their use is gone from tests, no code would use them. So that's the reasoning behind removing them. I could keep them, of course, which would keep the tests unchanged, but what point is there in testing exports that will never be used?

Copy link
Contributor Author

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

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

Thanks a bunch for thorough feedback.

@@ -9,6 +9,8 @@ import type {TransformOptions as BabelTransformOptions} from '@babel/core';
import type {TransformOptions as JestTransformOptions} from '@jest/transform';
import babelJest from '../index';

const {getCacheKey} = babelJest.createTransformer();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it was either that or const babelJestTransformer = babelJest.createTransformer(); and babelJestTransformer.getCacheKey(...) (or something similar).

Since we have no other exports than createTransformer, you would otherwise need to write babelJest.createTransformer().getCacheKey(...) in every location that used babelJest.getCacheKey(...) previously.

Not sure what you prefer. Since the transformer was not used for anything besides calling its getCacheKey() method, I thought removing it and just keeping the method through destructuring was fine.

docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
packages/jest-transform/src/types.ts Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
@fatso83 fatso83 force-pushed the 12403-transformer-types-update branch from fcc5574 to 940c43d Compare February 25, 2022 14:14
@SimenB
Copy link
Member

SimenB commented Feb 28, 2022

Sorry, missed the last push!

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks! veeeeeery close 😀

My last general question is if we should use admonitions int the docs? I feel like they might help break up the text a bit and make it more digestible? might be wrong, tho!

docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
docs/CodeTransformation.md Outdated Show resolved Hide resolved
@fatso83
Copy link
Contributor Author

fatso83 commented Feb 28, 2022

My last general question is if we should use admonitions int the docs?

I am change blind 🙈 Can someone do that in a separate PR? I think I am blind to what is better, more clear, etc at this point. And especially whether or not to add some color blocks and where to put them 🤷

@SimenB
Copy link
Member

SimenB commented Feb 28, 2022

Yeah, no problem 👍

@fatso83
Copy link
Contributor Author

fatso83 commented Mar 21, 2022

Hmm ... this is stale, but I am not sure why 🤷 Should I address something or are we good ⬆️?

@SimenB
Copy link
Member

SimenB commented Mar 21, 2022

Should be good - I've been away on vacation, so haven't had time to properly review

@fatso83
Copy link
Contributor Author

fatso83 commented Mar 22, 2022

Totally understandable; I was just not sure if this was blocking due to me or not, since it seemed everything substantial had been addressed.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks!

@SimenB SimenB changed the title Extract 'createTransformer' and use type predicates feat: extract 'createTransformer' and use type predicates Apr 4, 2022
@SimenB SimenB merged commit b096941 into jestjs:main Apr 4, 2022
@SimenB
Copy link
Member

SimenB commented Apr 5, 2022

@github-actions
Copy link

github-actions bot commented May 6, 2022

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 6, 2022
@fatso83 fatso83 deleted the 12403-transformer-types-update branch May 9, 2022 16:03
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.

3 participants