suchipi / chai-jest-snapshot

Chai assertion that provides Jest's snapshot testing
MIT License
103 stars 17 forks source link

matchSnapshot not creating .snap files #24

Open madhu-guruprasad opened 7 years ago

madhu-guruprasad commented 7 years ago

I am using this package with Mocha, and setting a snap files explicitly using setFilename. When I run the tests, it shows as successful every time (even after changes). However, there are no snapshot files created anywhere in the directory structure. Here's my setup - I have a test_helper.js file -

const jsdom = require('jsdom');
const chai = require('chai');
const chaiImmutable = require('chai-immutable');
const chaiJestSnapshot = require('chai-jest-snapshot');
const React = require('react');

const doc = new jsdom.JSDOM('<!doctype html><html><body></body></html>');
const win = doc.window;

global.document = win.document;
global.window = win;

// Attach everything to the global object
if(window) {
  Object.keys(window).forEach((key) => {
    if (!(key in global)) {
      global[key] = window[key];
    }
  });
}

chai.use(chaiImmutable);
chai.use(chaiJestSnapshot);

// You must use this Wrapper with functional react components
class Wrapper extends React.Component {
  render() {
    return this.props.children;
  }
}

global.Wrapper = Wrapper;

A sample test file -

beforeEach(function(done) {
  chaiJestSnapshot.configureUsingMochaContext(this);
  chaiJestSnapshot.setFilename('test/actions/shared.snap');
  done();
});

describe('actions/shared', function() {
  it('passes the right rowIndex and type in openSettingsMenu', function() {
    const rowIndex = 12;
    const action = openSettingsMenu(rowIndex);
    expect(action).to.matchSnapshot();
  });
})

I'm compiling using webpack. This is the npm script - "mocha-webpack --webpack-config webpack.test.config.js \"src/**/*.test.js\" --require 'test/test_helper.js'"

suchipi commented 7 years ago

Could you post the contents of your webpack config and babelrc?

If you could create a repo with these files that I could npm install and npm test to see the issue, that would be even better.

suchipi commented 7 years ago

The issue might have to do with you using configureUsingMochaContext and setFilename together; I don't have any tests covering that use-case (but if it doesn't work, it should be changed to work). I'll investigate this weekend.

marie-hughes commented 6 years ago

Is there an update on this issue? It's impacting our project rather significantly

suchipi commented 6 years ago

Hi @marie-hughes, are you using configureUsingMochaContext together with setFilename? If you are testing using Mocha, it is recommended to either use configureUsingMochaContext on its own (without setFilename), or use setFilename and setTestName together (without configureUsingMochaContext). Does using the library in either of those configurations resolve your issue?