codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 221 forks source link

preprocessor change between v4 & v5 leading to error when used with karma-mocha #543

Closed subodhkumar closed 1 year ago

subodhkumar commented 1 year ago

Expected Behavior

karma-webpack should work well with karma-mocha

Actual Behavior

This is related to executing the functional tests with karma,karma-webpack & karma-mocha

In our usecase where we recently updated the karma-webpack, we are facing errors as few files are not available post preprocessing causing the karma server to fail.

Not sure if the change is a bug or intended. Just wanted to understand the reason behind the change. Appreciate if there are any workarounds & corrections in my understanding of the code.

codymikol commented 1 year ago

In v5 you can require all of the files you want to include in your entry point rather than pass them in via configuration. Let me know if that works for you.

subodhkumar commented 1 year ago

Thanks for the response. In our use case we have set all the entrypoints.

however as shared above karma-mocha is adding few files which is not configured by us. With v5 as karma-webpack as it’s not adding files in the preprocessor those files are part of bundle output causing the error.

Are you recommending to import(set in the entry section) the files added by karma-mocha by us explicitly.

codymikol commented 1 year ago

Which files are karma-mocha adding?

codymikol commented 1 year ago

Are you recommending to import(set in the entry section) the files added by karma-mocha by us explicitly.

If you mean these files are created by you and added in your mocha config, yes, this should fix your problem.

subodhkumar commented 1 year ago

Which files are karma-mocha adding?

karma-mocha is adding karma-mocha/src/adapter.js

subodhkumar commented 1 year ago

Are you recommending to import(set in the entry section) the files added by karma-mocha by us explicitly.

If you mean these files are created by you and added in your mocha config, yes, this should fix your problem.

the files are not created by us, but are added by karma-mocha. These files used to be picked up by karma-webpack v4 here. But not anymore in v5

codymikol commented 1 year ago

Can you post the error that you're getting and configs for webpack / karma / mocha? We have integration tests for karma-mocha so I know its possible to use in v5.

subodhkumar commented 1 year ago

Hi @codymikol,

In our project, we construct a customTest.js at the root level of project which has import to all the test-entry points for the project. We trigger the karma process from the root level on the customTest.js. In our karma.config.js we have the following entry of preprocessor

preprocessors: {'**/*.js': ['webpack']}, // include add JS files from project root

I checked the integration tests in karma-webpack & checked how its working. I was able to simulate the error we are facing by making changes to one of the tests(link to the forked karma-webpack project with the code changes: #1, #2)

I have updated the preprocessor entries to point to the root location of the project as we have in our project. If you could clone the forked project & execute npm run test you will see the below error which we are getting.

Screenshot 2022-11-28 at 12 07 36 PM

This error is thrown as the js files(#file1, #file2) added by karma-mocha as allowed to be processed by the karma-webpack plugin due to the updated preprocessor pattern in karma config. However, with v5 the plugin is not allowing any new file additions as compared to v4

As the plugin is skipping the files added by karma-mocha(or plugin which might have similar code) the following code in karma is finding empty content for files added by karma-mocha & causing the error while setting sha1 to undefined

Hope this conveys the issue.

I have the below followup queries.

Thanks again for your time in checking & responding. Really appreciate it.

codymikol commented 1 year ago

This is trying to run the webpack preprocessor on all files in the repository including node_modules via globbing, try using a single file that specifies exactly what you want to import like the index.scenario.js file.

subodhkumar commented 1 year ago

So is including the files on node_modules as part of preprocessor pattern invalid? If so, this used to work in karma-we pack v4. Also, any reason for removing add files code in v5?

codymikol commented 1 year ago

The new pattern essentially just recursively imports everything, so there shouldn't be a need to specify what those files might be

index.test.ts
  requireAll('./tests/**/*/*.spec.js') -> this imports all test cases
  require('./src/index.ts') -> this imports all node_modules / source code needed for your app
codymikol commented 1 year ago

note that requireAll isn't actually a node built in, I use require.context to find and require all my test cases and alias it as requireAll.

subodhkumar commented 1 year ago

The new pattern essentially just recursively imports everything, so there shouldn't be a need to specify what those files might be

index.test.ts
  requireAll('./tests/**/*/*.spec.js') -> this imports all test cases
  require('./src/index.ts') -> this imports all node_modules / source code needed for your app

Can you please explain what is new pattern you are referring here

codymikol commented 1 year ago

Can you share your project or at least configurations? I may be able to point you in the right direction.

subodhkumar commented 1 year ago

Updated the preprocessor pattern to include only test files to address the issue we faced. Thanks for the support