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