简体   繁体   English

Jest 找不到模块“配置”

[英]Jest cannot find module 'config'

I'm using Jest to test my api calls file, when I run a simple test I've got an error Cannot find module 'config' from 'api.service.js' which is an import at the top of my api.service.js file:我正在使用 Jest 来测试我的 api 调用文件,当我运行一个简单的测试时,我遇到了一个错误无法找到模块 'config' from 'api.service.js' 这是我 api.service 顶部的一个导入.js 文件:

//api.service.js    
import config from "config"; //get data from my webpack.config.js
export const apiService = {getmyData};
function getmyData() {
  let queryString = `${config.API}/api/datacall`;

  return new Promise(
    (resolve, reject) => {
      client.get(`${config.API}/api/datacall`).then(result => {
        return resolve(result );
      });
    },
    error => {
      return reject(error);
    }
  );
}

Test File in the same folder as the above测试文件在与上面相同的文件夹中

//api.spec.js
import getmyDatafrom "./api.service";

const domain = "http://fakeapi.com";

const mockedConsolelog = jest.spyOn(global.console, "log");
const mockedConsoleerror = jest.spyOn(global.console, "error");

Why can't my test file get to my config like the api.service.js?为什么我的测试文件不能像 api.service.js 那样进入我的配置?

You probably have to do the import with a relative path.您可能必须使用相对路径进行导入。

import config from "./config";

Otherwise, it tries to import something from an npm module called config.否则,它会尝试从名为 config 的 npm 模块导入某些内容。

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

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