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

[Feature]: Jest-Cli exports some util APIs #12456

Closed
adamma1024 opened this issue Feb 22, 2022 · 7 comments
Closed

[Feature]: Jest-Cli exports some util APIs #12456

adamma1024 opened this issue Feb 22, 2022 · 7 comments

Comments

@adamma1024
Copy link

🚀 Feature Proposal

Proposal: jest-cli package export buildArgv API and init API, or adding callback/plugin system.

https://github.com/facebook/jest/blob/356b3b05b77f927b49e151302edbef6594d09a22/packages/jest-cli/src/cli/index.ts#L51-L86

https://github.com/facebook/jest/blob/34308ae21827a44916471106b4e3afdb3779cbb1/packages/jest-cli/src/init/index.ts#L40-L155

Motivation

My purpose is to do something else when invoke run API of jest-cli, like this:

async function run (maybeArgv, ...) {
  const argv = buildArgv(maybeArgv);

  if (argv.init) {
      await init();
      return;
  }

 // do something
}

As we can see, if I want to let it to work well, I should import buildArgv API and init API from jest-cli/build/cli/index and jest-cli/build/init
After v27, I noticed that jest-cli did not export them anymore. I'm so agreed that it's a great idea to just export APIs which were introduced in the document. But how can I solve this problem in an easy way? I think that I can only rewrite a jest-cli to finish it.
Which sounds so stupid.
So, can jest-cli support this workaround by exporting some util APIs, or adding a callback/plugin system?

Example

By exports in package.json ( the easiest way I think ):

  "exports": {
    ".": {
      "types": "./build/index.d.ts",
      "default": "./build/index.js"
    },
    "./package.json": "./package.json",
    "./bin/jest": "./bin/jest.js",
    "./util": "blabla"     // add this line
  },

By plugin system/ callback:

export async function run(
  maybeArgv?: Array<string>,
  project?: string,
  callback?: any, // add a callback
): Promise<void> {
  try {
    const argv = await buildArgv(maybeArgv);

    if (argv.init) {
      await init();
      return;
    }

    // use callback if it exists.
    if(callback) {
        callback(argv, project);
    } else {
        const projects = getProjectListFromCLIArgs(argv, project);

        const {results, globalConfig} = await runCLI(argv, projects);
        readResultsAndExit(results, globalConfig);
    }
  } catch (error: any) {
    clearLine(process.stderr);
    clearLine(process.stdout);
    if (error?.stack) {
      console.error(chalk.red(error.stack));
    } else {
      console.error(chalk.red(error));
    }

    exit(1);
    throw error;
  }
}

Pitch

The most important point is that I think jest really need a plugin system( or callback ) to ensure users can do something else easier.
The second point is a little "selfish" because jest doing that is really easier than me, lol. Otherwise, I must rewrite jest-cli in my project to do that.

@adamma1024 adamma1024 changed the title [Feature]: Jest-Cli exports some utils API [Feature]: Jest-Cli exports some util APIs Feb 22, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Mar 24, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@SimenB
Copy link
Member

SimenB commented Apr 23, 2022

@adamma1024 sorry, missed this. You're basically wanting #5048. However, we can export those APIs you imported from build if you want. Wanna send a PR?

@SimenB SimenB reopened this Apr 23, 2022
@github-actions github-actions bot removed the Stale label Apr 23, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label May 23, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@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 Jul 23, 2022
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

2 participants