snap-shot icon indicating copy to clipboard operation
snap-shot copied to clipboard

Unexpected snapshot id when using with Sazerac

Open mikec opened this issue 8 years ago • 6 comments

I used snapshot within a sazerac assertion like this:

import todos from './todos'
import snapshot from 'snap-shot'
import { test, given } from 'sazerac'

test(todos, () => {

  given([], {
    type: 'ADD_TODO',
    text: 'Run the tests',
    id: 0
  })
  .assert('should return the snapshotted value', (newState) => {
    snapshot(newState)
  })

})

and the resulting snap-shot file after first test run ends up looking like:

// describer.js.snap-shot
exports['4b900bc2e67514543c299092313fe2b17fdd51c9f059973b12b7e56d702c32a1 1'] = [
  {
    "id": 0,
    "text": "Run the tests",
    "completed": false
  }
]

not sure where it's getting describer and the random hash from, might actually be a sazerac issue.

sort of related to #38

mikec avatar Feb 23 '17 19:02 mikec

Hi!

snap-shot could not find the right test(...) wrapper here and just found the found wrapping the snapshot call. In this case it was an arrow function

(newState) => {
    snapshot(newState)
 })

When it cannot find the named test, it will use SHA of the wrapping function https://github.com/bahmutov/snap-shot#tests-with-dynamic-names

The best thing here to support the above test case would be to detect the full function

.assert('should return the snapshotted value', (newState) => {
    snapshot(newState)
})

and take the assertion text as the function name "should return the snapshotted value"

bahmutov avatar Feb 23 '17 20:02 bahmutov

speaking of "describer", it seems I do not discard the stack entries for it

snap-shot 33 callsite(s) +0ms
  snap-shot 
  snap-shot     file: /Users/gleb/git/snap-shot-jest-test/node_modules/sazerac/lib/describer.js
  snap-shot     line: 64,
  snap-shot     column: 3
  snap-shot    +1ms

bahmutov avatar Feb 23 '17 20:02 bahmutov

@bahmutov dug into it a bit and noticed that it's ignoring a line in the stack trace because of this check https://github.com/bahmutov/stack-sites/blob/master/src/index.js#L6

stack trace looks like:

__proto__:Array[0]
0:"    at stackSites (/Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/node_modules/stack-sites/src/index.js:14:13)"
1:"    at snapshot (/Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/node_modules/snap-shot/src/index.js:54:17)"
2:"    at /Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/src/reducers/todos.spec.js:13:54"
3:"    at assertionExecuter (/Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/node_modules/sazerac/lib/describer.js:64:3)"
4:"    at Object.<anonymous> (/Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/node_modules/sazerac/lib/describer.js:47:9)"
...

it's ignoring the at /Users/mfcalvanese/dev/bahmutov/sazerac-example/todos/src/reducers/todos.spec.js:13:54" because it doesn't have ( and ), and thus getting the name for the file as the next one describer.js rather than todos.spec.js as I was expecting

maybe hasLineInfo should check for the filename and line/char numbers instead of parens?

mikec avatar Feb 23 '17 20:02 mikec

also, haven't looked into the other issue, but is there any way to look up the stack trace for an it() call specifically? I think right now snapshot assumes that it's because executed directly within an it function, but that's not the case here.

mikec avatar Feb 23 '17 20:02 mikec

yeah, all good test cases that I need to handle properly, especially the parsing. How did you get the stack trace? I want to open the issue in https://github.com/bahmutov/stack-sites

bahmutov avatar Feb 23 '17 20:02 bahmutov

got the stack trace debugging on https://github.com/bahmutov/stack-sites/blob/master/src/index.js#L13

mikec avatar Feb 23 '17 21:02 mikec