export default async()

in packages/preset-monorepo/src/index.ts [87:134]


export default async (config: IOption, cmdArgs: any) => {
  const { packageDirs, dirWithPackages } = config;
  const monoRootDir = config.monoRootDir || process.cwd();

  let jestConfigs;
  if (packageDirs) {
    jestConfigs = await detectJestConfigsFromPackageDirs(packageDirs);
  } else if (dirWithPackages) {
    jestConfigs = await detectJestConfigsFromDirWithPackages(dirWithPackages);
  } else {
    jestConfigs = await detectJestConfigsFromDirWithPackages(
      path.resolve(monoRootDir, 'packages')
    );
  }

  const rootJestConfig = {
    projects: jestConfigs,
  };
  // 调用runCLI的时候,必须提供配置【路径】,直接传入配置对象会出问题
  // 因此我们先把解析好的配置写到一个文件中,然后通过这个文件的路径来指定jest配置
  writeFileSync(
    JEST_CONFIG_PATH,
    `module.exports=${JSON.stringify(rootJestConfig)}`
  );

  const bootJest = async (jestConfig: any, cliArgs: any) => {
    await runCLI(
      {
        config: JEST_CONFIG_PATH,
        coverageDirectory: path.resolve(monoRootDir, 'coverage'),
        ...cliArgs,
      } as any,
      [monoRootDir]
    );
  };

  const plugins = [
    [
      require.resolve('@alicloud/console-toolkit-plugin-unit-jest'),
      {
        bootJest: bootJest,
      },
    ],
  ];
  return {
    plugins,
  };
};