|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +import TestScheduler from '../test_scheduler'; |
| 12 | +import NotifyReporter from '../reporters/notify_reporter'; |
| 13 | +import type {TestSchedulerContext} from '../test_scheduler'; |
| 14 | +import type {AggregatedResult} from '../../../../types/TestResult'; |
| 15 | + |
| 16 | +const ICON_PATH = '/assets/jest_logo.png'; |
| 17 | + |
| 18 | +jest.mock('../reporters/default_reporter'); |
| 19 | +jest.mock('node-notifier', () => ({ |
| 20 | + notify: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +const initialContext: TestSchedulerContext = { |
| 24 | + firstRun: true, |
| 25 | + previousSuccess: false, |
| 26 | +}; |
| 27 | + |
| 28 | +const aggregatedResultsSuccess: AggregatedResult = { |
| 29 | + numFailedTestSuites: 0, |
| 30 | + numFailedTests: 0, |
| 31 | + numPassedTestSuites: 1, |
| 32 | + numPassedTests: 3, |
| 33 | + numRuntimeErrorTestSuites: 0, |
| 34 | + numTotalTestSuites: 1, |
| 35 | + numTotalTests: 3, |
| 36 | + success: true, |
| 37 | +}; |
| 38 | + |
| 39 | +const aggregatedResultsFailure: AggregatedResult = { |
| 40 | + numFailedTestSuites: 1, |
| 41 | + numFailedTests: 3, |
| 42 | + numPassedTestSuites: 0, |
| 43 | + numPassedTests: 9, |
| 44 | + numRuntimeErrorTestSuites: 0, |
| 45 | + numTotalTestSuites: 1, |
| 46 | + numTotalTests: 3, |
| 47 | + success: false, |
| 48 | +}; |
| 49 | + |
| 50 | +// Simulated sequence of events for NotifyReporter |
| 51 | +const notifyEvents = [ |
| 52 | + aggregatedResultsSuccess, |
| 53 | + aggregatedResultsFailure, |
| 54 | + aggregatedResultsSuccess, |
| 55 | + aggregatedResultsSuccess, |
| 56 | + aggregatedResultsFailure, |
| 57 | + aggregatedResultsFailure, |
| 58 | +]; |
| 59 | + |
| 60 | +const iconShown = path => path.endsWith(ICON_PATH); |
| 61 | + |
| 62 | +test('.addReporter() .removeReporter()', () => { |
| 63 | + const scheduler = new TestScheduler( |
| 64 | + {}, |
| 65 | + {}, |
| 66 | + Object.assign({}, initialContext), |
| 67 | + ); |
| 68 | + const reporter = new NotifyReporter(); |
| 69 | + scheduler.addReporter(reporter); |
| 70 | + expect(scheduler._dispatcher._reporters).toContain(reporter); |
| 71 | + scheduler.removeReporter(NotifyReporter); |
| 72 | + expect(scheduler._dispatcher._reporters).not.toContain(reporter); |
| 73 | +}); |
| 74 | + |
| 75 | +const testModes = (notifyMode: string, arl: Array<AggregatedResult>) => { |
| 76 | + const notify = require('node-notifier'); |
| 77 | + notify.notify.mock.calls = []; |
| 78 | + |
| 79 | + let previousContext = initialContext; |
| 80 | + arl.forEach((ar, i) => { |
| 81 | + const newContext = Object.assign(previousContext, { |
| 82 | + firstRun: i === 0, |
| 83 | + previousSuccess: previousContext.previousSuccess, |
| 84 | + }); |
| 85 | + const reporter = new NotifyReporter( |
| 86 | + {notify: true, notifyMode}, |
| 87 | + {}, |
| 88 | + newContext, |
| 89 | + ); |
| 90 | + previousContext = newContext; |
| 91 | + reporter.onRunComplete(new Set(), ar); |
| 92 | + }); |
| 93 | + |
| 94 | + expect( |
| 95 | + notify.notify.mock.calls.map(([{icon, message, title}]) => ({ |
| 96 | + icon: iconShown(icon), |
| 97 | + message, |
| 98 | + title, |
| 99 | + })), |
| 100 | + ).toMatchSnapshot(); |
| 101 | +}; |
| 102 | + |
| 103 | +test('test always', () => { |
| 104 | + testModes('always', notifyEvents); |
| 105 | +}); |
| 106 | + |
| 107 | +test('test success', () => { |
| 108 | + testModes('success', notifyEvents); |
| 109 | +}); |
| 110 | + |
| 111 | +test('test change', () => { |
| 112 | + testModes('change', notifyEvents); |
| 113 | +}); |
| 114 | + |
| 115 | +test('test success-change', () => { |
| 116 | + testModes('success-change', notifyEvents); |
| 117 | +}); |
| 118 | + |
| 119 | +test('test failure-change', () => { |
| 120 | + testModes('failure-change', notifyEvents); |
| 121 | +}); |
0 commit comments