enhanceUnexpectedTokenMessage.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = handlePotentialSyntaxError;
  6. exports.enhanceUnexpectedTokenMessage = enhanceUnexpectedTokenMessage;
  7. function _chalk() {
  8. const data = _interopRequireDefault(require('chalk'));
  9. _chalk = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. /**
  18. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  19. *
  20. * This source code is licensed under the MIT license found in the
  21. * LICENSE file in the root directory of this source tree.
  22. */
  23. const DOT = ' \u2022 ';
  24. function handlePotentialSyntaxError(e) {
  25. if (e.codeFrame) {
  26. e.stack = e.message + '\n' + e.codeFrame;
  27. }
  28. if (
  29. // `instanceof` might come from the wrong context
  30. e.name === 'SyntaxError' &&
  31. (e.message.includes('Unexpected token') ||
  32. e.message.includes('Cannot use import')) &&
  33. !e.message.includes(' expected')
  34. ) {
  35. throw enhanceUnexpectedTokenMessage(e);
  36. }
  37. return e;
  38. }
  39. function enhanceUnexpectedTokenMessage(e) {
  40. e.stack =
  41. `${_chalk().default.bold.red('Jest encountered an unexpected token')}
  42. This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
  43. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
  44. Here's what you can do:
  45. ${DOT}If you are trying to use ECMAScript Modules, see ${_chalk().default.underline(
  46. 'https://jestjs.io/docs/en/ecmascript-modules'
  47. )} for how to enable it.
  48. ${DOT}To have some of your "node_modules" files transformed, you can specify a custom ${_chalk().default.bold(
  49. '"transformIgnorePatterns"'
  50. )} in your config.
  51. ${DOT}If you need a custom transformation specify a ${_chalk().default.bold(
  52. '"transform"'
  53. )} option in your config.
  54. ${DOT}If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the ${_chalk().default.bold(
  55. '"moduleNameMapper"'
  56. )} config option.
  57. You'll find more details and examples of these config options in the docs:
  58. ${_chalk().default.cyan('https://jestjs.io/docs/en/configuration.html')}
  59. ${_chalk().default.bold.red('Details:')}
  60. ` + e.stack;
  61. return e;
  62. }