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

How to trace jest with custom environment #4164

Open
lughino opened this issue Mar 14, 2024 · 1 comment
Open

How to trace jest with custom environment #4164

lughino opened this issue Mar 14, 2024 · 1 comment

Comments

@lughino
Copy link

lughino commented Mar 14, 2024

Hi there,
I need to use a custom environment as I need a newer version of jsdom, so I imported the new @jest/environment-jsdom-abstract package and created a custom environment that extends from that class:

const JSDOM = require('jsdom');
const BaseEnv = require('@jest/environment-jsdom-abstract').default;

class JSDOMEnvironment extends BaseEnv {
  constructor(config, context) {
    super(config, context, JSDOM);
  }
}

module.exports = JSDOMEnvironment;
module.exports.default = JSDOMEnvironment;

Unfortunately, I did not get that working. I always get this error:

/Users/lughino/ui/node_modules/dd-trace/packages/datadog-plugin-jest/src/index.js:228
      this.testSuiteSpan.setTag(TEST_STATUS, status)
                         ^

TypeError: Cannot read properties of undefined (reading 'setTag')
    at /Users/lughino/ui/node_modules/dd-trace/packages/datadog-plugin-jest/src/index.js:228:26
    at Subscription._handler (/Users/lughino/ui/node_modules/dd-trace/packages/dd-trace/src/plugins/plugin.js:14:9)
    at Channel.publish (node:diagnostics_channel:56:9)
    at /Users/lughino/ui/node_modules/dd-trace/packages/datadog-instrumentations/src/jest.js:591:27
    at runTestInternal (/Users/lughino/ui/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/Users/lughino/ui/node_modules/jest-runner/build/runTest.js:444:34)

To initiate the dd-trace I tried to use the dd-trace/ci/init through NODE_OPTIONS, or simply including it in this test env file like require('dd-trace').init();.
None of my attempts made it work.

The jest settings are:

const baseConfig = {
  testEnvironment: require.resolve('@internal/jest-preset/jsdomEnv.js'),
  testRegex: '\\.test\\.(js|tsx?)$',
  testMatch: null,
}

If I switch to testEnvironment: 'jsdom' everything works properly.
I also tried to copy the exact content of the old jest-environment-jsdom package inside this custom file but I always get the same error.
It seems like there is some sort of logic around those test environments that prevents me from using tracing in a custom environment.

Any help would be very appreciated

@juan-fernandez
Copy link
Collaborator

hey @lughino ! Thanks for the report.

My first thought was that you could extend directly from jest-environment-jsdom (as they with node in the jest docs) but from what I see in the source, JSDOM is hardcoded: https://github.com/jestjs/jest/blob/9113f44b459f96db31e5ad3811d4d8b07f79e54f/packages/jest-environment-jsdom/src/index.ts#L17, so that's not an option for you.

Sadly I don't think there's any workaround for you, as we only monkey patch jest-environment-jsdom and jest-environment-node:

addHook({
name: 'jest-environment-node',
versions: ['>=24.8.0']
}, getTestEnvironment)
addHook({
name: 'jest-environment-jsdom',
versions: ['>=24.8.0']
}, getTestEnvironment)

This said, I think adding support for @jest/environment-jsdom-abstract wouldn't be too hard (famous last words). Would you be interesting in contributing the change? I can help 😄

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

No branches or pull requests

2 participants