Skip to content

Add source tests #7

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

Merged
merged 5 commits into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions lib/Assertion.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface AssertionStatic {
(target: any, message?: string): Assertion;
(target?: any, message?: string, stack?: Function): Assertion;
}

export interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
Expand Down Expand Up @@ -37,9 +37,9 @@ export interface Assertion extends LanguageChains, NumericComparison, TypeCompar
lengthOf: Length;
match: Match;
matches: Match;
string(string: string, message?: string): Assertion;
string(str: string, message?: string): Assertion;
keys: Keys;
key(string: string): Assertion;
key(str: string): Assertion;
throw: Throw;
throws: Throw;
Throw: Throw;
Expand Down Expand Up @@ -113,6 +113,7 @@ export interface CloseTo {

export interface Deep {
equal: Equal;
equals: Equal;
include: Include;
property: Property;
members: Members;
Expand Down Expand Up @@ -147,6 +148,7 @@ export interface Include {
(value: Object, message?: string): Assertion;
(value: string, message?: string): Assertion;
(value: number, message?: string): Assertion;
string(value: string, message?: string): Assertion;
keys: Keys;
members: Members;
any: KeyFilter;
Expand All @@ -158,7 +160,7 @@ export interface Match {
}

export interface Keys {
(...keys: string[]): Assertion;
(...keys: any[]): Assertion;
(keys: any[]): Assertion;
(keys: Object): Assertion;
}
Expand Down Expand Up @@ -187,4 +189,4 @@ export interface Members {

export interface PropertyChange {
(object: Object, prop: string, msg?: string): Assertion;
}
}
9 changes: 5 additions & 4 deletions lib/Chai.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import * as AE from 'assertion-error';

import * as Assert from './Assert';
import * as Assertion from './Assertion';
import * as A from './Assertion';
import * as Expect from './Expect';
import * as Should from './Should';
import * as Config from './Config';
import * as Utils from './Utils';

declare namespace chai {
export interface AssertionStatic extends Assertion.AssertionStatic {}
export interface AssertionStatic extends A.AssertionStatic {}
export class AssertionError extends AE.AssertionError {}
export var Assertion: Assertion.Assertion;
export var Assertion: A.AssertionStatic;
export var expect: Expect.ExpectStatic;
export var assert: Assert.AssertStatic;
export var config: Config.Config;
export var util: Utils.Utils;
export function should(): Should.Should;
export function Should(): Should.Should;
/**
* Provides a way to extend the internals of Chai
*/
Expand All @@ -26,6 +27,6 @@ export = chai;

declare global {
interface Object {
should: Assertion.Assertion;
should: A.Assertion;
}
}
1 change: 1 addition & 0 deletions lib/Config.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Config {
includeStack: boolean;
truncateThreshold: number;
}
5 changes: 5 additions & 0 deletions lib/Utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Assertion} from './Assertion';

export interface Utils {
addChainableMethod(ctx: any, name: string, chainingBehavior: (value: any) => void);
addMethod(ctx: any, name: string, method: (value: any) => void);
Expand All @@ -6,10 +8,13 @@ export interface Utils {
flag(obj: Object, key: string, value?: any);
getActual(obj: Object, actual?: any);
getEnumerableProperties(obj: Object);
getMessage(obj: Object, params: any[]);
getMessage(obj: Object, message: string, negateMessage: string);
getName(func: Function);
getPathInfo(path: string, obj: Object);
getPathValue(path: string, obj: Object);
getProperties(obj: Object);
hasProperty(obj: Object, name: string);
transferFlags(assertion: Assertion | any, obj: Object, includeAll?: boolean);
inspect(obj: any);
}
172 changes: 172 additions & 0 deletions source-test/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import err from './bootstrap/index';
import chai = require('chai');

describe('configuration', function () {
var assert = chai.assert;

var origConfig;

beforeEach(function() {
// backup current config
function clone(o) {
return JSON.parse(JSON.stringify(o));
}
origConfig = clone(chai.config);
});

afterEach(function() {
// restore config
Object.keys(origConfig).forEach(function(key) {
chai.config[key] = origConfig[key];
});
});

function fooThrows () {
chai.expect('foo').to.be.equal('bar');
}
function fooPropThrows () {
chai.expect('foo').to.not.exist;
}

it('includeStack is true for method assertions', function () {
chai.config.includeStack = true;

try {
fooThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// not all browsers support err.stack
if ('undefined' !== typeof err.stack) {
assert.include(err.stack, 'assertEqual', 'should have internal stack trace in error message');
assert.include(err.stack, 'fooThrows', 'should have user stack trace in error message');
}
}

});

it('includeStack is false for method assertions', function () {
chai.config.includeStack = false;

try {
fooThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// IE 10 supports err.stack in Chrome format, but without
// `Error.captureStackTrace` support that allows tuning of the error
// message.
// if ('undefined' !== typeof err.stack && 'undefined' !== typeof Error.captureStackTrace) {
// assert.notInclude(err.stack, 'assertEqual', 'should not have internal stack trace in error message');
// assert.include(err.stack, 'fooThrows', 'should have user stack trace in error message');
// }
}
});

it('includeStack is true for property assertions', function () {
chai.config.includeStack = true;

try {
fooPropThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// not all browsers support err.stack
// Phantom does not include function names for getter exec
// if ('undefined' !== typeof err.stack && 'undefined' !== typeof Error.captureStackTrace) {
// assert.include(err.stack, 'addProperty', 'should have internal stack trace in error message');
// assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
// }
}

});

it('includeStack is false for property assertions', function () {
chai.config.includeStack = false;

try {
fooPropThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// IE 10 supports err.stack in Chrome format, but without
// `Error.captureStackTrace` support that allows tuning of the error
// message.
// if ('undefined' !== typeof err.stack && 'undefined' !== typeof Error.captureStackTrace) {
// assert.notInclude(err.stack, 'addProperty', 'should not have internal stack trace in error message');
// assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
// }
}
});

describe('truncateThreshold', function() {
it('is 20', function() {
chai.config.truncateThreshold = 20;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { Object (v) } to deeply equal { v: 'x' }");
});

it('is 0', function() {
chai.config.truncateThreshold = 0;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { v: 'something longer than 20' } to deeply equal { v: 'x' }");
});
});

describe('deprecated properties', function() {
var origWarnFn;
var warnings;

beforeEach(function() {
origWarnFn = console.warn;
warnings = [];
console.warn = function(message) {
warnings.push(message);
};
});

afterEach(function() {
console.warn = origWarnFn;
});

// it('Assertion.includeStack warns that it is deprecated', function() {
// chai.Assertion.includeStack;

// assert.equal(warnings.length, 1);
// assert.equal(warnings[0], 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.');

// chai.Assertion.includeStack = true;

// assert.equal(warnings.length, 2);
// assert.equal(warnings[1], 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
// });

// it('Assertion.includeStack is kept in sync with config.includeStack', function() {
// assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
// chai.Assertion.includeStack = !chai.Assertion.includeStack;
// assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
// chai.config.includeStack = !chai.config.includeStack;
// assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
// });

// it('Assertion.showDiff warns that it is deprecated', function() {
// chai.Assertion.showDiff;

// assert.equal(warnings.length, 1);
// assert.equal(warnings[0], 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.');

// chai.Assertion.showDiff = true;

// assert.equal(warnings.length, 2);
// assert.equal(warnings[1], 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
// });

// it('Assertion.showDiff is kept in sync with config.showDiff', function() {
// assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
// chai.Assertion.showDiff = !chai.Assertion.showDiff;
// assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
// chai.config.showDiff = !chai.config.showDiff;
// assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
// });
});
});
Loading