config.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.configure = configure;
  6. exports.getConfig = getConfig;
  7. exports.runWithExpensiveErrorDiagnosticsDisabled = runWithExpensiveErrorDiagnosticsDisabled;
  8. var _prettyDom = require("./pretty-dom");
  9. // It would be cleaner for this to live inside './queries', but
  10. // other parts of the code assume that all exports from
  11. // './queries' are query functions.
  12. let config = {
  13. testIdAttribute: 'data-testid',
  14. asyncUtilTimeout: 1000,
  15. // asyncWrapper and advanceTimersWrapper is to support React's async `act` function.
  16. // forcing react-testing-library to wrap all async functions would've been
  17. // a total nightmare (consider wrapping every findBy* query and then also
  18. // updating `within` so those would be wrapped too. Total nightmare).
  19. // so we have this config option that's really only intended for
  20. // react-testing-library to use. For that reason, this feature will remain
  21. // undocumented.
  22. asyncWrapper: cb => cb(),
  23. unstable_advanceTimersWrapper: cb => cb(),
  24. eventWrapper: cb => cb(),
  25. // default value for the `hidden` option in `ByRole` queries
  26. defaultHidden: false,
  27. // default value for the `ignore` option in `ByText` queries
  28. defaultIgnore: 'script, style',
  29. // showOriginalStackTrace flag to show the full error stack traces for async errors
  30. showOriginalStackTrace: false,
  31. // throw errors w/ suggestions for better queries. Opt in so off by default.
  32. throwSuggestions: false,
  33. // called when getBy* queries fail. (message, container) => Error
  34. getElementError(message, container) {
  35. const prettifiedDOM = (0, _prettyDom.prettyDOM)(container);
  36. const error = new Error([message, `Ignored nodes: comments, ${config.defaultIgnore}\n${prettifiedDOM}`].filter(Boolean).join('\n\n'));
  37. error.name = 'TestingLibraryElementError';
  38. return error;
  39. },
  40. _disableExpensiveErrorDiagnostics: false,
  41. computedStyleSupportsPseudoElements: false
  42. };
  43. function runWithExpensiveErrorDiagnosticsDisabled(callback) {
  44. try {
  45. config._disableExpensiveErrorDiagnostics = true;
  46. return callback();
  47. } finally {
  48. config._disableExpensiveErrorDiagnostics = false;
  49. }
  50. }
  51. function configure(newConfig) {
  52. if (typeof newConfig === 'function') {
  53. // Pass the existing config out to the provided function
  54. // and accept a delta in return
  55. newConfig = newConfig(config);
  56. }
  57. // Merge the incoming config delta
  58. config = {
  59. ...config,
  60. ...newConfig
  61. };
  62. }
  63. function getConfig() {
  64. return config;
  65. }