Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

reactjs - Jest cannot find module @env React Native

I'm currently facing some problem I'm using in my react native app https://github.com/goatandsheep/react-native-dotenv for handling .env.

Error => Cannot find module '@env' from 'src/api/api.ts'

I'm testing currently my redux saga to call the api endpoint:

import axios from 'axios';
import {API_URL} from '@env';

export default axios.create({
  baseURL: API_URL,
  responseType: 'json',
});

API Endpoint

export const sendCheckNumber = async (
  phoneNumber: string,
): Promise<AuthenticationTO> => {
  const response = await axios.get<AuthenticationTO>(
    `SendCheckNumber/${phoneNumber}`,
  );

  return response.data;
};

I'm using ts-jest package.json. I saw in the docs its possible to include bable in ts-jest because I'm using bable to import the 'module:react-native-dotenv', as plugin. I thought that will already solve my problem but unfortunately it still fails. Maybe someone of you have some suggestion what could cause this error.

Thank you!!!

package.json

"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
      "\.(ts|tsx)$": "ts-jest"
    },
    "globals": {
      "ts-jest": {
        "babelConfig": "babel.config.js",
        "tsConfig": "tsconfig.jest.json"
      }
    },
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "modulePaths": [
      "<rootDir>/src"
    ],
    "testRegex": "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js)$"
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...