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

use of source-map-support causes the wrong line numbers to appear in @swc/jest tests #3105

Closed
parisholley opened this issue May 5, 2022 · 6 comments
Labels
bug:unverified feat:dx Issues related to the developer experience

Comments

@parisholley
Copy link

What version of Remix are you using?

1.4.0

Steps to Reproduce

kulshekhar/ts-jest#2372

if for any reason @remix-run/node is pulled into a test, this issue will happen. should either ENV flag outside of test, or version flag to only include on older node versions?

Expected Behavior

should show correct typescript line numbers

Actual Behavior

line numbers get mangled

@esamattis
Copy link
Contributor

esamattis commented May 20, 2022

I'm seeing this as well with babel-jest when ever @remix-run/node gets imported in the test code. I guess both have some sort of source map support which causes conflicts.

@machour machour added the feat:dx Issues related to the developer experience label May 20, 2022
@esamattis
Copy link
Contributor

esamattis commented Jun 3, 2022

The issue is here

sourceMapSupport.install();

Jest probably installs this as well and they seem to collide. The line numbers are correct if this line is removed. I created a repro here with babel-jest:

https://github.com/esamattis/remix-jest-line-numbers-bug

I also created a patch-package patch here which can be used to fix this in projects.

But this is not probably not the best way to fix this since it means coding jest specific things into Remix. Maybe we can add a global flag to disable Remix source maps in the test frameworks 🤔

Eg.

if (!process.env.REMIX_DISABLE_SOURCE_MAPS) {
  sourceMapSupport.install(); 
}

and Jest users could add REMIX_DISABLE_SOURCE_MAPS=1 to their jest config files.

Sent a PR #3374

@esamattis
Copy link
Contributor

Just to note: this issue happens with ts-jest too and the same workaround works for it too.

@yesmar12
Copy link

Since there has not been any progress I'm commenting to say that this worked for me with ts-jest as you say. Would love to see the PR above be put into the application.

@gorbak25
Copy link

gorbak25 commented Jun 7, 2023

If anyone encounters this issue you may use moduleNameMapper in jest to ensure the source-map-support module is is replaced in tests by a harmless stub:

// file: ./app/jest/source-map-support.stub.ts
/* eslint-disable no-console */
export const install = () => {
  let caller = "[UNKNOWN]";
  // Play some 4D chess
  const o = Error.prepareStackTrace;
  try {
    Error.prepareStackTrace = (_err, stackTraces) => {
      const c = stackTraces[1];
      if (c) caller = `${c.getFileName()}:${c.getLineNumber()}:${c.getColumnNumber()}`;
    };
    const a = new Error();
    void a.stack;
  } finally {
    Error.prepareStackTrace = o;
    console.error(
      `Warning! Someone tried to install source map support in a jest test. This will cause all stack traces to be malformed! The offender:\n${caller}`,
    );
  }
};

Then in your jest config you may:


/** @type {import('jest').Config} */
var config = {
  ...
  moduleNameMapper: {
    "^source-map-support$": "<rootDir>/app/jest/source-map-support.stub.ts",
    ...
  },
};

@brophdawg11
Copy link
Contributor

brophdawg11 commented Aug 2, 2023

This should be obsolete once v2 launches since we've removed the automatic inclusion of source map support and that will be handled in userland: #7009

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:unverified feat:dx Issues related to the developer experience
Projects
None yet
Development

No branches or pull requests

6 participants