6

i have created a jest test file below.But the snapshot for this file is not created. What is the issue in my code?

import React from 'react';
import Carousel from './component';
import renderer from 'react-test-renderer';

test('Carousel Component Test Suite', () => {

const component = renderer.create(
    <Carousel
       />
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();

});
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
sheeba
  • 61
  • 1
  • 2
  • Are you getting any errors? Have you done a `console.log` on `tree` to see if it has a value? I cannot see anything particularly wrong in the code. Have you run with `--u` as an argument as that is what updates the snapshot file? – Dan Mason Aug 18 '17 at 11:14
  • In console,nothing is printed. Error is cannot read property "0" of undefined. slideIndex is initialised as 0 in the component. – sheeba Aug 18 '17 at 12:03
  • Not the problem in this case but a .snap file won't be generated unless the test calls toMatchSnapshot or similar. – tschumann Dec 10 '18 at 00:06

2 Answers2

9

You have to update the test case. Fire the command below in command prompt/terminal etc.

npm test -u YourTest.spec.js

Do this in project root directory.

Hemant Sankhla
  • 923
  • 12
  • 23
1

I ran it locally (swapping in a simple component) and it generated a .snap file.

As far as I can see everything looks fine but it may be a problem with a specific version of a dependency you're using (but without seeing your package.json it's impossible to say).

tschumann
  • 2,776
  • 3
  • 26
  • 42