shelfio / jest-mongodb

Jest preset for MongoDB in-memory server
MIT License
587 stars 83 forks source link

Jest: Got error running globalSetup - First run works - After Monorepo + Babel #383

Open AlessandroVol23 opened 1 year ago

AlessandroVol23 commented 1 year ago

Hi 👋

I've got the following issue.

I have a jest / TS setup with this MongoDB preset running. For another library, I need to add babel and transform 2 libraries that they are working out. This breaks my MongoDB setup, unfortunately.

The first run always works successfully but the second one isn't. I get the following error:

TypeError: Jest: Got error running globalSetup - /Users/alessandrovolpicella/hashnode/repo/aws-
infrastructure/node_modules/.pnpm/@shelf+jest-mongodb@4.0.0_73bw6e4ycsfpa6zw336lwl3yni/node_modules/@shelf/jest-
mongodb/setup.js, reason: /Users/alessandrovolpicella/hashnode/repo/aws-infrastructure/node_modules/.pnpm/safe-
buffer@5.1.2/node_modules/safe-buffer/index.js: Cannot read properties of undefined (reading 'from')

Note: This whole setup worked in a normal repository. Now we have a monorepo and it doesn't work anymore. I have no idea how this plays together tbh.

Here are some of my config files:

// ./apps/infrastructure/jest.config.ts
import tsPreset from 'ts-jest/jest-preset';
import { pathsToModuleNameMapper } from 'ts-jest';
import type { Config } from '@jest/types';

import { compilerOptions } from './tsconfig.json';

const config: Config.InitialOptions = {
  ...tsPreset,
  preset: '@shelf/jest-mongodb',
  globalSetup: '<rootDir>/test/config/globalSetup.ts',
  setupFilesAfterEnv: ['<rootDir>/test/config/setupAfterEnv.ts'],
  testEnvironment: 'node',
  testMatch: ['**/*.test.ts'],
  modulePaths: [compilerOptions.baseUrl],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
  watchPathIgnorePatterns: ['globalConfig'],
  transformIgnorePatterns: [
    `<rootDir>/apps/infrastructure/node_modules/(?!aws-testing-library|filter-obj)`
  ],
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
    '^.+\\.tsx?$': 'ts-jest'
  }
};

export default config;

apps/infrastructure/test/config/globalSetup.ts

import mongoSetup from '@shelf/jest-mongodb/setup';

export default async function setup() {
  await mongoSetup();
  Object.assign(process.env, {
    AWS_REGION: 'us-west-2',
    ENVIRONMENT_NAME: 'prod',
    DB_CONNECTION_STRING_SECRET_ID: 'db-connection-string-secret-id',
    DISCORD_TOPIC_ARN: 'discord-topic-arn',
    IDEMPOTENCY_TABLE_NAME: 'idempotency-table-name',
    SQS_QUEUE_URL: 'sqs-queue-url',
    bucketAudioBlogs: 'bucket-audio-blogs'
  });
}

apps/infrastructure/babel.config.js

module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-typescript'
  ]
};

Reproduce first run

If I want to reproduce the one successful test

  1. Comment out preset: '@shelf/jest-mongodb', in jest.config.ts
  2. jest
  3. Test successful
  4. Run test again
  5. Fails

Do I even need the preset here?

If I comment it in again it works one time and fails the second one.

If I put the whole global setup into the setup after the environment it behaves the same.

I think it has something to do with:

I added this in the repo before it was a monorepo and all worked fine.

I need babel because of this issue https://github.com/erezrokah/aws-testing-library/issues/635#issuecomment-1204851257

I'm happy for any help 🙏