没有链接这样的模块:electron_common_features - 电子反应测试失败

分享于2022年11月15日 electron reactjs testing typescript 问答
【问题标题】:No such module was linked: electron_common_features - Electron react test failing没有链接这样的模块:electron_common_features - 电子反应测试失败
【发布时间】:2022-08-22 01:03:28
【问题描述】:

我从 electron-react-boilerplate 创建了一个反应电子应用程序。并安装了 @electron/remote 包。也改变了测试用例。现在,当我运行 npm test 时,它会显示 No such module was linked: electron_common_features 。该应用程序运行良好!

测试文件:-

import \'@testing-library/jest-dom\';
import { render, screen } from \'@testing-library/react\';
import App from \'../renderer/App\';

describe(\'App\', () => {
  it(\'should render\', () => {
    render();
    const windowElem = screen.findByTestId(\'window\');

    expect(windowElem).toBeInTheDocument();
  });
});

日志:-

 FAIL  src/__tests__/App.test.tsx
  ● Test suite failed to run

    No such module was linked: electron_common_features

      at Object.getElectronBinding (node_modules/@electron/remote/dist/src/common/get-electron-binding.js:6:24)
      at Object. (node_modules/@electron/remote/dist/src/common/module-names.js:44:41)
      at Object. (node_modules/@electron/remote/dist/src/renderer/remote.js:7:24)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        10.665 s
Ran all test suites.

来自 package.json 的版本:-

不是所有的包都列出来了!!

    \"detect-port\": \"^1.3.0\",
    \"electron\": \"^16.0.5\",
    \"electron-builder\": \"22.13.1\",
    \"electron-devtools-installer\": \"^3.2.0\",
    \"electron-notarize\": \"^1.1.1\",
    \"electron-rebuild\": \"^3.2.5\",
    \"jest\": \"^27.4.5\",
    \"ts-jest\": \"^27.1.2\",
    \"ts-loader\": \"^9.2.6\",
    \"ts-node\": \"^10.4.0\",
    \"typescript\": \"^4.5.4\",
    \"url-loader\": \"^4.1.1\",
    \"webpack\": \"^5.65.0\",
    \"webpack-bundle-analyzer\": \"^4.5.0\",
    \"webpack-cli\": \"^4.9.1\",
    \"webpack-dev-server\": \"^4.7.1\",
    \"webpack-merge\": \"^5.8.0\"
    \"7.css\": \"^0.5.0\",
    \"@electron/remote\": \"^2.0.1\",
    \"electron-debug\": \"^3.2.0\",
    \"electron-log\": \"^4.4.4\",
    \"electron-updater\": \"^4.6.4\",
    \"history\": \"^5.2.0\",
    \"react\": \"^17.0.2\",
    \"react-dom\": \"^17.0.2\",
    \"react-router-dom\": \"^6.2.1\",
    \"regenerator-runtime\": \"^0.13.9\",
    \"screenfull\": \"^6.0.1\"

    // engines
    \"node\": \">=14.x\",
    \"npm\": \">=7.x\"
  • 你好,我也有同样的错误,如果你找到任何解决方法,请告诉我。谢谢。
  • @AymenFezai 还没有!但是 I created an issue on GitHub (electron/remote#101) (不完全是这个问题),我认为解决这个问题也许也能解决这些错误!
  • 你能找到任何解决方法吗?
  • @PrajwalKulkarni 如果您仍然坚持这一点,就我而言,问题出在“@electron/remote”,所以我在 useEffect 中调用它。这是目前的解决方案,直到我找到更好的解决方案。我正在使用“Nextron”,顺便说一句。

【解决方案1】:

如果您的情况和我相似,经过多次试验我有解决方案。在测试您的应用程序之前输入以下代码:

jest.mock('@electron/remote', () => ({ exec: jest.fn() }));

您可以访问更多详细信息 https://github.com/electron-react-boilerplate/electron-react-boilerplate/issues/3295

【讨论】: