@@ -684,7 +684,7 @@ Given Mocha's use of function expressions to define suites and test cases, it's
684
684
Take the following example:
685
685
686
686
``` js
687
- const assert = require (' chai ' ). assert ;
687
+ const assert = require (' assert ' ) ;
688
688
689
689
function add (args ) {
690
690
return args .reduce ((prev , curr ) => prev + curr, 0 );
@@ -700,7 +700,7 @@ describe('add()', function() {
700
700
tests .forEach (({args, expected}) => {
701
701
it (` correctly adds ${ args .length } args` , function () {
702
702
const res = add (args);
703
- assert .equal (res, expected);
703
+ assert .strictEqual (res, expected);
704
704
});
705
705
});
706
706
});
@@ -725,7 +725,7 @@ describe('add()', function() {
725
725
const testAdd = ({args, expected}) =>
726
726
function () {
727
727
const res = add (args);
728
- assert .equal (res, expected);
728
+ assert .strictEqual (res, expected);
729
729
};
730
730
731
731
it (' correctly adds 2 args' , testAdd ({args: [1 , 2 ], expected: 3 }));
@@ -734,6 +734,34 @@ describe('add()', function() {
734
734
});
735
735
```
736
736
737
+ With ` top-level await ` you can collect your test data in a dynamic and asynchronous way while the test file is being loaded:
738
+
739
+ ``` js
740
+ // testfile.mjs
741
+ import assert from ' assert' ;
742
+
743
+ // top-level await: Node >= v14.8.0 with ESM test file
744
+ const tests = await new Promise (resolve => {
745
+ setTimeout (() => {
746
+ resolve ([
747
+ {args: [1 , 2 ], expected: 3 },
748
+ {args: [1 , 2 , 3 ], expected: 6 },
749
+ {args: [1 , 2 , 3 , 4 ], expected: 10 }
750
+ ]);
751
+ }, 5000 );
752
+ });
753
+
754
+ // in suites ASYNCHRONOUS callbacks are NOT supported
755
+ describe (' add()' , function () {
756
+ tests .forEach (({args, expected}) => {
757
+ it (` correctly adds ${ args .length } args` , function () {
758
+ const res = args .reduce ((sum , curr ) => sum + curr, 0 );
759
+ assert .strictEqual (res, expected);
760
+ });
761
+ });
762
+ });
763
+ ```
764
+
737
765
<h2 id =" test-duration " >Test duration</h2 >
738
766
739
767
Many reporters will display test duration and flag tests that are slow (default: 75ms), as shown here with the SPEC reporter:
0 commit comments