Skip to content

Commit

Permalink
rename t.ok() to t.truthy() and t.notOk() to t.falsy()
Browse files Browse the repository at this point in the history
* refactor `ok` to `truthy` and `notOk` to `falsy`

* update tests to be more explicit

* update docs to use a better assertion api

* realign power-assert output

* quick typo fix

* update assertions
  • Loading branch information
Kent C. Dodds authored and jamestalmage committed Apr 6, 2016
1 parent bcda753 commit a7f50eb
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 33 deletions.
10 changes: 9 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ export interface AssertContext {
/**
* Assert that value is truthy.
*/
ok(value: any, message?: string): void;
truthy(value: any, message?: string): void;
/**
* Assert that value is falsy.
*/
falsy(value: any, message?: string): void;
/**
* DEPRECATED, use `truthy`. Assert that value is truthy.
*/
ok(value: any, message?: string): void;
/**
* DEPRECATED, use `falsy`. Assert that value is falsy.
*/
notOk(value: any, message?: string): void;
/**
* Assert that value is true.
Expand Down
10 changes: 6 additions & 4 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ x.fail = function (msg) {
test(false, create(false, false, 'fail', msg, x.fail));
};

x.ok = function (val, msg) {
test(val, create(val, true, '==', msg, x.ok));
x.truthy = function (val, msg) {
test(val, create(val, true, '==', msg, x.truthy));
};

x.notOk = function (val, msg) {
test(!val, create(val, false, '==', msg, x.notOk));
x.falsy = function (val, msg) {
test(!val, create(val, false, '==', msg, x.falsy));
};

x.true = function (val, msg) {
Expand Down Expand Up @@ -142,6 +142,8 @@ x.ifError = x.error = function (err, msg) {
* deprecated APIs
*/
x.doesNotThrow = util.deprecate(x.notThrows, getDeprecationNotice('doesNotThrow()', 'notThrows()'));
x.ok = util.deprecate(x.truthy, getDeprecationNotice('ok()', 'truthy()'));
x.notOk = util.deprecate(x.falsy, getDeprecationNotice('notOk()', 'falsy()'));
x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()'));
x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()'));

Expand Down
6 changes: 4 additions & 2 deletions lib/enhance-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = enhanceAssert;
module.exports.formatter = formatter;

module.exports.PATTERNS = [
't.ok(value, [message])',
't.notOk(value, [message])',
't.truthy(value, [message])',
't.falsy(value, [message])',
't.true(value, [message])',
't.false(value, [message])',
't.is(value, expected, [message])',
Expand All @@ -12,6 +12,8 @@ module.exports.PATTERNS = [
't.notDeepEqual(value, expected, [message])',
't.regex(contents, regex, [message])',
// deprecated apis
't.ok(value, [message])',
't.notOk(value, [message])',
't.same(value, expected, [message])',
't.notSame(value, expected, [message])'
];
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ Assertions are mixed into the [execution object](#t) provided to each test callb

```js
test(t => {
t.ok('unicorn'); // assertion
t.truthy('unicorn'); // assertion
});
```

Expand All @@ -755,11 +755,11 @@ Passing assertion.

Failing assertion.

### `.ok(value, [message])`
### `.truthy(value, [message])`

Assert that `value` is truthy.

### `.notOk(value, [message])`
### `.falsy(value, [message])`

Assert that `value` is falsy.

Expand Down Expand Up @@ -832,7 +832,7 @@ const c = 'baz';
require('assert').ok(a.test(b) || b === c);
```

If you paste that into a Node REPL it'l return:
If you paste that into a Node REPL it'll return:

```
AssertionError: false == true
Expand All @@ -845,16 +845,16 @@ test(t => {
const a = /foo/;
const b = 'bar';
const c = 'baz';
t.ok(a.test(b) || b === c);
t.true(a.test(b) || b === c);
});
```

Will output:

```
t.ok(a.test(b) || b === c)
| | | |
| "bar" "bar" "baz"
t.true(a.test(b) || b === c)
| | | |
| "bar" "bar" "baz"
false
```

Expand Down
4 changes: 2 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,12 @@ test('power-assert support', function (t) {

t.match(
api.errors[0].error.message,
/t\.ok\(a === 'bar'\)\s*\n\s+\|\s*\n\s+"foo"/m
/t\.true\(a === 'bar'\)\s*\n\s+\|\s*\n\s+"foo"/m
);

t.match(
api.errors[1].error.message,
/with message\s+t\.ok\(a === 'foo', 'with message'\)\s*\n\s+\|\s*\n\s+"bar"/m
/with message\s+t\.true\(a === 'foo', 'with message'\)\s*\n\s+\|\s*\n\s+"bar"/m
);
});
});
Expand Down
20 changes: 10 additions & 10 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ test('.fail()', function (t) {
t.end();
});

test('.ok()', function (t) {
test('.truthy()', function (t) {
t.throws(function () {
assert.ok(0);
assert.ok(false);
assert.truthy(0);
assert.truthy(false);
});

t.doesNotThrow(function () {
assert.ok(1);
assert.ok(true);
assert.truthy(1);
assert.truthy(true);
});

t.end();
});

test('.notOk()', function (t) {
test('.falsy()', function (t) {
t.throws(function () {
assert.notOk(1);
assert.notOk(true);
assert.falsy(1);
assert.falsy(true);
});

t.doesNotThrow(function () {
assert.notOk(0);
assert.notOk(false);
assert.falsy(0);
assert.falsy(false);
});

t.end();
Expand Down
2 changes: 1 addition & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ test('throwing a anonymous function will report the function to the console', fu
test('log failed tests', function (t) {
execCli('fixture/one-pass-one-fail.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /false == true/);
t.match(stderr, /failed via t.fail\(\)/);
t.end();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/one-pass-one-fail.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import test from '../../';

test('this is a passing test', t => {
t.ok(true);
t.pass();
});

test('this is a failing test', t => {
t.ok(false);
t.fail();
});
4 changes: 2 additions & 2 deletions test/fixture/power-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import test from '../../';
test.serial(t => {
const a = 'foo';

t.ok(a === 'bar');
t.true(a === 'bar');
});

test.serial(t => {
const a = 'bar';

t.ok(a === 'foo', 'with message');
t.true(a === 'foo', 'with message');
});
2 changes: 1 addition & 1 deletion test/fixture/slow-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.cb('long running', t => {
}, {alwaysLast: true});

setTimeout(() => {
t.ok(true);
t.pass();
t.end();
});

Expand Down

0 comments on commit a7f50eb

Please sign in to comment.