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 testLocationInResults option #4782

Merged
merged 6 commits into from
Nov 4, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* `[jest-runtime]` Support sourcemaps in transformers ([#3458](https://github.com/facebook/jest/pull/3458))
* `[jest-snapshot]` Add a serializer for `jest.fn` to allow a snapshot of a jest mock ([#4668](https://github.com/facebook/jest/pull/4668))
* `[jest-worker]` Initial version of parallel worker abstraction, say hello! ([#4497](https://github.com/facebook/jest/pull/4497))
* `[jest-jasmine2]` Add `testLocationInResults` flag to add location information per spec to test results ([#4782](https://github.com/facebook/jest/pull/4782))

### Chore & Maintenance
* `[*]` [**BREAKING**] Drop support for Node.js version 4
Expand Down
13 changes: 13 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ Prevent tests from printing messages through the console.

Alias: `-t`. Run only tests and test suites with a name that matches the regex. For example, suppose you want to run only tests related to authorization which will have names like `"GET /api/posts with auth"`, then you can use `jest -t=auth`.

### `--testLocationInResults`
Copy link
Member Author

@SimenB SimenB Oct 31, 2017

Choose a reason for hiding this comment

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


Adds a `location` field to test results. Useful if you want to report the location of a test in a reporter.

Note that `column` is 0-indexed while `line` is not.

```json
{
"column": 4,
"line": 5
}
```

### `--testPathPattern=<regex>`

A regexp pattern string that is matched against all tests paths before executing the test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"setupFiles\\": [],
\\"snapshotSerializers\\": [],
\\"testEnvironment\\": \\"jest-environment-jsdom\\",
\\"testLocationInResults\\": false,
\\"testMatch\\": [
\\"**/__tests__/**/*.js?(x)\\",
\\"**/?(*.)(spec|test).js?(x)\\"
Expand Down
33 changes: 33 additions & 0 deletions integration_tests/__tests__/location_in_results.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

it('defaults to null for location', () => {
const result = runJest.json('location_in_results').json;

const assertions = result.testResults[0].assertionResults;
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(2);
expect(assertions[0].location).toBeNull();
expect(assertions[1].location).toBeNull();
});

it('adds correct location info when provided with flag', () => {
const result = runJest.json('location_in_results', [
'--testLocationInResults',
]).json;

const assertions = result.testResults[0].assertionResults;
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(2);
expect(assertions[0].location).toEqual({column: 1, line: 9});
expect(assertions[1].location).toEqual({column: 3, line: 14});
});
17 changes: 17 additions & 0 deletions integration_tests/location_in_results/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

it('no ancestors', () => {
expect(true).toBeTruthy();
});

describe('nested', () => {
it('also works', () => {
expect(true).toBeTruthy();
});
});
5 changes: 5 additions & 0 deletions integration_tests/location_in_results/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ export const options = {
description: 'Exit code of `jest` command if the test run failed',
type: 'string', // number
},
testLocationInResults: {
default: false,
description: 'Add `location` information to the test results',
type: 'boolean',
},
testMatch: {
description: 'The glob patterns Jest uses to detect test files.',
type: 'array',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default ({
snapshotSerializers: [],
testEnvironment: 'jest-environment-jsdom',
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: '',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const getConfigs = (
skipNodeResolution: options.skipNodeResolution,
snapshotSerializers: options.snapshotSerializers,
testEnvironment: options.testEnvironment,
testLocationInResults: options.testLocationInResults,
testMatch: options.testMatch,
testPathIgnorePatterns: options.testPathIgnorePatterns,
testRegex: options.testRegex,
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 @@ -480,6 +480,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'skipNodeResolution':
case 'testEnvironment':
case 'testFailureExitCode':
case 'testLocationInResults':
case 'testNamePattern':
case 'testRegex':
case 'testURL':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/valid_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default ({
snapshotSerializers: ['my-serializer-module'],
testEnvironment: 'jest-environment-jsdom',
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],
testNamePattern: 'test signature',
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"callsites": "^2.0.0",
"chalk": "^2.0.1",
"expect": "^21.2.1",
"graceful-fs": "^4.1.11",
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type Runtime from 'jest-runtime';

import path from 'path';
import fs from 'fs';
import callsites from 'callsites';
import JasmineReporter from './reporter';
import {install as jasmineAsyncInstall} from './jasmine_async';

Expand Down Expand Up @@ -44,6 +45,18 @@ async function jasmine2(
Object.assign(environment.global, jasmineInterface);
env.addReporter(jasmineInterface.jsApiReporter);

if (config.testLocationInResults === true) {
const originalIt = environment.global.it;
environment.global.it = (...args) => {
const stack = callsites()[1];
Copy link
Member Author

@SimenB SimenB Oct 28, 2017

Choose a reason for hiding this comment

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

not sure about hardcoding the index. Thoughts? Can traverse all and return the first one which is not inside node_modules instead, perhaps?

const it = originalIt(...args);

it.result.__callsite = stack;

return it;
};
}

jasmineAsyncInstall(environment.global);

environment.global.test = environment.global.it;
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-jasmine2/src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Suite = {
};

type SpecResult = {
__callsite?: Object,
description: string,
duration?: Milliseconds,
failedExpectations: Array<FailedAssertion>,
Expand Down Expand Up @@ -150,11 +151,19 @@ export default class Jasmine2Reporter {
const duration = start ? Date.now() - start : undefined;
const status =
specResult.status === 'disabled' ? 'pending' : specResult.status;
const location = specResult.__callsite
? {
column: specResult.__callsite.getColumnNumber(),
// $FlowFixMe: https://github.com/facebook/flow/issues/5213
line: specResult.__callsite.getLineNumber(),
}
: null;
const results = {
ancestorTitles,
duration,
failureMessages: [],
fullName: specResult.fullName,
location,
numPassingAsserts: 0, // Jasmine2 only returns an array of failed asserts.
status,
title: specResult.description,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-util/src/format_test_results.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function formatTestAssertion(
ancestorTitles: assertion.ancestorTitles,
failureMessages: null,
fullName: assertion.fullName,
location: assertion.location,
status: assertion.status,
title: assertion.title,
};
Expand Down
1 change: 1 addition & 0 deletions test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
skipNodeResolution: false,
snapshotSerializers: [],
testEnvironment: 'node',
testLocationInResults: false,
testMatch: [],
testPathIgnorePatterns: [],
testRegex: '.test.js$',
Expand Down
3 changes: 3 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type DefaultOptions = {|
snapshotSerializers: Array<Path>,
testEnvironment: string,
testFailureExitCode: string | number,
testLocationInResults: boolean,
testMatch: Array<Glob>,
testPathIgnorePatterns: Array<string>,
testRegex: string,
Expand Down Expand Up @@ -119,6 +120,7 @@ export type InitialOptions = {
snapshotSerializers?: Array<Path>,
testEnvironment?: string,
testFailureExitCode?: string | number,
testLocationInResults?: boolean,
testMatch?: Array<Glob>,
testNamePattern?: string,
testPathDirs?: Array<Path>,
Expand Down Expand Up @@ -215,6 +217,7 @@ export type ProjectConfig = {|
snapshotSerializers: Array<Path>,
testEnvironment: string,
testMatch: Array<Glob>,
testLocationInResults: boolean,
testPathIgnorePatterns: Array<string>,
testRegex: string,
testRunner: string,
Expand Down
7 changes: 7 additions & 0 deletions types/TestResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ export type Status = 'passed' | 'failed' | 'skipped' | 'pending';
export type Bytes = number;
export type Milliseconds = number;

type Callsite = {|
column: number,
line: number,
|}

export type AssertionResult = {|
ancestorTitles: Array<string>,
duration?: ?Milliseconds,
failureMessages: Array<string>,
fullName: string,
location: ?Callsite,
numPassingAsserts: number,
status: Status,
title: string,
Expand All @@ -96,6 +102,7 @@ export type AssertionResult = {|
export type FormattedAssertionResult = {
failureMessages: Array<string> | null,
fullName: string,
location: ?Callsite,
status: Status,
title: string,
};
Expand Down