[Pkg-javascript-commits] [node-tap] 59/186: Make snapshots nicer looking, support objects. More test coverage

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:43 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 14cb62eff9372ebde6f11f158e8fce95b744bf38
Author: isaacs <i at izs.me>
Date:   Sat Nov 4 17:22:30 2017 -0700

    Make snapshots nicer looking, support objects. More test coverage
---
 lib/snapshot.js                   |  13 +-
 lib/test.js                       |  21 +-
 tap-snapshots/unit-test.js-TAP.js | 976 +++++++++++++++++++++++++++++++++++++-
 unit/test.js                      | 188 +++++++-
 4 files changed, 1158 insertions(+), 40 deletions(-)

diff --git a/lib/snapshot.js b/lib/snapshot.js
index f250876..7ce8818 100644
--- a/lib/snapshot.js
+++ b/lib/snapshot.js
@@ -21,7 +21,7 @@ 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) || 0
+    const index = this.indexes.get(message) || 1
     this.indexes.set(message, index + 1)
     try {
       this.snapshot = this.snapshot || require(this.file)
@@ -32,14 +32,15 @@ class Snapshot {
         'to create snapshot files'
       )
     }
-    return this.snapshot[message + '_' + index]
+    return this.snapshot[message + ' ' + index]
+      .replace(/^\n|\n$/g, '')
   }
 
   snap (data, message) {
-    const index = this.indexes.get(message) || 0
+    const index = this.indexes.get(message) || 1
     this.indexes.set(message, index + 1)
     this.snapshot = this.snapshot || {}
-    this.snapshot[message + '_' + index] = data
+    this.snapshot[message + ' ' + index] = data
   }
 
   save () {
@@ -50,9 +51,9 @@ class Snapshot {
         Object.keys(this.snapshot).map(s =>
           `exports[\`${
             s.replace(/\`/g, '\\\`')
-          }\`] = \`${
+          }\`] = \`\n${
             this.snapshot[s].replace(/\`/g, '\\\`')
-          }\`\n`).join('\n'))
+          }\n\`\n`).join('\n'))
       mkdirp.sync(path.dirname(this.file))
       writeFile.sync(this.file, data, 'utf8')
     }
diff --git a/lib/test.js b/lib/test.js
index 88b5f19..9614b25 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -298,12 +298,15 @@ class Test extends Base {
       } else if (typeof p === 'string') {
         this.debug(' > STRING')
         this.parser.write(p)
-      } else if (Array.isArray(p)) {
-        this.debug(' > METHOD')
-        const m = p.shift()
-        this[m].apply(this, p)
       } else {
-        throw new Error('weird thing got in the queue')
+        /* istanbul ignore else */
+        if (Array.isArray(p)) {
+          this.debug(' > METHOD')
+          const m = p.shift()
+          this[m].apply(this, p)
+        } else {
+          throw new Error('weird thing got in the queue')
+        }
       }
     }
 
@@ -951,7 +954,7 @@ class Test extends Base {
 
   get fullname () {
     const cwd = process.cwd()
-    const main = process.argv[1]
+    const main = process.argv[1] || ''
     return (this.parent ? this.parent.fullname
       : main.indexOf(cwd) === 0 ? main.substr(cwd.length + 1)
       : path.basename(main)) +
@@ -970,6 +973,12 @@ class Test extends Base {
 
     // use notOk because snap doesn't return a truthy value
     const m = this.fullname + ' > ' + message
+    if (typeof found !== 'string')
+      found = util.inspect(found, {
+        showHidden: false,
+        depth: Infinity
+      })
+
     return this.writeSnapshot
       ? this.notOk(this[_snapshot].snap(found, m),
         message, extra)
diff --git a/tap-snapshots/unit-test.js-TAP.js b/tap-snapshots/unit-test.js-TAP.js
index 824b85f..a1f9c3f 100644
--- a/tap-snapshots/unit-test.js-TAP.js
+++ b/tap-snapshots/unit-test.js-TAP.js
@@ -1,30 +1,167 @@
 'use strict'
-exports[`unit/test.js TAP short output checks no plan > no plan_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks no plan no options > no plan 1`] = `
+TAP version 13
 ok 1 - this is fine
 1..1
+
+`
+
+exports[`unit/test.js TAP short output checks no plan buffered > no plan 1`] = `
+TAP version 13
+ok 1 - this is fine
+1..1
+
+`
+
+exports[`unit/test.js TAP short output checks no plan bailout > no plan 1`] = `
+TAP version 13
+ok 1 - this is fine
+1..1
+
+`
+
+exports[`unit/test.js TAP short output checks no plan runOnly > no plan 1`] = `
+TAP version 13
+ok 1 - this is fine
+1..1
+
+`
+
+exports[`unit/test.js TAP short output checks plan no options > plan 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks plan buffered > plan 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks plan bailout > plan 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
 `
 
-exports[`unit/test.js TAP short output checks plan > plan_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks plan runOnly > plan 1`] = `
+TAP version 13
 1..1
 ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks comment no options > comment 1`] = `
+TAP version 13
+# this is fine
+1..0
+
+`
+
+exports[`unit/test.js TAP short output checks comment buffered > comment 1`] = `
+TAP version 13
+# this is fine
+1..0
+
+`
+
+exports[`unit/test.js TAP short output checks comment bailout > comment 1`] = `
+TAP version 13
+# this is fine
+1..0
+
 `
 
-exports[`unit/test.js TAP short output checks comment > comment_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks comment runOnly > comment 1`] = `
+TAP version 13
 # this is fine
 1..0
+
+`
+
+exports[`unit/test.js TAP short output checks pragma no options > pragma 1`] = `
+TAP version 13
+pragma +strict
+1..0
+
+`
+
+exports[`unit/test.js TAP short output checks pragma buffered > pragma 1`] = `
+TAP version 13
+pragma +strict
+1..0
+
+`
+
+exports[`unit/test.js TAP short output checks pragma bailout > pragma 1`] = `
+TAP version 13
+pragma +strict
+1..0
+
 `
 
-exports[`unit/test.js TAP short output checks pragma > pragma_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks pragma runOnly > pragma 1`] = `
+TAP version 13
 pragma +strict
 1..0
+
+`
+
+exports[`unit/test.js TAP short output checks todo no options > todo 1`] = `
+TAP version 13
+not ok 1 - i will do this later # TODO
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: todo
+  source: |
+    tt.notOk(true, 'i will do this later', { todo: true })
+  ...
+
+ok 2 - i will do this later # TODO
+not ok 3 - expect truthy value # SKIP
+ok 4 - i did not do this later # SKIP
+1..4
+# todo: 2
+# skip: 2
+
+`
+
+exports[`unit/test.js TAP short output checks todo buffered > todo 1`] = `
+TAP version 13
+not ok 1 - i will do this later # TODO
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: todo
+  source: |
+    tt.notOk(true, 'i will do this later', { todo: true })
+  ...
+
+ok 2 - i will do this later # TODO
+not ok 3 - expect truthy value # SKIP
+ok 4 - i did not do this later # SKIP
+1..4
+# todo: 2
+# skip: 2
+
 `
 
-exports[`unit/test.js TAP short output checks todo > todo_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks todo bailout > todo 1`] = `
+TAP version 13
 not ok 1 - i will do this later # TODO
   ---
   at:
-    line: 27
-    column: 10
+    line: #
+    column: #
     file: unit/test.js
     function: todo
   source: |
@@ -37,9 +174,33 @@ ok 4 - i did not do this later # SKIP
 1..4
 # todo: 2
 # skip: 2
+
+`
+
+exports[`unit/test.js TAP short output checks todo runOnly > todo 1`] = `
+TAP version 13
+not ok 1 - i will do this later # TODO
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: todo
+  source: |
+    tt.notOk(true, 'i will do this later', { todo: true })
+  ...
+
+ok 2 - i will do this later # SKIP filter: only
+not ok 3 - expect truthy value # SKIP
+ok 4 - i did not do this later # SKIP filter: only
+1..4
+# todo: 1
+# skip: 3
+
 `
 
-exports[`unit/test.js TAP short output checks only > only_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks only no options > only 1`] = `
+TAP version 13
 # "run this with a comment" has \`only\` set but all tests run
 # Subtest: run this with a comment
     1..0
@@ -55,30 +216,166 @@ ok 2 - this is a child test # {time}
 ok 3 - run this with a comment # {time}
 
 1..3
+
 `
 
-exports[`unit/test.js TAP short output checks no plan fail > no plan fail_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks only buffered > only 1`] = `
+TAP version 13
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 1 - run this with a comment # {time}
+
+# Subtest: this is a child test
+    1..0
+ok 2 - this is a child test # {time}
+
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 3 - run this with a comment # {time}
+
+1..3
+
+`
+
+exports[`unit/test.js TAP short output checks only bailout > only 1`] = `
+TAP version 13
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 1 - run this with a comment # {time}
+
+# Subtest: this is a child test
+    1..0
+ok 2 - this is a child test # {time}
+
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 3 - run this with a comment # {time}
+
+1..3
+
+`
+
+exports[`unit/test.js TAP short output checks only runOnly > only 1`] = `
+TAP version 13
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 1 - run this with a comment # {time}
+
+# Subtest: this is a child test
+    1..0
+ok 2 - this is a child test # {time}
+
+# "run this with a comment" has \`only\` set but all tests run
+# Subtest: run this with a comment
+    1..0
+ok 3 - run this with a comment # {time}
+
+1..3
+
+`
+
+exports[`unit/test.js TAP short output checks no plan fail no options > no plan fail 1`] = `
+TAP version 13
 not ok 1 - this is fine
 1..1
 # failed 1 test
+
 `
 
-exports[`unit/test.js TAP short output checks plan fail > plan fail_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks no plan fail buffered > no plan fail 1`] = `
+TAP version 13
+not ok 1 - this is fine
 1..1
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks no plan fail bailout > no plan fail 1`] = `
+TAP version 13
 not ok 1 - this is fine
+Bail out! # this is fine
+"# this is fine"
+`
+
+exports[`unit/test.js TAP short output checks no plan fail runOnly > no plan fail 1`] = `
+TAP version 13
+not ok 1 - this is fine
+1..1
 # failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks plan fail no options > plan fail 1`] = `
+TAP version 13
+1..1
+not ok 1 - this is fine
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks plan fail buffered > plan fail 1`] = `
+TAP version 13
+1..1
+not ok 1 - this is fine
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks plan fail bailout > plan fail 1`] = `
+TAP version 13
+1..1
+not ok 1 - this is fine
+Bail out! # this is fine
+"# this is fine"
+`
+
+exports[`unit/test.js TAP short output checks plan fail runOnly > plan fail 1`] = `
+TAP version 13
+1..1
+not ok 1 - this is fine
+# failed 1 test
+
 `
 
-exports[`unit/test.js TAP short output checks expect fail > expect fail_0`] = `TAP version 13
+exports[`unit/test.js TAP short output checks expect fail no options > expect fail 1`] = `
+TAP version 13
 1..1
 ok 1 - this is fine
+
 `
 
-exports[`unit/test.js TAP short output checks sub > sub_0`] = `TAP version 13
-# Subtest: named child
+exports[`unit/test.js TAP short output checks expect fail buffered > expect fail 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks expect fail bailout > expect fail 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks expect fail runOnly > expect fail 1`] = `
+TAP version 13
+1..1
+ok 1 - this is fine
+
+`
+
+exports[`unit/test.js TAP short output checks sub no options > sub 1`] = `
+TAP version 13
+ok 1 - named child # {time} {
     ok 1 - this is fine
     1..1
-ok 1 - named child # {time}
+}
 
 # Subtest: named_function
     1..1
@@ -91,17 +388,656 @@ ok 2 - named_function # {time}
 ok 3 - promisey # {time}
 
 1..3
+
 `
 
-exports[`unit/test.js TAP short output checks parallel sub > parallel sub_0`] = `TAP version 13
-1..2
-ok 1 - slow child # {time} {
-    1..0
+exports[`unit/test.js TAP short output checks sub buffered > sub 1`] = `
+TAP version 13
+ok 1 - named child # {time} {
+    ok 1 - this is fine
+    1..1
 }
 
