Skip to content

jest-junit report check will incorrectly fail if the reporter is provided via require.resolve #201

@Aghassi

Description

@Aghassi

function _hasReporter(config, name) {
if (!config.reporters) {
config.reporters = [];
}
for (const r of config.reporters) {
if (Array.isArray(r)) {
return r.length > 0 && r[0] == name;
} else {
return r == name;
}
}
}

This logic checks if the reporters needed are in the reporter array already, and if not adds it. However, you can have a reporter in the reporters array that is defined like this:

import { createRequire } from 'node:module'
import path from 'path'
import { fileURLToPath } from 'url'


/**
 * NOTE(david.aghassi) - ESM doesn't have the concept
 * of `__dirname` or `__filename` global variables like commonjs
 * This logic allows us to use that older syntax
 * while leveraging the built-ins provided by ESM
 */
const __esm_filename = fileURLToPath(import.meta.url)
const __esm_dirname = path.dirname(__esm_filename)

const __esm_require = createRequire(import.meta.url)

const config = {
  reporters: ['default'].concat(
    ['jest-junit'].map(module => __esm_require.resolve(module))
  )
}

In this case, you are providing jest the reporter through an abolute path resolved by node. This will fail the name check because it won't match identically. This case matters for me because we centralize some of our jest infra so that consuming teams don't need to define it again and again.

Now, one could argue the most effective way to solve this is to globally declare jest-junit in your pnpm workspace, but I'm trying to avoid having global definitions when possible.

It would be ideal if this logic supported full path resolves so it doesn't try to inject jest-junit twice and fail due to module resolution problems

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedAspect isn't prioritizing this, but the community could

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions