为什么静态查询在Storybook中不起作用? 复制步骤预期结果实际结果环境

问题描述

说明

对于使用StaticQuery的任何组件,我在Storybook中都得到了“正在加载(StaticQuery)”,并且“无法获取此StaticQuery的结果。”在Storybook中查找使用useStaticQuery的任何组件。在本地和构建时,一切似乎都可以正常工作,但是在Storybook中,我得到了这一点:

最低复制量:https://github.com/mrseanbaines/gatsby-storybook-static-query

复制步骤

  1. 克隆存储库([email protected]:mrseanbaines/gatsby-storybook-static-query.git
  2. 运行故事书(yarn storybook

预期结果

组件应呈现

实际结果

我反而报错了

环境

使用gatsby-source-prismic-graphql获取数据。 @prismicio/gatsby-source-prismic-graphql也是如此。

  System:
    OS: macOS 10.15.6
    cpu: (8) x64 Intel(R) Core(TM) i5-8279U cpu @ 2.40GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.17.0 - ~/.nvm/versions/node/v12.17.0/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v12.17.0/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  browsers:
    Chrome: 84.0.4147.125
    Firefox: 79.0
    Safari: 13.1.2
  npmPackages:
    gatsby: 2.24.43 => 2.24.43 
    gatsby-source-prismic-graphql: 3.6.2 => 3.6.2 

解决方法

在项目的 .storybook 文件夹中编辑 main.js,如下所示。

module.exports = {
  "stories": [
    //your stories
    "../src/**/*.stories.mdx","../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],"addons": [
    //your addons
    "@storybook/addon-links","@storybook/addon-essentials","storybook-css-modules-preset"
  ],// This config to exlude gatsby node modules saved me.
  webpackFinal: async config => {
    // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
    config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]
    return config;
  }
}

参考https://www.gatsbyjs.com/docs/how-to/testing/visual-testing-with-storybook/

这样从 gatsby 导入的任何内容都不会破​​坏故事书