generateConfigFile.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _jestConfig() {
  7. const data = require('jest-config');
  8. _jestConfig = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. /**
  14. * Copyright (c) Meta Platforms, Inc. and affiliates.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. */
  19. const stringifyOption = (option, map, linePrefix = '') => {
  20. const description = _jestConfig().descriptions[option];
  21. const optionDescription =
  22. description != null && description.length > 0 ? ` // ${description}` : '';
  23. const stringifiedObject = `${option}: ${JSON.stringify(
  24. map[option],
  25. null,
  26. 2
  27. )}`;
  28. return `${optionDescription}\n${stringifiedObject
  29. .split('\n')
  30. .map(line => ` ${linePrefix}${line}`)
  31. .join('\n')},`;
  32. };
  33. const generateConfigFile = (results, generateEsm = false) => {
  34. const {useTypescript, coverage, coverageProvider, clearMocks, environment} =
  35. results;
  36. const overrides = {};
  37. if (coverage) {
  38. Object.assign(overrides, {
  39. collectCoverage: true,
  40. coverageDirectory: 'coverage'
  41. });
  42. }
  43. if (coverageProvider === 'v8') {
  44. Object.assign(overrides, {
  45. coverageProvider: 'v8'
  46. });
  47. }
  48. if (environment === 'jsdom') {
  49. Object.assign(overrides, {
  50. testEnvironment: 'jsdom'
  51. });
  52. }
  53. if (clearMocks) {
  54. Object.assign(overrides, {
  55. clearMocks: true
  56. });
  57. }
  58. const overrideKeys = Object.keys(overrides);
  59. const properties = [];
  60. for (const option in _jestConfig().descriptions) {
  61. const opt = option;
  62. if (overrideKeys.includes(opt)) {
  63. properties.push(stringifyOption(opt, overrides));
  64. } else {
  65. properties.push(stringifyOption(opt, _jestConfig().defaults, '// '));
  66. }
  67. }
  68. const configHeaderMessage = `/**
  69. * For a detailed explanation regarding each configuration property, visit:
  70. * https://jestjs.io/docs/configuration
  71. */
  72. `;
  73. const jsDeclaration = `/** @type {import('jest').Config} */
  74. const config = {`;
  75. const tsDeclaration = `import type {Config} from 'jest';
  76. const config: Config = {`;
  77. const cjsExport = 'module.exports = config;';
  78. const esmExport = 'export default config;';
  79. return [
  80. configHeaderMessage,
  81. useTypescript ? tsDeclaration : jsDeclaration,
  82. properties.join('\n\n'),
  83. '};\n',
  84. useTypescript || generateEsm ? esmExport : cjsExport,
  85. ''
  86. ].join('\n');
  87. };
  88. var _default = generateConfigFile;
  89. exports.default = _default;