index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. 'use strict';
  2. function _crypto() {
  3. const data = require('crypto');
  4. _crypto = function () {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function path() {
  10. const data = _interopRequireWildcard(require('path'));
  11. path = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _core() {
  17. const data = require('@babel/core');
  18. _core = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _chalk() {
  24. const data = _interopRequireDefault(require('chalk'));
  25. _chalk = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function fs() {
  31. const data = _interopRequireWildcard(require('graceful-fs'));
  32. fs = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _slash() {
  38. const data = _interopRequireDefault(require('slash'));
  39. _slash = function () {
  40. return data;
  41. };
  42. return data;
  43. }
  44. var _loadBabelConfig = require('./loadBabelConfig');
  45. function _interopRequireDefault(obj) {
  46. return obj && obj.__esModule ? obj : {default: obj};
  47. }
  48. function _getRequireWildcardCache() {
  49. if (typeof WeakMap !== 'function') return null;
  50. var cache = new WeakMap();
  51. _getRequireWildcardCache = function () {
  52. return cache;
  53. };
  54. return cache;
  55. }
  56. function _interopRequireWildcard(obj) {
  57. if (obj && obj.__esModule) {
  58. return obj;
  59. }
  60. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  61. return {default: obj};
  62. }
  63. var cache = _getRequireWildcardCache();
  64. if (cache && cache.has(obj)) {
  65. return cache.get(obj);
  66. }
  67. var newObj = {};
  68. var hasPropertyDescriptor =
  69. Object.defineProperty && Object.getOwnPropertyDescriptor;
  70. for (var key in obj) {
  71. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  72. var desc = hasPropertyDescriptor
  73. ? Object.getOwnPropertyDescriptor(obj, key)
  74. : null;
  75. if (desc && (desc.get || desc.set)) {
  76. Object.defineProperty(newObj, key, desc);
  77. } else {
  78. newObj[key] = obj[key];
  79. }
  80. }
  81. }
  82. newObj.default = obj;
  83. if (cache) {
  84. cache.set(obj, newObj);
  85. }
  86. return newObj;
  87. }
  88. /**
  89. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  90. *
  91. * This source code is licensed under the MIT license found in the
  92. * LICENSE file in the root directory of this source tree.
  93. */
  94. const THIS_FILE = fs().readFileSync(__filename);
  95. const jestPresetPath = require.resolve('babel-preset-jest');
  96. const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); // Narrow down the types
  97. const createTransformer = userOptions => {
  98. var _inputOptions$plugins, _inputOptions$presets;
  99. const inputOptions =
  100. userOptions !== null && userOptions !== void 0 ? userOptions : {};
  101. const options = {
  102. ...inputOptions,
  103. caller: {
  104. name: 'babel-jest',
  105. supportsDynamicImport: false,
  106. supportsExportNamespaceFrom: false,
  107. supportsStaticESM: false,
  108. supportsTopLevelAwait: false,
  109. ...inputOptions.caller
  110. },
  111. compact: false,
  112. plugins:
  113. (_inputOptions$plugins = inputOptions.plugins) !== null &&
  114. _inputOptions$plugins !== void 0
  115. ? _inputOptions$plugins
  116. : [],
  117. presets: ((_inputOptions$presets = inputOptions.presets) !== null &&
  118. _inputOptions$presets !== void 0
  119. ? _inputOptions$presets
  120. : []
  121. ).concat(jestPresetPath),
  122. sourceMaps: 'both'
  123. };
  124. function loadBabelConfig(cwd, filename, transformOptions) {
  125. var _transformOptions$sup,
  126. _transformOptions$sup2,
  127. _transformOptions$sup3,
  128. _transformOptions$sup4;
  129. // `cwd` first to allow incoming options to override it
  130. const babelConfig = (0, _loadBabelConfig.loadPartialConfig)({
  131. cwd,
  132. ...options,
  133. caller: {
  134. ...options.caller,
  135. supportsDynamicImport:
  136. (_transformOptions$sup =
  137. transformOptions === null || transformOptions === void 0
  138. ? void 0
  139. : transformOptions.supportsDynamicImport) !== null &&
  140. _transformOptions$sup !== void 0
  141. ? _transformOptions$sup
  142. : options.caller.supportsDynamicImport,
  143. supportsExportNamespaceFrom:
  144. (_transformOptions$sup2 =
  145. transformOptions === null || transformOptions === void 0
  146. ? void 0
  147. : transformOptions.supportsExportNamespaceFrom) !== null &&
  148. _transformOptions$sup2 !== void 0
  149. ? _transformOptions$sup2
  150. : options.caller.supportsExportNamespaceFrom,
  151. supportsStaticESM:
  152. (_transformOptions$sup3 =
  153. transformOptions === null || transformOptions === void 0
  154. ? void 0
  155. : transformOptions.supportsStaticESM) !== null &&
  156. _transformOptions$sup3 !== void 0
  157. ? _transformOptions$sup3
  158. : options.caller.supportsStaticESM,
  159. supportsTopLevelAwait:
  160. (_transformOptions$sup4 =
  161. transformOptions === null || transformOptions === void 0
  162. ? void 0
  163. : transformOptions.supportsTopLevelAwait) !== null &&
  164. _transformOptions$sup4 !== void 0
  165. ? _transformOptions$sup4
  166. : options.caller.supportsTopLevelAwait
  167. },
  168. filename
  169. });
  170. if (!babelConfig) {
  171. throw new Error(
  172. `babel-jest: Babel ignores ${_chalk().default.bold(
  173. (0, _slash().default)(path().relative(cwd, filename))
  174. )} - make sure to include the file in Jest's ${_chalk().default.bold(
  175. 'transformIgnorePatterns'
  176. )} as well.`
  177. );
  178. }
  179. return babelConfig;
  180. }
  181. return {
  182. canInstrument: true,
  183. getCacheKey(fileData, filename, configString, cacheKeyOptions) {
  184. const {config, instrument, rootDir} = cacheKeyOptions;
  185. const babelOptions = loadBabelConfig(
  186. config.cwd,
  187. filename,
  188. cacheKeyOptions
  189. );
  190. const configPath = [
  191. babelOptions.config || '',
  192. babelOptions.babelrc || ''
  193. ];
  194. return (0, _crypto().createHash)('md5')
  195. .update(THIS_FILE)
  196. .update('\0', 'utf8')
  197. .update(JSON.stringify(babelOptions.options))
  198. .update('\0', 'utf8')
  199. .update(fileData)
  200. .update('\0', 'utf8')
  201. .update(path().relative(rootDir, filename))
  202. .update('\0', 'utf8')
  203. .update(configString)
  204. .update('\0', 'utf8')
  205. .update(configPath.join(''))
  206. .update('\0', 'utf8')
  207. .update(instrument ? 'instrument' : '')
  208. .update('\0', 'utf8')
  209. .update(process.env.NODE_ENV || '')
  210. .update('\0', 'utf8')
  211. .update(process.env.BABEL_ENV || '')
  212. .digest('hex');
  213. },
  214. process(src, filename, config, transformOptions) {
  215. const babelOptions = {
  216. ...loadBabelConfig(config.cwd, filename, transformOptions).options
  217. };
  218. if (
  219. transformOptions === null || transformOptions === void 0
  220. ? void 0
  221. : transformOptions.instrument
  222. ) {
  223. babelOptions.auxiliaryCommentBefore = ' istanbul ignore next '; // Copied from jest-runtime transform.js
  224. babelOptions.plugins = (babelOptions.plugins || []).concat([
  225. [
  226. babelIstanbulPlugin,
  227. {
  228. // files outside `cwd` will not be instrumented
  229. cwd: config.rootDir,
  230. exclude: []
  231. }
  232. ]
  233. ]);
  234. }
  235. const transformResult = (0, _core().transformSync)(src, babelOptions);
  236. if (transformResult) {
  237. const {code, map} = transformResult;
  238. if (typeof code === 'string') {
  239. return {
  240. code,
  241. map
  242. };
  243. }
  244. }
  245. return src;
  246. }
  247. };
  248. };
  249. const transformer = {
  250. ...createTransformer(),
  251. // Assigned here so only the exported transformer has `createTransformer`,
  252. // instead of all created transformers by the function
  253. createTransformer
  254. };
  255. module.exports = transformer;