chai-jest-snapshot icon indicating copy to clipboard operation
chai-jest-snapshot copied to clipboard

matchSnapshot not creating .snap files

Open madhu-guruprasad opened this issue 8 years ago • 4 comments

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'"

madhu-guruprasad avatar Jul 19 '17 23:07 madhu-guruprasad

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 avatar Jul 20 '17 15:07 suchipi

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.

suchipi avatar Jul 20 '17 15:07 suchipi

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

marie-hughes avatar Feb 07 '18 23:02 marie-hughes

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?

suchipi avatar Feb 08 '18 04:02 suchipi