logging.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. /* eslint-disable global-require */
  3. /**
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. */
  21. describe('logging', () => {
  22. beforeEach(() => {
  23. jest.resetModules();
  24. jest.resetAllMocks();
  25. });
  26. const { console } = window;
  27. afterAll(() => {
  28. Object.assign(window, { console });
  29. });
  30. it('should pipe to `console` methods', () => {
  31. const { logging } = require('@superset-ui/core');
  32. jest.spyOn(logging, 'debug').mockImplementation();
  33. jest.spyOn(logging, 'log').mockImplementation();
  34. jest.spyOn(logging, 'info').mockImplementation();
  35. expect(() => {
  36. logging.debug();
  37. logging.log();
  38. logging.info();
  39. }).not.toThrow();
  40. jest.spyOn(logging, 'warn').mockImplementation(() => {
  41. throw new Error('warn');
  42. });
  43. expect(() => logging.warn()).toThrow('warn');
  44. jest.spyOn(logging, 'error').mockImplementation(() => {
  45. throw new Error('error');
  46. });
  47. expect(() => logging.error()).toThrow('error');
  48. jest.spyOn(logging, 'trace').mockImplementation(() => {
  49. throw new Error('Trace:');
  50. });
  51. expect(() => logging.trace()).toThrow('Trace:');
  52. });
  53. it('should use noop functions when console unavailable', () => {
  54. Object.assign(window, { console: undefined });
  55. const { logging } = require('@superset-ui/core');
  56. expect(() => {
  57. logging.debug();
  58. logging.log();
  59. logging.info();
  60. logging.warn('warn');
  61. logging.error('error');
  62. logging.trace();
  63. logging.table([
  64. [1, 2],
  65. [3, 4],
  66. ]);
  67. }).not.toThrow();
  68. Object.assign(window, { console });
  69. });
  70. });