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 support for custom dependency extractors #7313

Merged
merged 1 commit into from
Nov 1, 2018

Conversation

rubennorte
Copy link
Contributor

Summary

Adds a new dependencyExtractor option to use a custom function to extract dependencies from the code. It will allow users to implement support for custom functions loading modules (which will be taken into account to determine which tests to run):

package.json:

{
  ...
  "jest": {
    "dependencyExtractor": "<rootDir>/dependencyExtractor.js"
  }
}

dependencyExtractor:

const blockCommentRe = /\/\*[^]*?\*\//g;
const lineCommentRe = /\/\/.*/g;
const LOAD_MODULE_RE = /(?:^|[^.]\\s*)(\\bloadModule\\s*?\\(\\s*?)([\`'"])([^\`'"]+)(\\2\\s*?\\))/g;

module.exports = function dependencyExtractor(code, defaultExtractor) {
  const dependencies = defaultExtractor(code);
  const addDependency = (match, pre, quot, dep, post) => {
    dependencies.add(dep);
    return match;
  };

  code
    .replace(blockCommentRe, '')
    .replace(lineCommentRe, '')
    .replace(LOAD_MODULE_RE, addDependency);

  return dependencies;
};

sample-test.js:

const module = loadModule('./foo');

it('should reload when `foo` changes!', () => {
  // ...
});

Test plan

Updated unit tests for the jest-haste-map and added an E2E test for this feature.

@codecov-io
Copy link

Codecov Report

Merging #7313 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7313      +/-   ##
==========================================
- Coverage   66.64%   66.63%   -0.02%     
==========================================
  Files         241      241              
  Lines        9337     9334       -3     
  Branches        5        6       +1     
==========================================
- Hits         6223     6220       -3     
  Misses       3111     3111              
  Partials        3        3
Impacted Files Coverage Δ
packages/jest-config/src/ValidConfig.js 100% <ø> (ø) ⬆️
packages/jest-config/src/index.js 19.35% <ø> (ø) ⬆️
packages/jest-runtime/src/index.js 76.99% <ø> (ø) ⬆️
packages/jest-haste-map/src/index.js 92.05% <ø> (ø) ⬆️
packages/jest-config/src/normalize.js 85.53% <ø> (ø) ⬆️
packages/jest-config/src/Defaults.js 100% <ø> (ø) ⬆️
packages/jest-haste-map/src/worker.js 94.44% <100%> (+0.15%) ⬆️
packages/jest-haste-map/src/lib/extractRequires.js 100% <100%> (ø) ⬆️
packages/jest-mock/src/index.js 85.41% <0%> (-0.14%) ⬇️
packages/expect/src/spyMatchers.js 97.43% <0%> (-0.02%) ⬇️

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 bb9efac...f5c65f9. Read the comment docs.

@SimenB
Copy link
Member

SimenB commented Nov 1, 2018

Oh, awesome! I actually started a branch with this stuff months ago, but never was able to finish it

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.

Nice, integrating non-js runners is gonna be way better now.

@rubennorte rubennorte merged commit 223c3db into jestjs:master Nov 1, 2018
@rubennorte rubennorte deleted the custom-dependency-extractor branch November 1, 2018 18:37
@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.

5 participants