jest-community / jest-junit

A Jest reporter that creates compatible junit xml files
Apache License 2.0
485 stars 109 forks source link

Jest execution is not exiting on npx run without package.json file #120

Open benophilipmathew opened 4 years ago

benophilipmathew commented 4 years ago

Hi Guys,

Recently I was setting up azure release pipeline for my jest tests and found that the task execution is not exiting while running jest through npx. Later I found jest-junit reporter is causing this issue.

However, the interesting part is npx run with an empty package.json file will work as expected

Note - This is only happening with jest-junit package if we run without jest-junit it will run fine

How to reproduce-

step 1 - git clone https://benophilipmathew@bitbucket.org/benophilipmathew_dl/jest-junit-issue.git step 2 - npm i jest jest-junit step 3 - npx jest --maxWorkers=1 --forceExit

I am using node - 13.3.0 but I guess, it's the same with all other versions

expected result - test execution should stop after a successful run actual result - test execution will never stop

How I fixed this - added an empty (i.e. {}) package.json in the working folder and it started working as expected

Any idea why the empty package.json file is doing this magic?

palmerj3 commented 4 years ago

@benophilipmathew Thank you for reporting this! I will take a look.

Can I ask: why do you have no package.json? In my experience this is not very common at all to see (e.g. relying a node_module without a package.json present).

palmerj3 commented 4 years ago

I'm not able to reproduce this error locally using OS X. Are you running this on windows by chance? The only other mention of someone seeing jest-junit never exit was reported on Windows.

benophilipmathew commented 4 years ago

Hello @palmerj3, Yes, I am running it on Windows (locally & build server).

I totally agree with you, this is not a very common scenario and I come across this while creating release pipeline for my frontend tests which is driven through puppeteer (where I want to test stuff after deployment and that's why I am copying only a few files, installing only required package and running it through npx)

Anyway, my final version will definitely have a package.json and this is not something that can block me but curious to know what kind of dependency we have on package.json

palmerj3 commented 4 years ago

@benophilipmathew I don't have a windows machine to test with, unfortunately, but I should have access to one after the holidays so I'll leave this issue open until then.

mvromer commented 3 years ago

I ran into this exact problem this evening while testing a Yeoman generator I had written. I'm using Jest as my test framework, and I use jest-junit to produce JUnit test result files that I can upload to Azure Pipelines.

In my situation running on Windows 10, the Yeoman test helpers will create a temp directory under my user AppData\Local\Temp folder, change into that directory, and then run the generator under test within that. The Yeoman test helpers then give you different assertion helpers you can you use to make sure your generator actually produces the files you expect.

When jest-junit runs, it'll eventually call getAppOptions and pass for the pathToResolve the current working directory:

    return Object.assign({}, constants.DEFAULT_OPTIONS, reporterOptions, getAppOptions(process.cwd()), getEnvOptions());

That call eventually does a walk up the directory hierarchy looking for a package.json file. It's supposed to stop when it hits the root of the file system, but it looks like it's checking that by comparing against path.sep:

    traversing = pathToResolve !== path.sep;

That'll work (presumably) fine on Unix-based OSes where the file system root is /, but that won't work on Windows. Node's path.sep is set to \, but the root of the volume on Windows also contains the drive letter. So in my case, where the current working directory is under the user's temp directory, the root is C:\. So I think part of the fix is accounting for that on Windows systems. Otherwise, what happens is getAppOptions ends up going into an infinite loop.

mastrzyz commented 2 years ago

@mvromer , would this recent commit fix this issue ? https://github.com/jest-community/jest-junit/pull/215 also @benophilipmathew