Skip to content

Performance regression introduced in 10.2.0 #1438

@olivierlacan

Description

@olivierlacan

Expected Behavior

Prior to 10.2.0 our mocha test suite would boot and complete in roughly 20 seconds:

$ time NODE_CONFIG_ENV=test mocha 'src/**/*.spec.{ts,tsx}'
Node.js version is 14.16.1 (LTS)
Executing: "NODE_CONFIG_ENV=test mocha 'src/**/*.spec.{ts,tsx}'"
  ..............
  14 passing (5s)

real	0m12.147s
user	0m7.416s
sys	0m1.901s

Actual Behavior

After upgrading to 10.2.0 from 10.1.0 with no other changes to our tests or mocha itself:

time NODE_CONFIG_ENV=test mocha 'src/**/*.spec.{ts,tsx}'
Node.js version is 14.16.1 (LTS)
Executing: "NODE_CONFIG_ENV=test mocha 'src/**/*.spec.{ts,tsx}'"
  ..............
  14 passing (5s)

real	1m13.431s
user	0m56.354s
sys	0m16.190s

Steps to reproduce the problem

tsconfig:

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "jsx": "react",
    "lib": ["ES2020", "DOM"],
    "noEmit": true,
    "types": ["node", "mocha", "@our-namespace/types"],
    "resolveJsonModule": true
  },
  "exclude": ["node_modules", "dist"]
}

Mocha command:

NODE_CONFIG_ENV=test mocha 'src/**/*.spec.{ts,tsx}'

Mocha config in package.json:

"mocha": {
    "enable-source-maps": true,
    "extension": [
      "ts",
      "tsx"
    ],
    "reporter": "dot",
    "require": [
      "ignore-styles",
      "ts-node/register/transpile-only",
      "source-map-support/register",
      "global-jsdom/register",
      "@testing-library/react/dont-cleanup-after-each",
      "./ad-hocs/mocha/global-setup.ts",
      "./ad-hocs/mocha/hooks.ts"
    ],
    "slow": 4000,
    "timeout": 5000
  },

Contents of mocha helper files:

// ad-hocs/mocha/global-setup.ts

import { AppConfig } from "@our-namespace/types";
import { configure } from "@testing-library/react";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
import chaiDom from "chai-dom";
import chaiSubset from "chai-subset";
import config from "config";
import "cross-fetch/polyfill";
import { Settings } from "luxon";
import { setupServer } from "msw/node";
import sinonChai from "sinon-chai";

chai.use(sinonChai);
chai.use(chaiSubset);
chai.use(chaiAsPromised);
chai.use(chaiDom);

const appConfig = config.get<AppConfig>("appConfig");
global.AppConfig = appConfig;

// Configure luxon timezone
Settings.defaultZoneName = "America/New_York";

// Needs to be done once and shared because of how it hooks into the Node.js process.
// If this changes, it could be placed elsewhere. Here is an issue to track:
// https://github.com/mswjs/msw/issues/474
global.server = setupServer();

// This helps with performance. Locally it shaves ~2m off of our test suite time (424 tests).
// https://github.com/testing-library/dom-testing-library/issues/820#issuecomment-727006528
configure({
  defaultHidden: true,
});
// ad-hocs/mocha/hooks.ts

import { cleanup } from "@testing-library/react";
import { RootHookObject } from "mocha";
import sinon from "sinon";

export const mochaHooks: RootHookObject = {
  beforeAll() {
    global.server.listen({
      onUnhandledRequest: "error",
    });
  },
  afterAll() {
    global.server.close();
  },
  afterEach() {
    // https://sinonjs.org/releases/v9.1.0/general-setup/
    sinon.restore();

    global.server.resetHandlers();

    // We shoudn't have to do this manually but the auto cleanup doesn't seem to be working.
    // So we have @testing-library/react/dont-cleanup-after-each in the require section
    // of the mocha config in package.json and this manual call here.
    // If we figure out the issue, those should be able to be removed.
    // https://testing-library.com/docs/react-testing-library/api/#cleanup
    cleanup();
  },
};

Minimal reproduction

Running out of work time for a repro repo today but if it's really necessary I'll send one later.

Specifications

  • ts-node version: 4.2.0
  • node version: 14.15.0
  • TypeScript version: 4.3.5
  • tsconfig.json, if you're using one: supplied above
{}
  • Operating system and version: macOS 11.5.1 (20G80)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions