[Pkg-javascript-commits] [node-tap] 93/186: fail more usefully when a snapshot entry is not found

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:48 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-tap.

commit c4436bfaac627d82ef254a1cefad4de718b05b31
Author: isaacs <i at izs.me>
Date:   Wed Nov 15 16:40:57 2017 -0800

    fail more usefully when a snapshot entry is not found
---
 lib/snapshot.js  | 14 +++++++++++---
 test/snapshot.js |  1 +
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/lib/snapshot.js b/lib/snapshot.js
index 874876f..86af30f 100644
--- a/lib/snapshot.js
+++ b/lib/snapshot.js
@@ -21,7 +21,6 @@ class Snapshot {
 
   // should only ever call _one_ of read/save
   read (message) {
-    // XXX test line-by-line instead of all at once
     const index = this.indexes.get(message) || 1
     this.indexes.set(message, index + 1)
     try {
@@ -33,8 +32,17 @@ class Snapshot {
         'to create snapshot files'
       )
     }
-    return this.snapshot[message + ' ' + index]
-      .replace(/^\n|\n$/g, '')
+
+    const entry = message + ' ' + index
+    const s = this.snapshot[entry]
+    if (s === undefined)
+      throw new Error(
+        'Snapshot entry not found: "' + entry +  + '"\n' +
+        'Run with TAP_SNAPSHOT=1 in the environment\n' +
+        'to create snapshots'
+      )
+
+    return s.replace(/^\n|\n$/g, '')
   }
 
   snap (data, message) {
diff --git a/test/snapshot.js b/test/snapshot.js
index b676637..2eb1b40 100644
--- a/test/snapshot.js
+++ b/test/snapshot.js
@@ -33,6 +33,7 @@ t.test('actual test', t => {
   const ss = new Snapshot(t)
   t.equal(fs.readFileSync(__filename, 'utf8'), ss.read('this file'))
   t.equal('this is fine', ss.read('a statement of acceptance'))
+  t.throws(_ => ss.read('this is not in the file'))
 
   t.comment('saving without snapping anything removes the file')
   const sss = new Snapshot(t)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tap.git



More information about the Pkg-javascript-commits mailing list