Skip to content
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
10 changes: 4 additions & 6 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,29 @@ function wrapAssertions(callbacks) {
},

is(actual, expected, message) {
if (actual === expected) {
if (Object.is(actual, expected)) {
pass(this);
} else {
const diff = formatAssertError.formatDiff(actual, expected);
const values = diff ? [diff] : [
formatAssertError.formatWithLabel('Actual:', actual),
formatAssertError.formatWithLabel('Must be strictly equal to:', expected)
formatAssertError.formatWithLabel('Must be the same as:', expected)
];

fail(this, new AssertionError({
assertion: 'is',
message,
operator: '===',
values
}));
}
},

not(actual, expected, message) {
if (actual === expected) {
if (Object.is(actual, expected)) {
fail(this, new AssertionError({
assertion: 'not',
message,
operator: '!==',
values: [formatAssertError.formatWithLabel('Value is strictly equal:', actual)]
values: [formatAssertError.formatWithLabel('Value is the same as:', actual)]
}));
} else {
pass(this);
Expand Down
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ function macro(t, input, expected) {
t.is(eval(input), expected);
}

test('2 + 2 === 4', macro, '2 + 2', 4);
test('2 * 3 === 6', macro, '2 * 3', 6);
test('2 + 2 = 4', macro, '2 + 2', 4);
test('2 * 3 = 6', macro, '2 * 3', 6);
```

You can build the test title programmatically by attaching a `title` function to the macro:
Expand All @@ -643,7 +643,7 @@ function macro(t, input, expected) {
t.is(eval(input), expected);
}

macro.title = (providedTitle, input, expected) => `${providedTitle} ${input} === ${expected}`.trim();
macro.title = (providedTitle, input, expected) => `${providedTitle} ${input} = ${expected}`.trim();

test(macro, '2 + 2', 4);
test(macro, '2 * 3', 6);
Expand Down Expand Up @@ -914,23 +914,23 @@ Assert that `value` is `false`.

### `.is(value, expected, [message])`

Assert that `value` is equal to `expected`.
Assert that `value` is the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).

### `.not(value, expected, [message])`

Assert that `value` is not equal to `expected`.
Assert that `value` is not the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).

### `.deepEqual(value, expected, [message])`

Assert that `value` is deep equal to `expected`. This is based on [Lodash' `isEqual()`](https://lodash.com/docs/4.17.4#isEqual):
Assert that `value` is deeply equal to `expected`. This is based on [Lodash's `isEqual()`](https://lodash.com/docs/4.17.4#isEqual):

> Performs a deep comparison between two values to determine if they are equivalent.
>
> *Note*: This method supports comparing arrays, array buffers, booleans, date objects, error objects, maps, numbers, `Object` objects, regexes, sets, strings, symbols, and typed arrays. `Object` objects are compared by their own, not inherited, enumerable properties. Functions and DOM nodes are compared by strict equality, i.e. `===`.

### `.notDeepEqual(value, expected, [message])`

Assert that `value` is not deep equal to `expected`. The inverse of `.deepEqual()`.
Assert that `value` is not deeply equal to `expected`. The inverse of `.deepEqual()`.

### `.throws(function|promise, [error, [message]])`

Expand Down
150 changes: 141 additions & 9 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,118 @@ test('.is()', t => {
assertions.is('foo', 'foo');
});

passes(t, () => {
assertions.is('', '');
});

passes(t, () => {
assertions.is(true, true);
});

passes(t, () => {
assertions.is(false, false);
});

passes(t, () => {
assertions.is(null, null);
});

passes(t, () => {
assertions.is(undefined, undefined);
});

passes(t, () => {
assertions.is(1, 1);
});

passes(t, () => {
assertions.is(0, 0);
});

passes(t, () => {
assertions.is(-0, -0);
});

passes(t, () => {
assertions.is(NaN, NaN);
});

passes(t, () => {
assertions.is(0 / 0, NaN);
});

passes(t, () => {
const someRef = {foo: 'bar'};
assertions.is(someRef, someRef);
});

fails(t, () => {
assertions.is(0, -0);
});

fails(t, () => {
assertions.is(0, false);
});

fails(t, () => {
assertions.is('', false);
});

fails(t, () => {
assertions.is('0', 0);
});

fails(t, () => {
assertions.is('17', 17);
});

fails(t, () => {
assertions.is([1, 2], '1,2');
});

fails(t, () => {
// eslint-disable-next-line no-new-wrappers
assertions.is(new String('foo'), 'foo');
});

fails(t, () => {
assertions.is(null, undefined);
});

fails(t, () => {
assertions.is(null, false);
});

fails(t, () => {
assertions.is(undefined, false);
});

fails(t, () => {
assertions.is({foo: 'bar'}, {foo: 'bar'});
});

fails(t, () => {
// eslint-disable-next-line no-new-wrappers
assertions.is(new String('foo'), new String('foo'));
});

fails(t, () => {
assertions.is(0, null);
});

fails(t, () => {
assertions.is(0, NaN);
});

fails(t, () => {
assertions.is('foo', NaN);
});

failsWith(t, () => {
assertions.is('foo', 'bar');
}, {
assertion: 'is',
message: '',
operator: '===',
values: [
{label: 'Difference:', formatted: /foobar/}
]
Expand All @@ -118,10 +224,9 @@ test('.is()', t => {
}, {
assertion: 'is',
message: '',
operator: '===',
values: [
{label: 'Actual:', formatted: /foo/},
{label: 'Must be strictly equal to:', formatted: /42/}
{label: 'Must be the same as:', formatted: /42/}
]
});

Expand All @@ -130,10 +235,31 @@ test('.is()', t => {
}, {
assertion: 'is',
message: 'my message',
operator: '===',
values: [
{label: 'Actual:', formatted: /foo/},
{label: 'Must be strictly equal to:', formatted: /42/}
{label: 'Must be the same as:', formatted: /42/}
]
});

failsWith(t, () => {
assertions.is(0, -0, 'my message');
}, {
assertion: 'is',
message: 'my message',
values: [
{label: 'Actual:', formatted: /0/},
{label: 'Must be the same as:', formatted: /-0/}
]
});

failsWith(t, () => {
assertions.is(-0, 0, 'my message');
}, {
assertion: 'is',
message: 'my message',
values: [
{label: 'Actual:', formatted: /-0/},
{label: 'Must be the same as:', formatted: /0/}
]
});

Expand All @@ -145,22 +271,28 @@ test('.not()', t => {
assertions.not('foo', 'bar');
});

fails(t, () => {
assertions.not(NaN, NaN);
});

fails(t, () => {
assertions.not(0 / 0, NaN);
});

failsWith(t, () => {
assertions.not('foo', 'foo');
}, {
assertion: 'not',
message: '',
operator: '!==',
values: [{label: 'Value is strictly equal:', formatted: /foo/}]
values: [{label: 'Value is the same as:', formatted: /foo/}]
});

failsWith(t, () => {
assertions.not('foo', 'foo', 'my message');
}, {
assertion: 'not',
message: 'my message',
operator: '!==',
values: [{label: 'Value is strictly equal:', formatted: /foo/}]
values: [{label: 'Value is the same as:', formatted: /foo/}]
});

t.end();
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ test('fails with the first assertError', t => {
t.is(result.reason.name, 'AssertionError');
t.same(result.reason.values, [
{label: 'Actual:', formatted: formatValue(1)},
{label: 'Must be strictly equal to:', formatted: formatValue(2)}
{label: 'Must be the same as:', formatted: formatValue(2)}
]);
t.end();
});
Expand Down