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

[5.0.0-alpha.3.0] Can't bundle Typescript #423

Closed
vcvuwac opened this issue Jul 8, 2019 · 3 comments
Closed

[5.0.0-alpha.3.0] Can't bundle Typescript #423

vcvuwac opened this issue Jul 8, 2019 · 3 comments

Comments

@vcvuwac
Copy link

vcvuwac commented Jul 8, 2019

I'd like to use karma-webpack to compile typescript.

  • Operating System: Windows 10
  • Node Version: v11.9.0
  • NPM Version: 6.5.0
  • webpack Version: 4.35.2
  • karma-webpack Version: 5.0.0-alpha.3.0

Expected Behavior

Successfully complete test.

Actual Behavior

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be one of type string, TypedArray, or DataView. Received type undefined
    at Hash.update (internal/crypto/hash.js:64:11)
    at Object.sha1 (C:\Users\HiICS\Desktop\karmatest\node_modules\karma\lib\utils\crypto-utils.js:9:8)
    at nextPreprocessor (C:\Users\HiICS\Desktop\karmatest\node_modules\karma\lib\preprocessor.js:28:30)
    at processFile (C:\Users\HiICS\Desktop\karmatest\node_modules\karma-webpack\lib\karma-webpack.js:120:5)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)

karma-webpack generates a name of bundle file from .ts path.
https://github.com/webpack-contrib/karma-webpack/blob/eec6dfc9e80413fe957136975034757bdffe9e8e/lib/karma-webpack.js#L84-L87

However it gets a bundle content from a file path generated from .js path.
So, second argument of done() is always undefined.
https://github.com/webpack-contrib/karma-webpack/blob/eec6dfc9e80413fe957136975034757bdffe9e8e/lib/karma-webpack.js#L105-L120

Code

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    files: ['index.test.ts'],
    preprocessors: {
      '*.ts': ['webpack']
    },
    browsers: ['Chrome'],
    webpack: {
      resolve: { extensions: ['.ts'] },
      module: {
        rules: [
          { test: /\.ts$/, use: ['ts-loader'] }
        ]
      }
    }
  })
}
// index.test.ts
describe("Test", () => it("test", () => expect(true).toBe(true)));

How Do We Reproduce?

See karma.conf.js above. You can install all dependencies by npm i -D karma karma-chrome-launcher karma-jasmine jasmine karma-webpack@5.0.0-alpha.3.0 webpack ts-loader typescript @types/jasmine. This bug is also reproduced if you use mocha. No problem even if tsconfig.json is an empty file.
It's not reproduced in karma-webpack@4.0.2

@naoak
Copy link

naoak commented Apr 20, 2020

I made a patch for this issue. Hope this helps anyone.

diff --git a/node_modules/karma-webpack/lib/karma-webpack.js b/node_modules/karma-webpack/lib/karma-webpack.js
index 05a35b2..8740b3a 100644
--- a/node_modules/karma-webpack/lib/karma-webpack.js
+++ b/node_modules/karma-webpack/lib/karma-webpack.js
@@ -113,10 +113,11 @@ function preprocessorFactory(config, emitter) {
   return async function processFile(content, file, done) {
     await controller.bundle();
 
+    const originalFileInfo = path.parse(file.path);
     file.path = normalize(transformPath(file.path)); // eslint-disable-line no-param-reassign
 
-    const bundleContent =
-      controller.bundlesContent[getPathKey(file.path, true)];
+    const pathKey = getPathKey(file.path.replace('.js', originalFileInfo.ext), false) + '.js'
+    const bundleContent = controller.bundlesContent[pathKey];
     done(null, bundleContent);
   };
 }

@codymikol
Copy link
Owner

To limit confusion, I am combining all threads for webpack 5 / karma6 support over here ==> #475

@codymikol
Copy link
Owner

v5 is now available

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

No branches or pull requests

3 participants