简体   繁体   English

GitHub 操作解决了错误的 Node.js 路径

[英]GitHub Actions resolve wrong Node.js path

I am running my tests in Jest and through GitHub actions.我正在 Jest 中运行我的测试,并通过 GitHub 操作。 Unfortunately, it resolves the path incorrect.不幸的是,它解决了不正确的路径。

In my source file , I use the following logic to require a JSON from the same project directory:在我的源文件中,我使用以下逻辑来要求来自同一项目目录的 JSON:

const json = require(require('path').resolve('.') + 'i18n', 'en.json');

In Jest, the path is mocked by jest.mock :在 Jest 中,路径由jest.mock

jest.mock(require('path').resolve('.', 'i18n', 'en.json'), () => {
  return { title: 'English title', paragraph: 'Hello World' };
});

Locally this works fine, but when I push the changes to GitHub actions, the name of project folder is duplicated:在本地这工作正常,但是当我将更改推送到 GitHub 操作时,项目文件夹的名称是重复的:

Cannot find module '/home/runner/work/simple-translator/simple-translator/i18n/en.json' from 'tests/translator.node.test.js'

I think it should just contain simple-translator once.我认为它应该只包含一次simple-translator You can see a live example of the failing workflow here .您可以在此处查看失败工作流的实时示例。 And that's the entire test that fails .就是失败的整个测试

Do you have any idea why the path resolution in GitHub Actions is not working properly?您知道为什么 GitHub Actions 中的路径分辨率无法正常工作吗?

Let me try, though I am not familiar with jest or github CI process...让我试试,虽然我不熟悉 jest 或 github CI 过程......
The mock json file generated by test procedure is at tests/i18n/en.json , while your working process is looking for a path `${moduleHomeRoot}/i18n/en.json` .测试程序生成的模拟 json 文件位于tests/i18n/en.json ,而您的工作进程正在寻找路径`${moduleHomeRoot}/i18n/en.json` I guess you should add '..' into the jest.mock, as:我想你应该在 jest.mock 中添加 '..',如下所示:

jest.mock(require('path').resolve('..', 'i18n', 'en.json'), () => {
  return { title: 'English title', paragraph: 'Hello World' };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM