tape-testing / tape

tap-producing test harness for node and browsers
MIT License
5.77k stars 307 forks source link

"at:" missing from diagnostic when file path starts with "file://" #601

Closed tbeseda closed 7 months ago

tbeseda commented 10 months ago

Here's a sample of tape output without the at: entry in the diagnostics YAML The 4th line of the "stack" block should also be in the YAML as "at"

not ok 10 A failing string equal
  ---
    operator: equal
    expected: 'Good dog'
    actual:   'Bad dog'
    stack: |-
      Error: A failing string equal
          at Test.assert [as _assert] (/my-project/node_modules/tape/lib/test.js:479:48)
          at Test.strictEqual (/my-project/node_modules/tape/lib/test.js:643:7)
          at Test.<anonymous> (file:///my-project/test/tap/make-tap.js:51:5)
          at Test.run (/my-project/node_modules/tape/lib/test.js:113:28)
          at Immediate.next [as _onImmediate] (/my-project/node_modules/tape/lib/results.js:157:7)
          at process.processImmediate (node:internal/timers:476:21)
  ...

I believe it's this loop that sets the Test.at https://github.com/ljharb/tape/blob/5a776572cb7dcf46e32c493db716579e3bc5dc5d/lib/test.js#L523-L525

I'm hesitant to wade into that regex 😬 but I can make an attempt if it's not an apparent/easy fix

🍻

ljharb commented 10 months ago

I'm not sure how to turn this into a failing test case. Can you provide a .js file, and the expected tape output from it?

ljharb commented 7 months ago

@tbeseda ping, i'd love to address this

tbeseda commented 7 months ago

Apologies, I've been neglecting my GH notifications. I'll work on a repro you can run and update here.

tbeseda commented 7 months ago

Looking at this again, I realized it's related to executing tests in an ESM environment.

with CJS

const test = require('tape')

test('test', function (t) {
  t.plan(1)
  t.equal('foobaz', 'foobar')
})

I get the expected output with the at: line

TAP version 13
# test
not ok 1 should be strictly equal
  ---
    operator: equal
    expected: 'foobar'
    actual:   'foobaz'
    at: Test.<anonymous> (/project-path/index.cjs:5:5)
    stack: |-
      Error: should be strictly equal
          at Test.assert [as _assert] (/project-path/node_modules/tape/lib/test.js:479:48)
          at Test.strictEqual (/project-path/node_modules/tape/lib/test.js:661:7)
          at Test.<anonymous> (/project-path/index.cjs:5:5)
          at Test.run (/project-path/node_modules/tape/lib/test.js:113:28)
          at Immediate.next [as _onImmediate] (/project-path/node_modules/tape/lib/results.js:156:7)
          at process.processImmediate (node:internal/timers:476:21)
  ...

1..1
# tests 1
# pass  0
# fail  1

with MJS

import test from 'tape'

test('test', function (t) {
  t.plan(1)
  t.equal('foobaz', 'foobar')
})

I don't get the at: line, but there is a nice stack:

TAP version 13
# test
not ok 1 should be strictly equal
  ---
    operator: equal
    expected: 'foobar'
    actual:   'foobaz'
    stack: |-
      Error: should be strictly equal
          at Test.assert [as _assert] (/project-path/node_modules/tape/lib/test.js:479:48)
          at Test.strictEqual (/project-path/node_modules/tape/lib/test.js:661:7)
          at Test.<anonymous> (file:///project-path/index.mjs:5:5)
          at Test.run (/project-path/node_modules/tape/lib/test.js:113:28)
          at Immediate.next [as _onImmediate] (/project-path/node_modules/tape/lib/results.js:156:7)
          at process.processImmediate (node:internal/timers:476:21)
  ...

1..1
# tests 1
# pass  0
# fail  1

this file:/// line has the location of my failing assertion

at Test.<anonymous> (file:///project-path/index.mjs:5:5)
ljharb commented 7 months ago

aha, yes i'm sure that it's due to the regex expecting paths, while native ESM in node unfortunately uses URLs. I'll see what's possible - an actually correcct regex for a URL is probably impossible, but perhaps since this can only happen in a node that has a native URL, I can use that when available.