bahmutov / snap-shot

Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
169 stars 3 forks source link

Single promise stack trace is missing #35

Open bahmutov opened 7 years ago

bahmutov commented 7 years ago

See src/async-spec.js test "promise to snapshot"

// async-spec.js
const snapshot = require('.')
it.only('promise to snapshot (does nothing!)', () => {
    // straight into snapshot comparison does not work
    return Promise.resolve(20)
      .then(snapshot)
})

The reported stack does not have "async-spec.js" at all, instead only has

  snap-shot [ { functionName: 'stackSites',
  snap-shot     filename: '/Users/irinakous/git/snap-shot/node_modules/stack-sites/src/index.js',
  snap-shot     line: 14,
  snap-shot     column: 13 },
  snap-shot   { functionName: 'snapshot',
  snap-shot     filename: '/Users/irinakous/git/snap-shot/src/index.js',
  snap-shot     line: 38,
  snap-shot     column: 17 } ] +0ms
bahmutov commented 7 years ago

The code that gets the call site is in https://github.com/bahmutov/snap-shot/blob/57eb06e3b971c155a45e5b688bf7455bd5acea52/src/index.js#L37

function snapshot (what, update) {
  const sites = stackSites()
  if (sites.length < 3) {
    // hmm, maybe there is test (like we are inside Cypress)
    if (this && this.test && this.test.title) {
      debug('no callsite, but have test title "%s"', this.test.title)
      return this.test.title
    }
    debug(sites)
    const msg = 'Do not have caller function callsite'
    throw new Error(msg)
  }

where const stackSites = require('stack-sites') from https://github.com/bahmutov/stack-sites