Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storyshots not working after upgrading to 5.2 (fileName is missing in the context from addon-storyshots-puppeteer) #8272

Closed
mazikwyry opened this issue Oct 2, 2019 · 2 comments

Comments

@mazikwyry
Copy link

Describe the bug
After upgrade to 5.2, all the image snapshot tests are failing. It is because fileName is undefined in context that is exposed from addon-storyshots-puppeteer. I guess it has something to do with a new way we call configure.

To Reproduce
Steps to reproduce the behavior:

  1. Upgrade all packages to 5.2
  2. Change configure to not receive loadStories, but require.context("../packages", true, /.stories.js$/);
  3. Run image snapshot test export APP_ENV=test && yarn docs:build && NODE_ENV=test jest .jest/image_snapshot.test.js --config=.jest/jest.config.js

Expected behavior
All tests pass.

Code snippets
Error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined

       6 |   console.log(Object.keys(context), context.kind, context.story, url);
       7 |   return {
    >  8 |     customSnapshotsDir: `${path.dirname(context.fileName)}/__image_snapshots__`,
         |                                 ^
       9 |     customDiffDir: `${path.dirname(context.fileName)}/__image_snapshots__/diff`,
      10 |     failureThreshold: 0.02,
      11 |     failureThresholdType: "percent",

image_snapshot.test.js

import initStoryshots from "@storybook/addon-storyshots";
import { imageSnapshot } from "@storybook/addon-storyshots-puppeteer";
import path from "path";

const getMatchOptions = ({ context: { fileName } }) => {
  return {
    customSnapshotsDir: `${path.dirname(fileName)}/__image_snapshots__`, //fileName is undefined
    customDiffDir: `${path.dirname(fileName)}/__image_snapshots__/diff`,
    failureThreshold: 0.02,
    failureThresholdType: "percent",
  };
};

const getGotoOptions = () => {
  return {
    waitUntil: "networkidle0",
  };
};

initStoryshots({
  framework: "react",
  suite: "Image storyshots",
  test: imageSnapshot({
    storybookUrl: `file://${path.resolve(__dirname, "../")}/storybook-static`,
    getGotoOptions,
    getMatchOptions,
  }),
});

.storybook/config

import { addDecorator, configure, addParameters } from "@storybook/react";
import { withInfo } from "@storybook/addon-info";
import { withActions } from "@storybook/addon-actions";

import theme from "./theme";
import StoryWrapper from "./storyWrapper";

// Automatically import all files ending in *.stories.js
const req = require.context("../packages", true, /.stories.js$/);

// Apply story wrapper decorator
addDecorator(StoryWrapper);

// You may ask why NODE_ENV and APP_ENV...
// That's all because of
// env: {
//   test: {
//     plugins: ["require-context-hook"],
//   },
// },
// When we run unit tests. We need this (NODE_ENV=test) because otherwise
// it will fail to require stories in node env - `require.context`
// On the other hand - visual tests, running in browser cannot have this plugin
// because it will be handled via webpack. Thus APP_ENV=test for modifying behavior
// of storybook in visual tests.
if (process.env.NODE_ENV === "test" || process.env.APP_ENV === "test") {
  // Apply decorator that displays story info
  addParameters({
    info: {
      disable: true,
    },
  });
} else {
  addDecorator(
    withInfo({
      inline: true,
      styles: {
        infoBody: {
          border: 0,
        },
      },
    }),
  );

  addDecorator(withActions({}));

  addParameters({
    options: {
      theme: theme,
    },
  });
}

configure(req, module);

Babel and Jest are configured with require-context.

System:

Environment Info:

  System:
    OS: Linux 5.0 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
  Binaries:
    Node: 10.16.0 - ~/.asdf/shims/node
    Yarn: 1.16.0 - ~/.asdf/shims/yarn
    npm: 6.9.0 - ~/.asdf/shims/npm
  Browsers:
    Chrome: 77.0.3865.90
    Firefox: 69.0.1
  npmPackages:
    @storybook/addon-actions: ^5.2.1 => 5.2.1 
    @storybook/addon-docs: ^5.2.1 => 5.2.1 
    @storybook/addon-info: ^5.2.1 => 5.2.1 
    @storybook/addon-knobs: 5.2.1 => 5.2.1 
    @storybook/addon-links: ^5.2.1 => 5.2.1 
    @storybook/addon-storyshots: ^5.2.1 => 5.2.1 
    @storybook/addon-storyshots-puppeteer: ^5.2.1 => 5.2.1 
    @storybook/addons: ^5.2.1 => 5.2.1 
    @storybook/codemod: ^5.2.1 => 5.2.1 
    @storybook/react: ^5.2.1 => 5.2.1 
    @storybook/theming: ^5.2.1 => 5.2.1 
@shilman
Copy link
Member

shilman commented Oct 2, 2019

Closing as dupe to #7697

There's a PR for that which is marked "do not merge" .. @Hypnosphi any progress?

@shilman shilman closed this as completed Oct 2, 2019
@Hypnosphi
Copy link
Member

Hypnosphi commented Oct 2, 2019

@shilman tomorrow is a public holiday in Germany so I think I'll be able to pick it up. What needs to be done is to remove the displayName part in favor of #7878 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants