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(reporters): add static filepath property to all reporters #11015

Merged
merged 2 commits into from
Jan 19, 2021

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Jan 18, 2021

Summary

https://github.com/jevakallio/jest-clean-console-reporter documents a pattern of doing

    // Overriding config.reporters wipes out default reporters, so
    // we need to restore the summary reporter
    "@jest/reporters/build/SummaryReporter",

This won't work in Jest 27 due to #9921.

$ node -p 'require("@jest/reporters/build/SummaryReporter")'

internal/modules/cjs/loader.js:438
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './build/SummaryReporter' is not defined by "exports" in /Users/simen/repos/jest/node_modules/@jest/reporters/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:513:3)
    at resolveExports (internal/modules/cjs/loader.js:432:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:472:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at [eval]:1:1
    at Script.runInThisContext (vm.js:133:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

This change allows one to do

const {SummaryReporter} = require('@jest/reporters');

// this absolute path can be passed to `require`
const path = SummaryReporter.filepath;

/cc @jevakallio

Test plan

Tested in the shell 😅

$ node -p 'require("@jest/reporters").SummaryReporter.filename'

/Users/simen/repos/jest/packages/jest-reporters/build/SummaryReporter.js
$ node -p 'require(require("@jest/reporters").SummaryReporter.filename)'

{
  default: [class SummaryReporter extends BaseReporter] {
    filename: '/Users/simen/repos/jest/packages/jest-reporters/build/SummaryReporter.js'
  }
}

And since we handle default "export" from CJS, this should work fine 👍

https://github.com/facebook/jest/blob/f0dc9932cba828a1c51ef842c00c1ca51b7f2796/packages/jest-core/src/TestScheduler.ts#L392

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.

👍🏼

@SimenB SimenB merged commit d00695d into jestjs:master Jan 19, 2021
@SimenB SimenB deleted the reporter-filename branch January 19, 2021 07:00
@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 10, 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.

3 participants