Skip to content

Commit 9406470

Browse files
eemednovemberborn
authored andcommitted
Only apply power-assert to new t.assert() assertion
1 parent c1f6fdf commit 9406470

23 files changed

+171
-82
lines changed

docs/03-assertions.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test('skip assertion', t => {
8484

8585
## Enhanced assertion messages
8686

87-
AVA comes with [`power-assert`](https://github.com/power-assert-js/power-assert) built-in, giving you more descriptive assertion messages. It reads your test and tries to infer more information from the code.
87+
AVA comes with [`power-assert`](https://github.com/power-assert-js/power-assert) built-in, giving you more descriptive assertion messages.
8888

8989
Let's take this example, using Node's standard [`assert` library](https://nodejs.org/api/assert.html):
9090

@@ -101,24 +101,48 @@ If you paste that into a Node REPL it'll return:
101101
AssertionError: false == true
102102
```
103103

104-
In AVA however, this test:
104+
With AVA's `assert` assertion however, this test:
105105

106106
```js
107107
test('enhanced assertions', t => {
108108
const a = /foo/;
109109
const b = 'bar';
110110
const c = 'baz';
111-
t.true(a.test(b) || b === c);
111+
t.assert(a.test(b) || b === c);
112112
});
113113
```
114114

115115
Will output:
116116

117117
```
118-
t.true(a.test(b) || b === c)
119-
| | | |
120-
| "bar" "bar" "baz"
121-
false
118+
6: const c = 'baz';
119+
7: t.assert(a.test(b) || b === c);
120+
8: });
121+
122+
Value is not truthy:
123+
124+
false
125+
126+
a.test(b) || b === c
127+
=> false
128+
129+
b === c
130+
=> false
131+
132+
c
133+
=> 'baz'
134+
135+
b
136+
=> 'bar'
137+
138+
a.test(b)
139+
=> false
140+
141+
b
142+
=> 'bar'
143+
144+
a
145+
=> /foo/
122146
```
123147

124148
## Custom assertions
@@ -147,6 +171,10 @@ Passing assertion.
147171

148172
Failing assertion.
149173

174+
### `.assert(value, [message])`
175+
176+
Asserts that `value` is truthy. This is [`power-assert`](#enhanced-assertion-messages) enabled.
177+
150178
### `.truthy(value, [message])`
151179

152180
Assert that `value` is truthy.

docs/06-configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ Arguments passed to the CLI will always take precedence over the CLI options con
5757
- `tap`: if `true`, enables the [TAP reporter](./05-command-line.md#tap-reporter)
5858
- `verbose`: if `true`, enables verbose output
5959
- `snapshotDir`: specifies a fixed location for storing snapshot files. Use this if your snapshots are ending up in the wrong location
60-
- `compileEnhancements`: if `false`, disables [power-assert](https://github.com/power-assert-js/power-assert) — which otherwise helps provide more descriptive error messages — and detection of improper use of the `t.throws()` assertion
61-
- `extensions`: extensions of test files that are not precompiled using AVA's Babel presets. Note that files are still compiled to enable power-assert and other features, so you may also need to set `compileEnhancements` to `false` if your files are not valid JavaScript. Setting this overrides the default `"js"` value, so make sure to include that extension in the list, as long as it's not included in `babel.extensions`
60+
- `compileEnhancements`: if `false`, disables [`power-assert`](./03-assertions.md#enhanced-assertion-messages) — which otherwise helps provide more descriptive error messages — and detection of improper use of the `t.throws()` assertion
61+
- `extensions`: extensions of test files that are not precompiled using AVA's Babel presets. Note that files are still compiled to enable `power-assert` and other features, so you may also need to set `compileEnhancements` to `false` if your files are not valid JavaScript. Setting this overrides the default `"js"` value, so make sure to include that extension in the list, as long as it's not included in `babel.extensions`
6262
- `require`: extra modules to require before tests are run. Modules are required in the [worker processes](./01-writing-tests.md#process-isolation)
6363
- `babel`: test file specific Babel options. See our [Babel recipe](./recipes/babel.md#configuring-babel) for more details
6464
- `babel.extensions`: extensions of test files that will be precompiled using AVA's Babel presets. Setting this overrides the default `"js"` value, so make sure to include that extension in the list

docs/08-common-pitfalls.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ AVA [can't trace uncaught exceptions](https://github.com/avajs/ava/issues/214) b
7575

7676
### Why are the enhanced assertion messages not shown?
7777

78-
Ensure that the first parameter passed into your test is named `t`. This is a requirement of [`power-assert`](https://github.com/power-assert-js/power-assert), the library that provides the enhanced messages.
78+
Ensure that the first parameter passed into your test is named `t`. This is a requirement of [`power-assert`](https://github.com/power-assert-js/power-assert), the library that provides the [enhanced messages](./03-assertions.md#enhanced-assertion-messages).
7979

8080
```js
8181
test('one is one', t => {
82-
t.is(1, 1);
82+
t.assert(1 === 1);
8383
});
8484
```
8585

lib/assert.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -589,110 +589,139 @@ function wrapAssertions(callbacks) {
589589
message: message || 'No snapshot available, run with --update-snapshots'
590590
}));
591591
}
592-
}
593-
};
592+
},
594593

595-
const enhancedAssertions = enhanceAssert(pass, fail, {
596594
truthy(actual, message) {
597-
if (!actual) {
598-
throw new AssertionError({
595+
if (actual) {
596+
pass(this);
597+
} else {
598+
fail(this, new AssertionError({
599599
assertion: 'truthy',
600600
message,
601601
operator: '!!',
602602
values: [formatWithLabel('Value is not truthy:', actual)]
603-
});
603+
}));
604604
}
605605
},
606606

607607
falsy(actual, message) {
608608
if (actual) {
609-
throw new AssertionError({
609+
fail(this, new AssertionError({
610610
assertion: 'falsy',
611611
message,
612612
operator: '!',
613613
values: [formatWithLabel('Value is not falsy:', actual)]
614-
});
614+
}));
615+
} else {
616+
pass(this);
615617
}
616618
},
617619

618620
true(actual, message) {
619-
if (actual !== true) {
620-
throw new AssertionError({
621+
if (actual === true) {
622+
pass(this);
623+
} else {
624+
fail(this, new AssertionError({
621625
assertion: 'true',
622626
message,
623627
values: [formatWithLabel('Value is not `true`:', actual)]
624-
});
628+
}));
625629
}
626630
},
627631

628632
false(actual, message) {
629-
if (actual !== false) {
630-
throw new AssertionError({
633+
if (actual === false) {
634+
pass(this);
635+
} else {
636+
fail(this, new AssertionError({
631637
assertion: 'false',
632638
message,
633639
values: [formatWithLabel('Value is not `false`:', actual)]
634-
});
640+
}));
635641
}
636642
},
637643

638644
regex(string, regex, message) {
639645
if (typeof string !== 'string') {
640-
throw new AssertionError({
646+
fail(this, new AssertionError({
641647
assertion: 'regex',
642648
improperUsage: true,
643649
message: '`t.regex()` must be called with a string',
644650
values: [formatWithLabel('Called with:', string)]
645-
});
651+
}));
652+
return;
646653
}
647654

648655
if (!(regex instanceof RegExp)) {
649-
throw new AssertionError({
656+
fail(this, new AssertionError({
650657
assertion: 'regex',
651658
improperUsage: true,
652659
message: '`t.regex()` must be called with a regular expression',
653660
values: [formatWithLabel('Called with:', regex)]
654-
});
661+
}));
662+
return;
655663
}
656664

657665
if (!regex.test(string)) {
658-
throw new AssertionError({
666+
fail(this, new AssertionError({
659667
assertion: 'regex',
660668
message,
661669
values: [
662670
formatWithLabel('Value must match expression:', string),
663671
formatWithLabel('Regular expression:', regex)
664672
]
665-
});
673+
}));
674+
return;
666675
}
676+
677+
pass(this);
667678
},
668679

669680
notRegex(string, regex, message) {
670681
if (typeof string !== 'string') {
671-
throw new AssertionError({
682+
fail(this, new AssertionError({
672683
assertion: 'notRegex',
673684
improperUsage: true,
674685
message: '`t.notRegex()` must be called with a string',
675686
values: [formatWithLabel('Called with:', string)]
676-
});
687+
}));
688+
return;
677689
}
678690

679691
if (!(regex instanceof RegExp)) {
680-
throw new AssertionError({
692+
fail(this, new AssertionError({
681693
assertion: 'notRegex',
682694
improperUsage: true,
683695
message: '`t.notRegex()` must be called with a regular expression',
684696
values: [formatWithLabel('Called with:', regex)]
685-
});
697+
}));
698+
return;
686699
}
687700

688701
if (regex.test(string)) {
689-
throw new AssertionError({
702+
fail(this, new AssertionError({
690703
assertion: 'notRegex',
691704
message,
692705
values: [
693706
formatWithLabel('Value must not match expression:', string),
694707
formatWithLabel('Regular expression:', regex)
695708
]
709+
}));
710+
return;
711+
}
712+
713+
pass(this);
714+
}
715+
};
716+
717+
const enhancedAssertions = enhanceAssert(pass, fail, {
718+
assert(actual, message) {
719+
if (!actual) {
720+
throw new AssertionError({
721+
assertion: 'assert',
722+
message,
723+
operator: '!!',
724+
values: [formatWithLabel('Value is not truthy:', actual)]
696725
});
697726
}
698727
}

lib/enhance-assert.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ const concordanceOptions = require('./concordance-options').default;
88
// https://github.com/avajs/babel-preset-transform-test-files/blob/master/espower-patterns.json
99
// Then release a new version of that preset and bump the SemVer range here.
1010
const PATTERNS = [
11-
't.truthy(value, [message])',
12-
't.falsy(value, [message])',
13-
't.true(value, [message])',
14-
't.false(value, [message])',
15-
't.regex(contents, regex, [message])',
16-
't.notRegex(contents, regex, [message])'
11+
't.assert(value, [message])'
1712
];
1813

1914
const computeStatement = node => generate(node).code;

media/magic-assert-combined.png

-77.4 KB
Loading

media/magic-assert-nested.png

-50.9 KB
Binary file not shown.

media/power-assert.png

88.6 KB
Loading

media/screenshot-fixtures/magic-assert-nested.js

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "screenshot-fixtures",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "magic-assert-buffers.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": ""
11+
}

0 commit comments

Comments
 (0)