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

Performance regression introduced in 10.2.0 #1438

Closed
olivierlacan opened this issue Aug 17, 2021 · 5 comments
Closed

Performance regression introduced in 10.2.0 #1438

olivierlacan opened this issue Aug 17, 2021 · 5 comments
Milestone

Comments

@olivierlacan
Copy link

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)
@cspotcode
Copy link
Collaborator

"require": [
    // ...
    "source-map-support/register",

ts-node has always registered source-map-support automatically, and in 10.2.0 we switched to a fork that should have better performance, features, fixed a few bugs. See the release notes

Perhaps doubling-up, loading 2x instances of source-map-support at the same time, is causing issues.

@olivierlacan
Copy link
Author

Yep, looks like that was it. After bumping back to 10.2.0 and removing "source-map-support/register", from mocha requires config we're back to normal:

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

  14 passing (1s)

real	0m7.713s
user	0m5.933s
sys	0m1.412s

If ts-node is automatically registering this, is it not possible to warn if an attempt is made to register it again. Seems like a fairly hard to debug issue otherwise.

@cspotcode
Copy link
Collaborator

We're using a fork, so technically it's a different library. Feel free to propose a pull request with the warning behavior you would like to see.

@olivierlacan
Copy link
Author

@cspotcode Do you mind pointing me to where the fork is localized in the codebase or elsewhere? I'm not sure I'll have time to tackle this but it feels worthwhile if anybody ever happens upon this thread with similar issues.

@cspotcode
Copy link
Collaborator

Sure, here's a link dump.

The dependency is declared here:

"@cspotcode/source-map-support": "0.6.1",

Published here:
http://npm.im/@cspotcode/source-map-support

Code lives here:
https://github.com/cspotcode/node-source-map-support

It declares a dependency here:
https://github.com/cspotcode/node-source-map-support/blob/797e4f3cc85514f756d495ba69591ef2f2b43c32/package.json#L19

Which is published here:
http://npm.im/@cspotcode/source-map-consumer

Code lives here:
https://github.com/cspotcode/source-map

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

No branches or pull requests

2 participants