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 hangs with multiple config files in different testing directories #352

Open
canpan14 opened this issue Jun 13, 2022 · 1 comment
Open

Comments

@canpan14
Copy link

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

  • ROOT
    • jest.config.ts
    • root.test.ts
    • CHILD-ONE
      • jest.config.ts
      • jest-mongodb-config.js
      • childOne.test.ts
    • CHILD-TWO
      • jest.config.ts
      • jest-mongodb-config.js
      • childOne.test.ts

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:

  • Only having one jest-mongodb-config.js at the root
    • Fails because root/childOne/childTwo are all scoped within their own folders so they can't reference the one at the root level
  • Setting different 'instance' names or leaving 'instance' empty
  • No change
  • Toggling useSharedDBForAllJestWorkers true/false
    • No change

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
Copy link

same issue here.

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

2 participants