Test is failing while creating package, but passes in all other cases

It might be a bit overkill, but...

Try deploying the package metadata to a blank dev org. And then have the dev org create the package via the older packaging methods.

You aren't so much interested in the resulting package (assuming it doesn't fail on the test). In this scenario the test cases will run in the context of the dev org and you can capture the resulting debug logs to help determine the failure reason.

In a version create command, the test is not run in your org. This means that there is absolutely no data or metadata available in the phantom org that runs the test except for that provided in the package.

You must create all the data you need for the test to pass. If you need to debug the logic, create a new Scratch Org, install your package and/or deploy your metadata there, and run the tests. You can use normal debug techniques at that point.

You may also want to look at Specify Unpackaged Metadata for Package Version Creation Tests (Pilot), although it's not clear that this would help you in this case.