Browse Source

correct async test issue

Kieran Gibb 3 years ago
parent
commit
1681cc005c
No account linked to committer's email address
2 changed files with 14 additions and 12 deletions
  1. 9
    8
      test/index.test.js
  2. 5
    4
      test/util.js

+ 9
- 8
test/index.test.js View File

@@ -90,9 +90,13 @@ describe('message encoding', (context) => {
90 90
 })
91 91
 
92 92
 describe('hypercore', (context) => {
93
-  context('encrypted the log', (assert, next) => {
94
-    const storage = tmp()
93
+  let storage
94
+
95
+  context.beforeEach((c) => {
96
+    storage = tmp()
97
+  })
95 98
 
99
+  context('encrypted the log', (assert, next) => {
96 100
     const key = crypto.symmetricKey()
97 101
     const encoder = crypto.encoder(key, { valueEncoding: 'utf-8' })
98 102
     const feed = hypercore(storage.path, { valueEncoding: encoder })
@@ -108,14 +112,12 @@ describe('hypercore', (context) => {
108 112
         assert.error(err, 'no error')
109 113
         assert.same('boop', entry, 'hypercore decrypts the message')
110 114
 
111
-        cleanup(storage.root)
112
-        next()
115
+        cleanup(storage.root, next)
113 116
       })
114 117
     })
115 118
   })
116
-  context('encrypted the log, with a json object', (assert, next) => {
117
-    const storage = tmp()
118 119
 
120
+  context('encrypted the log, with a json object', (assert, next) => {
119 121
     const key = crypto.symmetricKey()
120 122
     const encoder = crypto.encoder(key, { valueEncoding: 'json' })
121 123
     const feed = hypercore(storage.path, { valueEncoding: encoder })
@@ -129,8 +131,7 @@ describe('hypercore', (context) => {
129 131
         assert.error(err, 'no error')
130 132
         assert.same(message, entry, 'hypercore decrypts the message')
131 133
 
132
-        cleanup(storage.root)
133
-        next()
134
+        cleanup(storage.root, next)
134 135
       })
135 136
     })
136 137
   })

+ 5
- 4
test/util.js View File

@@ -3,10 +3,11 @@ const mkdirp = require('mkdirp')
3 3
 const rimraf = require('rimraf')
4 4
 const debug = require('debug')('cobox-crypto')
5 5
 
6
-function cleanup (dir) {
6
+function cleanup (dir, cb) {
7 7
   rimraf(dir, (err) => {
8
-    debug(`[CLEANUP] ${ err ? 'failed' : 'success'}`)
9
-    if (err) throw (err)
8
+    debug(`[CLEANUP] cleaning ${dir} ${ err ? 'failed' : 'success'}`)
9
+    if (err) return console.error(err)
10
+    cb()
10 11
   })
11 12
 }
12 13
 
@@ -14,7 +15,7 @@ function tmp () {
14 15
   var root = `./tmp/`
15 16
   var path = `.${tmpdir().name}`
16 17
   mkdirp.sync(path)
17
-  debug(`[TEMP] ${path}`)
18
+  debug(`[TEMP] generated temp directory ${path}`)
18 19
   return { path, root }
19 20
 }
20 21