6

when trying to spin up a server using Jest, I keep getting the error: Cannot find module 'services/middleware'.

In this case, services/middleware is located in src/services/middleware.

I have set the moduleNameMapper option in the jest config, so I'm not sure what is still causing the error.

webpack.config.js

module.exports = (env) => {
  return {
    mode: MODE,
    externalsPresets: { node: true },
    externals: [nodeExternals()],
    node: {
      global: true,
      __filename: false,
      __dirname: false,
    },
    watch: !isProduction,
    resolve: {
      alias: {
        services: path.resolve(__dirname, 'src', 'services'),
      },
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          loader: 'swc-loader',
        },
      ],
    },
  };
};

and

jest.config.js

module.exports = {
  testEnvironment: 'node',
  transform: {
    '^.+\\.(t|j)sx?$': '@swc/jest',
  },
  rootDir: '.',
  moduleDirectories: ['node_modules', 'src'],
  globalSetup: './tests/config/globalSetup.js',
  globalTeardown: './tests/config/globalTeardown.js',
  moduleNameMapper: {
    '^services(.*)$': '<rootDir>/src/services$1',
  },
  moduleFileExtensions: ['js', 'jsx'],
};
juliomalves
  • 42,130
  • 20
  • 150
  • 146
bbenson09
  • 123
  • 1
  • 6

0 Answers0