-ok 2 - fast child # {time} {
-    ok 1 - slow is going
+# Subtest: named_function
+    1..1
+    ok 1 - also fine
+ok 2 - named_function # {time}
+
+# Subtest: promisey
+    ok 1 - i promise, it is fine
+    1..1
+ok 3 - promisey # {time}
+
+1..3
+
+`
+
+exports[`unit/test.js TAP short output checks sub bailout > sub 1`] = `
+TAP version 13
+ok 1 - named child # {time} {
+    ok 1 - this is fine
     1..1
 }
 
+# Subtest: named_function
+    1..1
+    ok 1 - also fine
+ok 2 - named_function # {time}
+
+# Subtest: promisey
+    ok 1 - i promise, it is fine
+    1..1
+ok 3 - promisey # {time}
+
+1..3
+
+`
+
+exports[`unit/test.js TAP short output checks sub runOnly > sub 1`] = `
+TAP version 13
+ok 1 - named child # SKIP filter: only
+ok 2 - named_function # SKIP filter: only
+ok 3 - promisey # SKIP filter: only
+1..3
+# skip: 3
+
+`
+
+exports[`unit/test.js TAP short output checks parallel sub no options > parallel sub 1`] = `
+TAP version 13
+1..2
+# Subtest: slow child
+    1..0
+ok 1 - slow child # {time}
+
+# Subtest: fast child
+    not ok 1 - slow is going
+      ---
+      at:
+        line: #
+        column: #
+        file: unit/test.js
+        type: Timeout
+        function: _
+        method: _onTimeout
+  stack: |
+{STACK}
+not ok 2 - fast child # {time}
+
+# failed 1 of 2 tests
+
+`
+
+exports[`unit/test.js TAP short output checks parallel sub buffered > parallel sub 1`] = `
+TAP version 13
+1..2
+# Subtest: slow child
+    1..0
+ok 1 - slow child # {time}
+
+# Subtest: fast child
+    not ok 1 - slow is going
+      ---
+      at:
+        line: #
+        column: #
+        file: unit/test.js
+        type: Timeout
+        function: _
+        method: _onTimeout
+  stack: |
+{STACK}
+not ok 2 - fast child # {time}
+
+# failed 1 of 2 tests
+
+`
+
+exports[`unit/test.js TAP short output checks parallel sub bailout > parallel sub 1`] = `
+TAP version 13
+1..2
+# Subtest: slow child
+    1..0
+ok 1 - slow child # {time}
+
+# Subtest: fast child
+    not ok 1 - slow is going
+      ---
+      at:
+        line: #
+        column: #
+        file: unit/test.js
+        type: Timeout
+        function: _
+        method: _onTimeout
+  stack: |
+{STACK}
+"# slow is going"
+`
+
+exports[`unit/test.js TAP short output checks parallel sub runOnly > parallel sub 1`] = `
+TAP version 13
+1..2
+ok 1 - slow child # SKIP filter: only
+ok 2 - fast child # SKIP filter: only
+# skip: 2
+
+`
+
+exports[`unit/test.js TAP short output checks reasoned bailout no options > reasoned bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks reasoned bailout buffered > reasoned bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks reasoned bailout bailout > reasoned bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks reasoned bailout runOnly > reasoned bailout 1`] = `
+TAP version 13
+ok 1 - (unnamed test) # SKIP filter: only
+1..1
+# skip: 1
+
+`
+
+exports[`unit/test.js TAP short output checks unreasonable bailout no options > unreasonable bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out!
+
+`
+
+exports[`unit/test.js TAP short output checks unreasonable bailout buffered > unreasonable bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out!
+
+`
+
+exports[`unit/test.js TAP short output checks unreasonable bailout bailout > unreasonable bailout 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    Bail out!
+
+`
+
+exports[`unit/test.js TAP short output checks unreasonable bailout runOnly > unreasonable bailout 1`] = `
+TAP version 13
+ok 1 - (unnamed test) # SKIP filter: only
+1..1
+# skip: 1
+
+`
+
+exports[`unit/test.js TAP short output checks bailout after end no options > bailout after end 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    1..1
+Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks bailout after end buffered > bailout after end 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    1..1
+Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks bailout after end bailout > bailout after end 1`] = `
+TAP version 13
+# Subtest: (unnamed test)
+    ok 1 - this is fine
+    1..1
+Bail out! not fine
+"not fine"
+`
+
+exports[`unit/test.js TAP short output checks bailout after end runOnly > bailout after end 1`] = `
+TAP version 13
+ok 1 - (unnamed test) # SKIP filter: only
+1..1
+# skip: 1
+
+`
+
+exports[`unit/test.js TAP short output checks gentle thrower no options > gentle thrower 1`] = `
+TAP version 13
+not ok 1 - ok
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Object.gentle thrower
+  stack: |
+{STACK}
+  source: |
+    'gentle thrower': tt => tt.threw(new Error('ok')),
+  ...
+
+1..1
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks gentle thrower buffered > gentle thrower 1`] = `
+TAP version 13
+not ok 1 - ok
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Object.gentle thrower
+  stack: |
+{STACK}
+  source: |
+    'gentle thrower': tt => tt.threw(new Error('ok')),
+  ...
+
+1..1
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP short output checks gentle thrower bailout > gentle thrower 1`] = `
+TAP version 13
+not ok 1 - ok
+  ---
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Object.gentle thrower
+  stack: |
+{STACK}
+  source: |
+    'gentle thrower': tt => tt.threw(new Error('ok')),
+  ...
+
+Bail out! # ok
+"# ok"
+`
+
+exports[`unit/test.js TAP short output checks gentle thrower runOnly > gentle thrower 1`] = `
+TAP version 13
+not ok 1 - ok
+  ---
+  runOnly: true
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: Object.gentle thrower
+  stack: |
+{STACK}
+  source: |
+    'gentle thrower': tt => tt.threw(new Error('ok')),
+  ...
+
+1..1
+# failed 1 test
+
+`
+
+exports[`unit/test.js TAP assertion checks error > error 1`] = `
+TAP version 13
+ok 1 - this is not an error
+not ok 2 - this error is poop
+  ---
+  found:
+    name: Error
+  stack: |
+{STACK}
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: error
+  stack: |
+{STACK}
+  source: |
+    tt.error(new Error('poop'), 'this error is poop')
+  ...
+
+not ok 3 - this error is "poop"
+  ---
+  found: poop
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: error
+  stack: |
+{STACK}
+  source: |
+    tt.error('poop', 'this error is "poop"')
+  ...
+
+1..3
+# failed 2 of 3 tests
+
+`
+
+exports[`unit/test.js TAP assertion checks equal > equal 1`] = `
+TAP version 13
+not ok 1 - should be equal
+  ---
+  found: 1
+  wanted: 2
+  compare: ===
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: equal
+  stack: |
+{STACK}
+  source: |
+    tt.equal(1, 2)
+  ...
+
+not ok 2 - should be equal # SKIP
+ok 3 - one is one
+not ok 4 - should be equal
+  ---
+  found:
+    foo: 1
+  wanted:
+    foo: 1
+  compare: ===
+  note: Objects never === one another
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: equal
+  stack: |
+{STACK}
+  source: |
+    tt.equal({foo: 1}, {foo: 1})
+  ...
+
+1..4
+# failed 3 of 4 tests
+# skip: 1
+
+`
+
+exports[`unit/test.js TAP assertion checks not > not 1`] = `
+TAP version 13
+ok 1 - should not be equal
+ok 2 - should not be equal # SKIP
+not ok 3 - one is not one
+  ---
+  found: 1
+  doNotWant: 1
+  compare: '!=='
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: not
+  stack: |
+{STACK}
+  source: |
+    tt.not(1, 1, 'one is not one')
+  ...
+
+ok 4 - should not be equal
+1..4
+# failed 1 of 4 tests
+# skip: 1
+
+`
+
+exports[`unit/test.js TAP assertion checks same > same 1`] = `
+TAP version 13
+ok 1 - should be equivalent
+ok 2 - should be equivalent
+ok 3 - object exactness
+not ok 4 - should be equivalent # SKIP
+ok 5 - this one passes
+ok 6 - should not be equivalent # SKIP
+not ok 7 - this one fails
+  ---
+  found:
+    foo:
+      bar: 2
+  doNotWant:
+    foo:
+      bar: '2'
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: same
+  stack: |
+{STACK}
+  source: |
+    tt.notSame({ foo: { bar: 2 } }, { foo: { bar: '2' } },
+  ...
+
+not ok 8 - should be equivalent strictly # SKIP
+not ok 9 - should be equivalent strictly
+  ---
+  found:
+    - 1
+    - 2
+    - 3
+  wanted:
+    - '1'
+    - '2'
+    - '3'
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: same
+  stack: |
+{STACK}
+  source: |
+    tt.strictSame([1, 2, 3], ['1', '2', '3'])
+  ...
+
+ok 10 - should be equivalent strictly
+ok 11 - should be equivalent strictly
+ok 12 - should be equivalent strictly # SKIP
+ok 13 - this one passes
+ok 14 - this one passes
+not ok 15 - this one fails
+  ---
+  found:
+    foo:
+      bar: 2
+  doNotWant:
+    foo:
+      bar: 2
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: same
+  stack: |
+{STACK}
+  source: |
+    tt.notStrictSame({ foo: { bar: 2 } }, { foo: { bar: 2 } },
+  ...
+
+1..15
+# failed 5 of 15 tests
+# skip: 4
+
+`
+
+exports[`unit/test.js TAP assertion checks match > match 1`] = `
+TAP version 13
+ok 1 - should match pattern provided
+not ok 2 - should match pattern provided
+  ---
+  found:
+    a: b
+    c: /asdf/
+  pattern:
+    a: asdf
+    c: 1
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: match
+  stack: |
+{STACK}
+  source: |
+    tt.match({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 })
+  ...
+
+ok 3 - a message
+not ok 4 - should match pattern provided # TODO
+  ---
+  found:
+    a: b
+    c: /asdf/
+  pattern:
+    a: asdf
+    c: 1
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: match
+  source: |
+    tt.match({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 },
+  ...
+
+not ok 5 - should not match pattern provided
+  ---
+  found:
+    a: b
+    c: /asdf/
+  pattern:
+    a: 'function String() { [native code] }'
+    c: 'function RegExp() { [native code] }'
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: match
+  stack: |
+{STACK}
+  source: |
+    tt.notMatch({ a: 'b', c: /asdf/ }, { a: String, c: RegExp })
+  ...
+
+ok 6 - should not match pattern provided
+not ok 7 - a message
+  ---
+  found:
+    a: b
+    c: /asdf/
+  pattern:
+    a: 'function String() { [native code] }'
+    c: 'function RegExp() { [native code] }'
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: match
+  stack: |
+{STACK}
+  source: |
+    tt.notMatch({ a: 'b', c: /asdf/ }, { a: String, c: RegExp },
+  ...
+
+ok 8 - should not match pattern provided # TODO
+1..8
+# failed 4 of 8 tests
+# todo: 2
+
+`
+
+exports[`unit/test.js TAP assertion checks type > type 1`] = `
+TAP version 13
+not ok 1 - this fails
+  ---
+  found: 'null'
+  wanted: object
+  compare: ===
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: type
+  stack: |
+{STACK}
+  source: |
+    tt.type(null, 'object', 'this fails')
+  ...
+
+ok 2 - type is object
+ok 3 - type is number
+ok 4 - type is Test
+not ok 5 - fails, anonymously
+  ---
+  found: Object
+  wanted: (anonymous constructor)
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: type
+  stack: |
+{STACK}
+  source: |
+    tt.type({}, function () {}, 'fails, anonymously')
+  ...
+
+ok 6 - a thing is a thing
+ok 7 - arrows are functions
+ok 8 - arrows are functions
+not ok 9 - fail: arrows are not objects
+  ---
+  found: function
+  wanted: Object
+  compare: ===
+  at:
+    line: #
+    column: #
+    file: unit/test.js
+    function: type
+  stack: |
+{STACK}
+  source: |
+    tt.type(() => {}, Object, 'fail: arrows are not objects')
+  ...
+
+ok 10 - type is object
+ok 11 - type is Test
+ok 12 - type is EventEmitter
+1..12
+# failed 3 of 12 tests
+
 `
diff --git a/unit/test.js b/unit/test.js
index 021fa67..e3a04be 100644
--- a/unit/test.js
+++ b/unit/test.js
@@ -1,7 +1,21 @@
 const t = require('../')
 const Test = t.Test
+const assert = require('assert')
+
+const clean = out => out
+  .replace(/ # time=[0-9\.]+m?s( \{.*)?\n/g, ' # {time}$1\n')
+  .replace(/\n( {2})+stack: \|-?\n((\1  .*).*\n)+/gm,
+    '\n$1stack: |\n{STACK}\n')
+  .replace(/:[0-9]+:[0-9]+(\)?\n)/g, '#:#$1')
+  .replace(/(line|column): [0-9]+/g, '$1: #')
+  .split(process.cwd()).join('{CWD}')
+
 
 t.test('short output checks', t => {
+  const env = process.env.TAP_BUFFER
+  process.env.TAP_BUFFER = '0'
+  t.teardown(_ => process.env.TAP_BUFFER = env)
+
   const cases = {
     'no plan': tt => {
       tt.pass('this is fine')
@@ -60,15 +74,15 @@ t.test('short output checks', t => {
     },
 
     'sub': tt => {
-      tt.test('named child', { buffered: false }, tt => {
+      tt.test('named child', { buffered: true }, tt => {
         tt.pass('this is fine')
         tt.end()
       })
-      tt.test({ buffered: false }, function named_function (tt) {
+      tt.test(function named_function (tt) {
         tt.plan(1)
         tt.pass('also fine')
       })
-      tt.test('promisey', { buffered: false }, tt => new Promise(resolve => {
+      tt.test('promisey', tt => new Promise(resolve => {
         tt.pass('i promise, it is fine')
         resolve()
       }))
@@ -88,6 +102,168 @@ t.test('short output checks', t => {
         tt.end()
       }))
     },
+
+    'reasoned bailout': tt => {
+      tt.test(tt => {
+        tt.pass('this is fine')
+        tt.bailout('not fine')
+      })
+      tt.end()
+    },
+
+    'unreasonable bailout': tt => {
+      tt.test(tt => {
+        tt.pass('this is fine')
+        tt.bailout()
+      })
+      tt.end()
+    },
+
+    'bailout after end': tt => {
+      tt.test(tt => {
+        tt.pass('this is fine')
+        tt.end()
+        tt.bailout('not fine')
+      })
+      tt.end()
+    },
+
+    // _actually_ throwing is only handled by root TAP test
+    // using a Domain to catch beyond async stack drops
+    'gentle thrower': tt => tt.threw(new Error('ok')),
+  }
+
+  const keys = Object.keys(cases)
+  t.plan(keys.length)
+
+  for (let i in cases) {
+    t.test(i, t => {
+      const go = (t, tt) => new Promise(resolve => {
+        let out = ''
+        tt.on('data', c => out += c)
+        let didIt = false
+        const done = reason => {
+          // make sure we don't test on BOTH bailout and end
+          // as that is unnecessary
+          if (didIt)
+            return
+          didIt = true
+
+          if (tt.output)
+            out = tt.output
+
+          if (reason)
+            out = out.trim() + '\n' + JSON.stringify(reason)
+
+          t.matchSnapshot(clean(out), i)
+          resolve()
+        }
+        tt.on('end', done)
+        tt.on('bailout', done)
+        cases[i](tt)
+      })
+
+      t.test('no options', t =>
+        go(t, new Test()))
+      t.test('buffered', t =>
+        go(t, new Test({ buffered: true })))
+      t.test('bailout', t =>
+        go(t, new Test({ bail: true })))
+      t.test('runOnly', t =>
+        go(t, new Test({ runOnly: true })))
+      t.end()
+    })
+  }
+})
+
+t.test('assertion checks', t => {
+  const env = process.env.TAP_BUFFER
+  process.env.TAP_BUFFER = '0'
+  t.teardown(_ => process.env.TAP_BUFFER = env)
+
+  const cases = {
+    'error': tt => {
+      tt.error(null, 'this is not an error')
+      tt.error(new Error('poop'), 'this error is poop')
+      tt.error('poop', 'this error is "poop"')
+      tt.end()
+    },
+
+    equal: tt => {
+      tt.equal(1, 2)
+      tt.equal(1, '1', { skip: true })
+      tt.equal(1, 1, 'one is one')
+      // fails, but with the special note
+      tt.equal({foo: 1}, {foo: 1})
+      tt.end()
+    },
+
+    not: tt => {
+      tt.not(1, 2)
+      tt.not(1, '1', { skip: true })
+      tt.not(1, 1, 'one is not one')
+      tt.not({}, {})
+      tt.end()
+    },
+
+    same: tt => {
+      const o = { foo: 1 }
+      tt.same([1, 2, 3], ['1', '2', '3'])
+      tt.same(o, o)
+      tt.same({ foo: 1 }, { foo: 1 }, 'object exactness')
+      tt.same({ foo: 2 }, { foo: 1 }, { skip: true })
+      tt.notSame({ foo: 2 }, { foo: 1 }, 'this one passes')
+      tt.notSame({ foo: 2 }, { foo: 1 }, { skip: true })
+      tt.notSame({ foo: { bar: 2 } }, { foo: { bar: '2' } },
+                 'this one fails')
+
+      tt.strictSame({ foo: 2 }, { foo: 1 }, { skip: true })
+      tt.strictSame([1, 2, 3], ['1', '2', '3'])
+      tt.strictSame(o, { foo: 1 })
+      tt.strictSame(o, o)
+      tt.notStrictSame({ foo: 2 }, { foo: 1 }, { skip: true })
+      tt.notStrictSame({ foo: 2 }, { foo: 1 }, 'this one passes')
+      tt.notStrictSame({ foo: { bar: 2 } }, { foo: { bar: '2' } },
+                       'this one passes')
+      tt.notStrictSame({ foo: { bar: 2 } }, { foo: { bar: 2 } },
+                       'this one fails')
+
+      tt.end()
+    },
+
+    match: tt => {
+      tt.match({ a: 'b', c: /asdf/ }, { a: String, c: RegExp })
+      tt.match({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 })
+      tt.match({ a: 'b', c: /asdf/ }, { a: String, c: RegExp },
+               'a message')
+      tt.match({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 },
+               { todo: true })
+      tt.notMatch({ a: 'b', c: /asdf/ }, { a: String, c: RegExp })
+      tt.notMatch({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 })
+      tt.notMatch({ a: 'b', c: /asdf/ }, { a: String, c: RegExp },
+                  'a message')
+      tt.notMatch({ a: 'b', c: /asdf/ }, { a: 'asdf', c: 1 },
+                  { todo: true })
+
+      tt.end()
+    },
+
+    type: tt => {
+      tt.type(null, 'object', 'this fails')
+      tt.type(null, 'object', { expectFail: true })
+      tt.type(1234, 'number')
+      tt.type(tt, Test)
+      tt.type({}, function () {}, 'fails, anonymously')
+      const o = {}
+      tt.type(o, o, 'a thing is a thing')
+      tt.type(() => {}, 'function', 'arrows are functions')
+      tt.type(() => {}, Function, 'arrows are functions')
+      tt.type(() => {}, Object, 'fail: arrows are not objects')
+      tt.type({}, 'object')
+      tt.type(tt, 'Test')
+      tt.type(tt, 'EventEmitter')
+      tt.end()
+    },
   }
 
   const keys = Object.keys(cases)
@@ -99,11 +275,7 @@ t.test('short output checks', t => {
       const tt = new Test()
       let out = ''
       tt.on('data', c => out += c)
-      tt.on('end', _ => {
-        out = out.replace(
-          / # time=[0-9\.]+m?s( \{.*)?\n/g, ' # {time}$1\n')
-        t.matchSnapshot(out, i)
-      })
+      tt.on('end', _ => t.matchSnapshot(clean(out), i))
       cases[i](tt)
     })
   }

-- 
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