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

Add "dependencies" in jest transformers #6680

Closed
Schniz opened this issue Jul 11, 2018 · 4 comments
Closed

Add "dependencies" in jest transformers #6680

Schniz opened this issue Jul 11, 2018 · 4 comments

Comments

@Schniz
Copy link

Schniz commented Jul 11, 2018

Hey! 👋
Thanks for all the hard work you're putting in Jest. I love it. So thanks ❤️
I'm not quite sure that what I'm asking for is right, but I hope so - I haven't thought about a different way to solve my problem.

🚀 Feature Proposal

Adding a way to declare code dependencies in jest transform steps, that will eventually help jest to know when to invalidate cache (for babel compilation, etc)

Motivation

Imagine that you use prepack for some of your code and you want to test it. You'd probably write a babel plugin (or a custom transformer) to do that. Unfortunately, prepack, like any other "bundler" with runtime, will leverage dependencies. This means, that the cache that jest has on the file (which is basically its file name) isn't good enough.

It's not just for "prepack" or bundlers, if we'll take a look at @kentcdodds's import-all.macro, we'll see that you can import a directory using importAll('./dir/*.js'). If we can say "this code depends on any file in this directory".

Even when declaring getCacheKey, for some reason the cache doesn't invalidate while in watch mode.

Currently, if you use some kind of "bundler" or any babel plugin/macro like this, you need to use --no-cache in jest.

Example

Currently, a custom jest transformer can compile files using custom steps:

module.exports = {
  process(fileContents, fileName) {
    return compileTheFile(fileContents)
  }
}

I'm suggesting adding a method or data structure that will provide a way to include the dependencies:

module.exports = {
  process(fileContents, fileName) {
    const {code, dependencies} = runPrepackSynchronously(fileName);
    return {code, dependencies: [...dependencies, maybeSomeOtherFilenameHere]}
  }
}

This way jest will know when to invalidate the cache (whenever one of these files changes!)

we can still support the string return value from process for simpler usages!

Pitch

Supporting explicit dependencies will help making our apps more testable. Changes in sass files could be watched in Jest. Prepack, webpack configurations, etc, will be able to be tested too. Wider babel plugins support.

Actually, what I'd like more is a way of sharing these dependencies between babel plugins and jest without writing a custom transformer. but maybe that'll be a PR for babel-jest after landing this feature! 😸

@SimenB
Copy link
Member

SimenB commented Jan 8, 2019

Does #7313 (and #7349, #7350) cover this use case? It allows you to tell jest-haste-map what dependencies a file has.

It'll be available in the next major of Jest (currently at jest@beta, jest@24.0.0-alpha.9)

@Schniz
Copy link
Author

Schniz commented Jan 8, 2019

Yes, it sounds like it solves that. Thanks @SimenB 😸

cc @yanivefraim

@SimenB
Copy link
Member

SimenB commented Jan 8, 2019

Awesome! Closing then 🙂 Happy to reopen if it isn't enough. Docs: https://jestjs.io/docs/en/next/configuration#dependencyextractor-string

@github-actions
Copy link

This issue 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

No branches or pull requests

3 participants