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

Improve chai support (with detailed output, to match jest exceptions) #8444

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
04c7b47
expand the assersion type error checking
rpgeeganage May 9, 2019
a68725d
fix the linting in the modified files
rpgeeganage May 9, 2019
cd84d63
added same fix for the circus
rpgeeganage May 9, 2019
da41f0a
updated the CHANGELOG.md
rpgeeganage May 9, 2019
59ab223
remove an accident commit on unwanted file
rpgeeganage May 9, 2019
15e2cc6
chore(jest-config): util types (#8437)
JoshRosenstein May 11, 2019
f0a6fa7
added test
rpgeeganage May 11, 2019
c4140d5
added tests
rpgeeganage May 11, 2019
b43f85a
fixed linting
rpgeeganage May 11, 2019
7f22d1e
update the snapshot
rpgeeganage May 11, 2019
9da7213
expand the assersion type error checking
rpgeeganage May 9, 2019
919848f
fix the linting in the modified files
rpgeeganage May 9, 2019
3301eab
added same fix for the circus
rpgeeganage May 9, 2019
51981ef
updated the CHANGELOG.md
rpgeeganage May 9, 2019
008aaf1
remove an accident commit on unwanted file
rpgeeganage May 9, 2019
824f96c
added test
rpgeeganage May 11, 2019
31dc2ce
added tests
rpgeeganage May 11, 2019
eaea884
fixed linting
rpgeeganage May 11, 2019
60d2847
update the snapshot
rpgeeganage May 11, 2019
76808f1
Merge branch 'improve_chai_output' of https://github.com/rpgeeganage/…
rpgeeganage May 11, 2019
3a3184e
expand the assersion type error checking
rpgeeganage May 9, 2019
ce74f6c
fix the linting in the modified files
rpgeeganage May 9, 2019
0396d93
added same fix for the circus
rpgeeganage May 9, 2019
8d430b6
updated the CHANGELOG.md
rpgeeganage May 9, 2019
fad812b
remove an accident commit on unwanted file
rpgeeganage May 9, 2019
711ff3d
added test
rpgeeganage May 11, 2019
246174c
added tests
rpgeeganage May 11, 2019
b3ca01c
fixed linting
rpgeeganage May 11, 2019
8b922cf
update the snapshot
rpgeeganage May 11, 2019
8a2cf42
expand the assersion type error checking
rpgeeganage May 9, 2019
b3b4c4d
fix the linting in the modified files
rpgeeganage May 9, 2019
d25ef62
updated the CHANGELOG.md
rpgeeganage May 9, 2019
da811e9
remove an accident commit on unwanted file
rpgeeganage May 9, 2019
c535263
added tests
rpgeeganage May 11, 2019
5610557
update the snapshot
rpgeeganage May 11, 2019
8533e19
Merge branch 'improve_chai_output' of https://github.com/rpgeeganage/…
rpgeeganage May 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@
- `[expect]` Improve report when matcher fails, part 17 ([#8349](https://github.com/facebook/jest/pull/8349))
- `[expect]` Improve report when matcher fails, part 18 ([#8356](https://github.com/facebook/jest/pull/8356))
- `[expect]` Improve report when matcher fails, part 19 ([#8367](https://github.com/facebook/jest/pull/8367))
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8444](https://github.com/facebook/jest/pull/8444))

### Fixes

Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap
@@ -0,0 +1 @@
test
20 changes: 20 additions & 0 deletions e2e/__tests__/chaiAssertionLibrary.ts
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

import path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import runJest from '../runJest';
import {extractSummary, run} from '../Utils';

test('chai assertion errors should display properly', () => {
const dir = path.resolve(__dirname, '../chai-assertion-library-errors');
run('yarn', dir);

const {stderr} = runJest('chai-assertion-library-errors');
const {rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
});
26 changes: 26 additions & 0 deletions e2e/chai-assertion-library-errors/__tests__/chai_assertion.js
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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';
const chai = require('chai');

describe('chai.js assertion library test', () => {
it('expect', () => {
chai.expect('hello world').to.equal('hello sunshine');
});

it('should', () => {
chai.should();
const stringExpected = 'hello world';
const actualExpected = 'hello sunshine';
stringExpected.should.to.equal(actualExpected);
});

it('assert', () => {
chai.assert.strictEqual('hello world', 'hello sunshine');
});
});
9 changes: 9 additions & 0 deletions e2e/chai-assertion-library-errors/package.json
@@ -0,0 +1,9 @@
{
"jest": {
"testEnvironment": "node",
"verbose": false
},
"dependencies": {
"chai": "^4.2.0"
}
}
47 changes: 47 additions & 0 deletions e2e/chai-assertion-library-errors/yarn.lock
@@ -0,0 +1,47 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


assertion-error@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==

chai@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
dependencies:
assertion-error "^1.1.0"
check-error "^1.0.2"
deep-eql "^3.0.1"
get-func-name "^2.0.0"
pathval "^1.1.0"
type-detect "^4.0.5"

check-error@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=

deep-eql@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
integrity sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==
dependencies:
type-detect "^4.0.0"

get-func-name@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=

pathval@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA=

type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
14 changes: 11 additions & 3 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -7,13 +7,13 @@

import {AssertionError} from 'assert';
import {Circus} from '@jest/types';
import chalk from 'chalk';
import {
diff,
DiffOptions,
printExpected,
printReceived,
DiffOptions,
} from 'jest-matcher-utils';
import chalk from 'chalk';
import prettyFormat from 'pretty-format';

interface AssertionErrorWithStack extends AssertionError {
Expand Down Expand Up @@ -60,7 +60,7 @@ const formatNodeAssertErrors = (event: Circus.Event, state: Circus.State) => {
} else {
error = errors;
}
return error.code === 'ERR_ASSERTION'
return isAssertionError(error)
? {message: assertionErrorMessage(error, {expand: state.expand})}
: errors;
});
Expand Down Expand Up @@ -168,4 +168,12 @@ function assertionErrorMessage(
);
}

function isAssertionError(error: Circus.TestError) {
return (
error instanceof AssertionError ||
error.name === AssertionError.name ||
error.code === 'ERR_ASSERTION'
);
}

export default formatNodeAssertErrors;
21 changes: 13 additions & 8 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -659,14 +659,19 @@ export default function normalize(
? _replaceRootDirTags(options.rootDir, project)
: project,
)
.reduce((projects, project) => {
// Project can be specified as globs. If a glob matches any files,
// We expand it to these paths. If not, we keep the original path
// for the future resolution.
const globMatches =
typeof project === 'string' ? glob.sync(project) : [];
return projects.concat(globMatches.length ? globMatches : project);
}, []);
.reduce(
(projects, project) => {
// Project can be specified as globs. If a glob matches any files,
// We expand it to these paths. If not, we keep the original path
// for the future resolution.
const globMatches =
typeof project === 'string' ? glob.sync(project) : [];
return projects.concat(
globMatches.length ? globMatches : project,
);
},
[] as Array<string>,
);
break;
case 'moduleDirectories':
case 'testMatch':
Expand Down
55 changes: 34 additions & 21 deletions packages/jest-config/src/utils.ts
Expand Up @@ -48,8 +48,8 @@ export const resolve = (
${chalk.bold('<rootDir>')} is: ${rootDir}`,
);
}

return module;
/// can cast as string since nulls will be thrown
return module as string;
};

export const escapeGlobCharacters = (path: Config.Path): Config.Glob =>
Expand All @@ -69,37 +69,50 @@ export const replaceRootDirInPath = (
);
};

// TODO: Type as returning same type as input
const _replaceRootDirInObject = (
const _replaceRootDirInObject = <T extends ReplaceRootDirConfigObj>(
rootDir: Config.Path,
config: any,
): {[key: string]: unknown} => {
if (config !== null) {
const newConfig: {[key: string]: unknown} = {};
for (const configKey in config) {
newConfig[configKey] =
configKey === 'rootDir'
? config[configKey]
: _replaceRootDirTags(rootDir, config[configKey]);
}
return newConfig;
config: T,
): T => {
const newConfig = {} as T;
for (const configKey in config) {
newConfig[configKey] =
configKey === 'rootDir'
? config[configKey]
: _replaceRootDirTags(rootDir, config[configKey]);
}
return config;
return newConfig;
};

// TODO: Type as returning same type as input
export const _replaceRootDirTags = (rootDir: Config.Path, config: any): any => {
type OrArray<T> = T | Array<T>;
type ReplaceRootDirConfigObj = {[key: string]: Config.Path};
type ReplaceRootDirConfigValues =
| OrArray<ReplaceRootDirConfigObj>
| OrArray<RegExp>
| OrArray<Config.Path>;

export const _replaceRootDirTags = <T extends ReplaceRootDirConfigValues>(
rootDir: Config.Path,
config: T,
): T => {
if (config == null) {
return config;
}
switch (typeof config) {
case 'object':
if (Array.isArray(config)) {
return config.map(item => _replaceRootDirTags(rootDir, item));
/// can be string[] or {}[]
return config.map(item => _replaceRootDirTags(rootDir, item)) as T;
}
if (config instanceof RegExp) {
return config;
}
return _replaceRootDirInObject(rootDir, config);

return _replaceRootDirInObject(
rootDir,
config as ReplaceRootDirConfigObj,
) as T;
case 'string':
return replaceRootDirInPath(rootDir, config);
return replaceRootDirInPath(rootDir, config) as T;
}
return config;
};
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Env.ts
Expand Up @@ -663,7 +663,10 @@ export default function(j$: Jasmine) {
let checkIsError;
let message;

if (error instanceof AssertionError) {
if (
error instanceof AssertionError ||
error.name === AssertionError.name
) {
checkIsError = false;
// @ts-ignore TODO Possible error: j$.Spec does not have expand property
message = assertionErrorMessage(error, {expand: j$.Spec.expand});
Expand Down
13 changes: 9 additions & 4 deletions packages/jest-jasmine2/src/jasmine/Spec.ts
Expand Up @@ -236,10 +236,9 @@ export default class Spec {
passed: false,
expected: '',
actual: '',
error:
error instanceof AssertionError
? assertionErrorMessage(error, {expand: this.expand})
: error,
error: this.isAssertionError(error)
? assertionErrorMessage(error, {expand: this.expand})
: error,
},
true,
);
Expand Down Expand Up @@ -292,6 +291,12 @@ export default class Spec {
getFullName() {
return this.getSpecName(this);
}

isAssertionError(error: Error) {
return (
error instanceof AssertionError || error.name === AssertionError.name
);
}
}

Spec.pendingSpecExceptionMessage = '=> marked Pending';
Expand Down