小编use*_*418的帖子

在 useEffect() 中的状态更改后,Enzyme mount() 重新渲染?

我是 React 和 React 单元测试的新手。我有一个组件,它根据 useState 设置的状态加载一些 div。在 useEffect 内部,如果一个函数返回了一个被拒绝的 promise,那么应该显示一个错误 div。我想测试这个场景,但是因为初始状态设置为 error = false,所以仍然显示原始 div。

我的问题是,我怎样才能做到这一点,以便测试注册更新的 div?我正在使用 Enzyme 的 mount(),但我也尝试过使用 Shallow()。我还验证了它实际上是通过一些 console.logs 进入 .catch() 的。

成分:

export const Main = (props) => { 
  const [showLoadError, setShowLoadError] = useState(false);
  const [loadError, setLoadError] = useState('');

  React.useEffect(() => {
    const { categories, actions } = props;

    // retrieve categories from Db if they don't exist in state
    if (categories.length === 0) {
      actions.loadCategories().catch(err => {
        setShowLoadError(true);
        setLoadError(err.message);
      })
    } else {
      setShowLoadError(false); …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing reactjs jestjs enzyme

5
推荐指数
0
解决办法
3009
查看次数

找不到模块“节点”

我正在尝试通过 package.json 运行测试脚本,

"test": "cross-env NODE_ENV=test nyc mocha --recursive --compilers js:babel-register --require babel-polyfill",
Run Code Online (Sandbox Code Playgroud)

并在运行时出现以下错误:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'C:\Users\aw030085\Desktop\github\diabetesdashboard\node'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Run Code Online (Sandbox Code Playgroud)

我有一个同事可以很好地运行它,但他不确定是否安装了任何额外的东西。我不确定为什么它会抛出错误。

我使用 Windows 10 安装了节点 10.16.2。

javascript node.js

4
推荐指数
1
解决办法
5709
查看次数

标签 统计

javascript ×2

enzyme ×1

jestjs ×1

node.js ×1

reactjs ×1

unit-testing ×1