shelfio / jest-mongodb

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

Jest hangs with multiple config files in different testing directories #352

Open canpan14 opened 2 years ago

canpan14 commented 2 years ago

Using v2.2.2 on TS v4.6.2 with node v16 I have folder structure like the following:

The root jest.config.ts is setup to call it's own tests and then point at both children jest.configs.ts files so they can kick themselves off. This works file and all the tests pass except the tests hang at the end. It causes an issue because even though they all pass, the testing stage of our build cycle never finishes because they hang, causing the build to fail eventually.

If I stop one of the children from running tests or remove all of their mongodb-config setup then everything works fine. Clearly either something isn't closing out or the in memory mongodb's are colliding in some weird way.

Things I tried:

Root jest.config.ts

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  preset: 'ts-jest',
  reporters: [
    'default',
    ['jest-junit', { outputDirectory: 'test-reports/' }],
    [
      './node_modules/jest-html-reporter',
      {
        pageTitle: 'CDK Test Report',
        includeFailureMsg: true,
        includeConsoleLog: true,
      },
    ],
  ],
  testEnvironment: 'node',
  coveragePathIgnorePatterns: ['__tests__/util', 'config/'],
  testPathIgnorePatterns: ['__tests__/util', 'config/'],
  coverageDirectory: 'test-reports/',
  projects: [
    '<rootDir>/jest.config.ts',
    '<rootDir>/functions/childOne/jest.config.ts',
    '<rootDir>/functions/childTwo/jest.config.ts',
  ],
  testMatch: ['<rootDir>/test/*.test.ts'],
};
export default config;   

Child jest.config.ts (other is a duplicate)

const mongodbPreset = require('@shelf/jest-mongodb/jest-preset');
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  ...mongodbPreset,
  preset: 'ts-jest',
  reporters: ['default', ['jest-junit', { outputDirectory: 'test-reports/' }]],
  coveragePathIgnorePatterns: ['__tests__/util'],
  testPathIgnorePatterns: ['__tests__/util'],
  coverageDirectory: 'test-reports/',
  transform: {
    '\\.pem$': './jest.filepath.transformer.js',
    '^.+\\.(ts|tsx)?$': 'ts-jest',
  },
  projects: ['<rootDir>/jest.config.ts'],
  testMatch: ['<rootDir>/test/*.test.ts'],
};
export default config;

jest-mongodb-config.ts (both are the same except instance name)

module.exports = {
  mongodbMemoryServerOptions: {
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    instance: {
      dbName: 'jest',
    },
    autoStart: false,
  },
};

Example code setup

describe(‘Run my tests', () => {
  let mongoClient: MongoClient;
  let db: Db;
  const OLD_ENV = process.env;

  beforeAll(async () => {
    mongoClient = await new MongoClient(global.__MONGO_URI__).connect();
    db = mongoClient.db();
  });
  beforeEach(async () => {
    process.env = {...OLD_ENV};
    // some sensitive env variables remove that we set during testing
  });
  afterEach(() => {
    jest.restoreAllMocks();
  })
  afterAll(async () => {
    await mongoClient.close();
    process.env = OLD_ENV;
  });
}
mr-pinzhang commented 1 year ago

same issue here.