Skip to content

rename t.ok() to t.truthy() and t.notOk() to t.falsy() #716

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 6 commits into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,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 @@ -753,11 +753,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 @@ -843,14 +843,14 @@ test(t => {
const a = /foo/;
const b = 'bar';
const c = 'baz';
t.ok(a.test(b) || b === c);
t.truthy(a.test(b) || b === c);
Copy link
Member

Choose a reason for hiding this comment

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

t.true()

});
```

Will output:

```
t.ok(a.test(b) || b === c)
t.truthy(a.test(b) || b === c)
Copy link
Member

Choose a reason for hiding this comment

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

t.true()

| | | |
Copy link
Contributor

Choose a reason for hiding this comment

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

add two spaces to align

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Thanks @jfmengels and @forresst

| "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\.truthy\(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\.truthy\(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
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.truthy(true);
Copy link
Member

Choose a reason for hiding this comment

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

t.pass();

});

test('this is a failing test', t => {
t.ok(false);
t.truthy(false);
Copy link
Member

Choose a reason for hiding this comment

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

t.fail();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was going to go through and update some of these to a more sensible test, but decided to opt for a direct refactor. I'll go through and update these to be better 👍

});
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.truthy(a === 'bar');
});

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

t.ok(a === 'foo', 'with message');
t.truthy(a === 'foo', 'with message');
Copy link
Member

Choose a reason for hiding this comment

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

Actually, these should just be t.true.

});
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.truthy(true);
Copy link
Member

Choose a reason for hiding this comment

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

t.pass();

t.end();
});

Expand Down