Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notifications for test results #1170

Merged
merged 7 commits into from
Jun 16, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Jest uses Jasmine 2 by default. An introduction to Jasmine 2 can be found
- [`collectCoverageOnlyFrom` [object]](#collectcoverageonlyfrom-object)
- [`coverageThreshold` [object]](#coveragethreshold-object)
- [`globals` [object]](#globals-object)
- [`growl` [boolean]](#growl-boolean)
- [`mocksPattern` [string]](#mockspattern-string)
- [`moduleDirectories` [array<string>]](#moduledirectories-array-string)
- [`moduleFileExtensions` [array<string>]](#modulefileextensions-array-string)
Expand Down Expand Up @@ -490,6 +491,11 @@ For example, the following would create a global `__DEV__` variable set to `true

Note that, if you specify a global reference value (like an object or array) here, and some code mutates that value in the midst of running a test, that mutation will *not* be persisted across test runs for other test files.

### `growl` [boolean]
(default: `false`)

Activates Growl notifications for test results.

### `mocksPattern` [string]
(default: `(?:[\\/]|^)__mocks__[\\/]`)

Expand Down
Binary file added packages/jest-cli/src/assets/jest_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ const options = {
),
type: 'boolean',
},
growl: {
description: wrap(
'Activates Growl notifications for test results.'
),
type: 'boolean',
},
watch: {
description: wrap(
'Watch files for changes and rerun tests related to changed files. ' +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"chalk": "^1.1.1",
"growl": "^1.9.2",
"istanbul": "^0.4.2",
"jest-environment-jsdom": "^12.1.0",
"jest-environment-node": "^12.1.0",
Expand Down
98 changes: 98 additions & 0 deletions packages/jest-config/src/Growler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* @flow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we put this below the license. Ignore me if you just copy-pasted from another file - we can do a bulk change later if that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change it.

*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

import type {AggregatedResult} from 'types/TestResult';

const growl = require('growl');
const path = require('path');
const util = require('util');

const isDarwin = process.platform === 'darwin';

const APP_NAME = 'Jest';

const ICON = '/assets/jest_logo.png';

const NOTIFICATIONS = {
success: {
text: (isDarwin ? '✅ ' : '') + '%d tests passed',
options: {
title: '%d%% Passed',
image: ICON,
name: APP_NAME,
sticky: false,
priority: 0,
},
},
failure: {
text: (isDarwin ? '⛔️ ' : '') + '%d of %d tests failed',
options: {
title: '%d%% Failed',
image: ICON,
name: APP_NAME,
sticky: false,
priority: 1,
},
},
};

type Notification = {
text: string,
options: {
title: string,
image: string,
name: string,
sticky: boolean,
priority: ?number;
}
};

class Growler {

static onTestResults(result: AggregatedResult): void {
let info;
let title;
let text;

if (result.success) {
info = NOTIFICATIONS['success'];
title = util.format(info.options.title, 100);
text = util.format(info.text, result.numPassedTests);
} else {
info = NOTIFICATIONS['failure'];
title = util.format(info.options.title, Math.ceil(
(result.numFailedTests / result.numTotalTests) * 100,
));
text = util.format(
info.text,
result.numFailedTests,
result.numTotalTests,
);
}
const image = path.join(__dirname, info.options.image);

const notification = {
text,
options: Object.assign({}, info.options, {title, image}),
};

this.trigger(notification);
}

static trigger(notification: Notification): void {
growl(
notification.text,
notification.options
);
}
}

module.exports = Growler;
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = ({
coverageCollector: require.resolve('./IstanbulCollector'),
coverageReporters: ['json', 'text', 'lcov', 'clover'],
globals: {},
growl: false,
haste: {
providesModuleNodeModules: [],
},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ function normalize(config, argv) {
case 'coverageReporters':
case 'coverageThreshold':
case 'globals':
case 'growl':
case 'haste':
case 'logHeapUsage':
case 'mocksPattern':
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-config/src/reporters/DefaultTestReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {Process} from 'types/Process';

const chalk = require('chalk');
const formatFailureMessage = require('jest-util').formatFailureMessage;
const Growler = require('../Growler');
const path = require('path');
const VerboseLogger = require('./VerboseLogger');

Expand Down Expand Up @@ -183,6 +184,11 @@ class DefaultTestReporter {

this._printSummary(aggregatedResults);
this.log(results);

if (config.growl) {
Growler.onTestResults(aggregatedResults);
}

return snapshotFailure ? false : aggregatedResults.success;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/setFromArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function setFromArgv(config, argv) {
config.verbose = argv.verbose;
}

if (argv.growl) {
config.growl = argv.growl;
}

if (argv.bail) {
config.bail = argv.bail;
}
Expand Down
1 change: 1 addition & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ runCommands('node bin/jest.js --runInBand', 'packages/jest-cli');
runCommands('node bin/jest.js --runInBand --logHeapUsage', 'packages/jest-cli');
runCommands('node bin/jest.js --json', 'packages/jest-cli');
runCommands('node bin/jest.js --verbose', 'packages/jest-cli');
runCommands('node bin/jest.js --growl', 'packages/jest-cli');

examples.forEach(runExampleTests);

Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BaseConfig = {
coverageReporters: Array<string>,
globals: ConfigGlobals,
haste: HasteConfig,
growl: boolean,
mocksPattern: string,
moduleDirectories: Array<string>,
moduleFileExtensions: Array<string>,
Expand Down