bahmutov / snap-shot-core

Save / load named snapshots, useful for tests
14 stars 9 forks source link

Making snapshot even more generic #6

Open fernandogmar opened 7 years ago

fernandogmar commented 7 years ago

Hi bahmutov,

I am using tape for my tests (good post: https://medium.com/javascript-scene/why-i-use-tape-instead-of-mocha-so-should-you-6aa105d8eaf4)

I was looking for a library that allows you to work with snapshots no matter your testing framework is, and I was a bit excited after reading this: https://glebbahmutov.com/blog/snapshot-testing/

but it looks like it was not as agnostic as I wish. Just following the code to know if it could be easily hacked I relized that you are using this snap-shot-core.... and you are using the function raiseIfDifferent where actually you compare the values... and do whatever is needed... right?

maybe it could be an option for having this function raiseIfDifferent as a parameter for snapshot... so custom actions can be made... more or less like you did with compare ;)

I would add an example of what I mean here, just to get an idea.

First an example of a test file using tape

import test from 'tape';
import api_core from 'api-core.js';

test('api-core', (assert) => {

      assert.test('api_core.add', (assert) => {
          const actual = api_core.add(2, 1);
          const expected = 3;

          assert.equal(actual, expected, 'should be ' + expected);
          assert.end();
      });
  });

});

following what you did on cypress-system.js

function raiseIfDifferent ({value, expected}) {
  cy.then(() => {
    expect(value).to.equal(expected)
  })
}

The previous test could be written as:

import test from 'tape';
import api-core from 'api-core.js';
import snapshot from 'snap-shot';
import R from 'ramda';//Yes I use ramda too :D

const raiser = R.curry(_raiser);

test('api-core', (assert) => {
      assert.test('api_core.add', (assert) => {
          snapshot(api_core.add(2, 1), raiser(assert))
          assert.end();
      });
  });

function _raiser(assert, {value, expected}) {
   assert.equal(value, expected):
}

});

I hope you get the point, no idea if this is possible or even there are better alternatives, but it would awesome this library would be really agnostic to the testing framework :smile:

What do you think?

bahmutov commented 7 years ago

I see what you mean, and added separate parameters to the snap-shot-core api. See example in https://github.com/bahmutov/snap-shot-core-tape-demo

PS: also love that you use Ramda

fernandogmar commented 7 years ago

Awesome! I am looking forward to seeing what you get :smile: let me know if I can help :wink:

PS: I got Ramda and Kefirjs as a good combo

bahmutov commented 7 years ago

I know! https://glebbahmutov.com/blog/ramda-for-reactive-streams/

BTW, the changes have been pushed, but I'm thinking what if you could pass assert.equal function directly into snap-shot-core? then snap-shot-core would fetch value from snapshot and if found just call your assert.equal, making the test runner do its work. It would look like this

const {test} = require('tape')
const snapshot = require('snap-shot-core')

test('a test', (assert) => {
  snapshot({
    what: 43,
    __filename,
    specName: assert.name,
    equal: assert.equal
  })
  assert.end()
})
fernandogmar commented 7 years ago

Cool! and smart solution! that is only for snap-shot-core, right? Now I am guessing how this could be used directly with snap-shot, I think it is going to be bit more difficult to get it working for example with data-driven... :thinking:

snapshot(isPrime, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Using just snap-shot-core.... I am thinking out loud... for now I just can do something like...?

const {test} = require('tape')
const snapshot = require('snap-shot-core')

test('a test', (assert) => {
    [1, 2, 3, 4, 5, 6, 7, 8, 9].map((value) => {
      snapshot({
        what: isPrime(value),
        __filename,
        specName: assert.name,
        equal: assert.equal
      })
    })

  assert.end()
})

BTW, I have been playing with https://github.com/bahmutov/snap-shot-core-tape-demo ;), and I saw your changes on your commit. Really fast work ;)

Another thing, What it is __filename ? UPDATE: ok I got it https://nodejs.org/api/globals.html#globals_filename

Thanks for all!