reactjs - "Missing the cookie header or object"在 react 烟雾测试期间抛出错误(Create-react-app)

标签 reactjs cookies redux jestjs

我使用 universal-cookie 存储在本地存储中,然后通过管道传输到存储中。

class App extends Component {

  componentDidMount() {
    // if a cookie is present, save the value in the store for use communication 
    // with the server. If the cookie is undefined, the user is redirected to the login page.
    // Redirection is handled by router.
    const userNameInCookie = cookies.get('userName');
    if (userNameInCookie) {
        this.props.dispatch(actions.setUserNameFromCookie(userNameInCookie));
    }
  }

  render() {
    return (
        <div>
            <div className="header">
                <h2 className="header-title">Traveler</h2>
            </div>
            {this.props.children}
        </div>
    );
  }
}

当我运行 Jest 测试套件时,每个测试都会失败并出现此错误

FAIL  src/test/UserCreateForm.test.js
● Test suite failed to run

Missing the cookie header or object

  at new Cookies (node_modules/universal-cookie/lib/Cookies.js:35:15)
  at Object.<anonymous> (src/actions/index.js:4:17)
  at Object.<anonymous> (src/reducers/index.js:1:258)
  at Object.<anonymous> (src/store.js:4:40)
  at Object.<anonymous> (src/test/UserCreateForm.test.js:7:40)
  at handle (node_modules/worker-farm/lib/child/index.js:41:8)
  at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
  at emitTwo (events.js:106:13)
  at process.emit (events.js:194:7)
  at process.nextTick (internal/child_process.js:766:12)
  at _combinedTickCallback (internal/process/next_tick.js:73:7)
  at process._tickCallback (internal/process/next_tick.js:104:9)

我也尝试过使用 Enzyme 进行烟雾测试,但遇到了同样的错误。该代码的行为完全符合我的要求,因此我相信 universal-cookie 根本无法与测试配合。

有什么想法吗? 谢谢!

最佳答案

你导入了universal-cookie然后定义了类吗?

import Cookies from 'universal-cookie';
const cookies = new Cookies();

所以你的代码看起来是......

import Cookies from 'universal-cookie';

class App extends Component {
  componentDidMount() {
    const cookies = new Cookies();
    const userNameInCookie = cookies.get('userName');
    if (userNameInCookie) {
      this.props.dispatch(actions.setUserNameFromCookie(userNameInCookie));
    }
  }

  render() {
    return (
      <div>
        <div className="header">
          <h2 className="header-title">Traveler</h2>
        </div>
        {this.props.children}
      </div>
    );
  }
}

关于reactjs - "Missing the cookie header or object"在 react 烟雾测试期间抛出错误(Create-react-app),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44034753/

相关文章:

javascript - 如何在 React Native 上设置 Slider 的样式

java - jsessionid传入url,如果浏览器保存cookie和jsessionid

swift - 如何使用 Swift 将 Parse 安装 objectId(或 devicetoken)保存为 cookie

javascript - 通过复选框在 Redux 状态中切换 True 和 False

javascript - 对于 redux reducer 来说哪个更快 : switch or map?

javascript - 如何调度多个 action creator(React + Redux + 服务端渲染)

reactjs - 创建 react 应用程序: Range error - Maximum call stack size exceeded

reactjs - 仅在 onChange 停止触发设定时间后,如何使 React 输入 onChange 设置状态?

javascript - React-native 异步获取返回 null

php - $_ENV 、 $_SESSION 和 $_COOKIE 之间有什么区别