File tree Expand file tree Collapse file tree 6 files changed +104
-0
lines changed
samples/node/web/test/async-test Expand file tree Collapse file tree 6 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "version" : " 0.2.0" ,
3
+ "configurations" : [
4
+ {
5
+ "name" : " Test" ,
6
+ "type" : " node" ,
7
+ "request" : " launch" ,
8
+ "program" : " ${workspaceRoot}/node_modules/mocha/bin/mocha" ,
9
+ "stopOnEntry" : false ,
10
+ "args" : [
11
+ " --require" ,
12
+ " hook"
13
+ ],
14
+ "cwd" : " ${workspaceRoot}" ,
15
+ "preLaunchTask" : null ,
16
+ "runtimeExecutable" : null ,
17
+ "runtimeArgs" : [
18
+ " --nolazy"
19
+ ],
20
+ "env" : {
21
+ "NODE_ENV" : " test"
22
+ },
23
+ "externalConsole" : false ,
24
+ "sourceMaps" : false ,
25
+ "outDir" : null
26
+ }
27
+ ]
28
+ }
Original file line number Diff line number Diff line change
1
+ 1 + (2 + 4) * (9 - 2) / 3
Original file line number Diff line number Diff line change
1
+ // hello.js
2
+ console . log ( 'init hello.js...' ) ;
3
+
4
+ const fs = require ( 'mz/fs' ) ;
5
+
6
+ // a simple async function:
7
+ module . exports = async ( ) => {
8
+ let expression = await fs . readFile ( './data.txt' , 'utf-8' ) ;
9
+ let fn = new Function ( 'return ' + expression ) ;
10
+ let r = fn ( ) ;
11
+ console . log ( `Calculate: ${ expression } = ${ r } ` ) ;
12
+ return r ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ require ( 'babel-core/register' ) ( {
2
+ presets : [ 'stage-3' ]
3
+ } ) ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " mocha-async-test" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " Mocha async test" ,
5
+ "main" : " app.js" ,
6
+ "scripts" : {
7
+ "test" : " mocha --require hook"
8
+ },
9
+ "keywords" : [
10
+ " mocha" ,
11
+ " test" ,
12
+ " async"
13
+ ],
14
+ "author" : " Michael Liao" ,
15
+ "license" : " Apache-2.0" ,
16
+ "repository" : {
17
+ "type" : " git" ,
18
+ "url" : " https://github.com/michaelliao/learn-javascript.git"
19
+ },
20
+ "dependencies" : {
21
+ "babel-core" : " 6.13.2" ,
22
+ "babel-polyfill" : " 6.13.0" ,
23
+ "babel-preset-es2015-node6" : " 0.3.0" ,
24
+ "babel-preset-stage-3" : " 6.5.0" ,
25
+ "mz" : " 2.4.0"
26
+ },
27
+ "devDependencies" : {
28
+ "mocha" : " 3.0.2"
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ const assert = require ( 'assert' ) ;
2
+
3
+ const hello = require ( '../hello' ) ;
4
+
5
+ describe ( '#async hello' , ( ) => {
6
+ describe ( '#asyncCalculate()' , ( ) => {
7
+ // function(done) {}
8
+ it ( '#async with done' , ( done ) => {
9
+ ( async function ( ) {
10
+ try {
11
+ let r = await hello ( ) ;
12
+ assert . strictEqual ( r , 15 ) ;
13
+ done ( ) ;
14
+ } catch ( err ) {
15
+ done ( err ) ;
16
+ }
17
+ } ) ( ) ;
18
+ } ) ;
19
+
20
+ it ( '#async function' , async ( ) => {
21
+ let r = await hello ( ) ;
22
+ assert . strictEqual ( r , 15 ) ;
23
+ } ) ;
24
+
25
+ it ( '#sync function' , ( ) => {
26
+ assert ( true ) ;
27
+ } ) ;
28
+ } ) ;
29
+ } ) ;
You can’t perform that action at this time.
0 commit comments