Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlessandroVol23 opened this issue Aug 31, 2022 · 0 comments

Comments

@AlessandroVol23
Copy link

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/@[email protected]_73bw6e4ycsfpa6zw336lwl3yni/node_modules/@shelf/jest-
mongodb/setup.js, reason: /Users/alessandrovolpicella/hashnode/repo/aws-infrastructure/node_modules/.pnpm/safe-
[email protected]/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:

  • Babel
  • Monorepo

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

I need babel because of this issue erezrokah/aws-testing-library#635 (comment)

I'm happy for any help 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant