codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 221 forks source link

Error during file loading or preprocessing #533

Closed Merynek closed 10 months ago

Merynek commented 2 years ago

I got some errors during running karma tests.

Error log:

"C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" test --scripts-prepend-node-path=auto

> worldee-client@1.0.0 test
> node --max_old_space_size=8000 node_modules/karma/bin/karma start karma.conf.js

karma-webpack does not currently support custom entries, if this is something you need,
consider opening an issue.
ignoring attempt to set the entry option...

karma-webpack does not currently support customized filenames via
webpack output.filename, if this is something you need consider opening an issue.
defaulting js/[name].[hash].js to [name].js.
Webpack bundling...
Webpack starts watching...
(node:17484) [DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK] DeprecationWarning: Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation)
.loader
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:17484) [DEP_WEBPACK_MAIN_TEMPLATE_RENDER_MANIFEST] DeprecationWarning: MainTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)
(node:17484) [DEP_WEBPACK_CHUNK_TEMPLATE_RENDER_MANIFEST] DeprecationWarning: ChunkTemplate.hooks.renderManifest is deprecated (use Compilation.hooks.renderManifest instead)
(node:17484) [DEP_WEBPACK_MAIN_TEMPLATE_HASH_FOR_CHUNK] DeprecationWarning: MainTemplate.hooks.hashForChunk is deprecated (use JavascriptModulesPlugin.getCompilationHooks().chunkHa
sh instead)
(node:17484) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(node:17484) [DEP_WEBPACK_MODULE_ID] DeprecationWarning: Module.id: Use new ChunkGraph API
(node:17484) [DEP_WEBPACK_MODULE_UPDATE_HASH] DeprecationWarning: Module.updateHash: Use new ChunkGraph API
(node:17484) [DEP_WEBPACK_CHUNK_MODULES_ITERABLE] DeprecationWarning: Chunk.modulesIterable: Use new ChunkGraph API
(node:17484) [DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX_2] DeprecationWarning: ChunkGroup.getModuleIndex2 was renamed to getModulePostOrderIndex
assets by path *.js 12 MiB
  assets by status 917 KiB [compared for emit] 83 assets
  assets by status 11.1 MiB [emitted] 2 assets
assets by path images/ 2.96 MiB
  assets by path images/.storybook/mocks/photos/db/ 2.28 MiB 21 assets
  assets by path images/.storybook/mocks/user_photos/photos/*.jpg 624 KiB 11 assets
  asset images/src/app/components/components/loader-area/loading_gif.gif?037557462b2738cdd9b0473a6d402928 75.4 KiB [compared for emit] (auxiliary name: commons) (auxiliary id hint:
 commons)
assets by path sprites/*.svg 3.12 MiB
  assets by status 3.07 MiB [big]
    asset sprites/src_app_components_components_svgIcon_icons_customsprite-9a9070.svg 2.2 MiB [emitted] [big]
    + 2 assets
  + 6 assets
asset commons.css 23.9 KiB [emitted] [compared for emit] (name: commons) (id hint: commons) 1 related asset
asset manifest.json 12.3 KiB [emitted] [compared for emit]
webpack 5.70.0 compiled successfully in 33129 ms
30 03 2022 07:49:32.088:ERROR [karma-server]: Error during file loading or preprocessing
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at Hash.update (node:internal/crypto/hash:105:11)
    at Object.sha1 (C:\Work\WORLDEE_WEB\client\node_modules\karma\lib\utils\crypto-utils.js:9:8)
    at runProcessors (C:\Work\WORLDEE_WEB\client\node_modules\karma\lib\preprocessor.js:70:26)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async FileList.preprocess [as _preprocess] (C:\Work\WORLDEE_WEB\client\node_modules\karma\lib\preprocessor.js:106:5)
    at async Promise.all (index 0)
    at async C:\Work\WORLDEE_WEB\client\node_modules\karma\lib\file-list.js:90:11
    at async Promise.all (index 2)
30 03 2022 07:49:32.101:ERROR [karma-server]: UnhandledRejection: Error: ENOENT: no such file or directory, open 'C:\Work\WORLDEE_WEB\client/../www/client//images/.storybook/mocks/
photos/db/6.png?f53065b56022e5125e9bd2e02b12253d'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at C:\Work\WORLDEE_WEB\client\node_modules\karma-webpack\lib\webpack\plugin.js:20:66
    at Array.forEach (<anonymous>)
    at C:\Work\WORLDEE_WEB\client\node_modules\karma-webpack\lib\webpack\plugin.js:16:29
    at Hook.eval [as callAsync] (eval at create (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:38:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\Hook.js:18:14)
    at Watching._done (C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:287:28)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:209:21
    at Compiler.emitRecords (C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:906:5)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:187:22
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:872:14
    at Hook.eval [as callAsync] (eval at create (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\Hook.js:18:14)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:869:27
    at C:\Work\WORLDEE_WEB\client\node_modules\neo-async\async.js:2818:7
30 03 2022 07:49:32.101:ERROR [karma-server]: Error: ENOENT: no such file or directory, open 'C:\Work\WORLDEE_WEB\client/../www/client//images/.storybook/mocks/photos/db/6.png?f530
65b56022e5125e9bd2e02b12253d'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at C:\Work\WORLDEE_WEB\client\node_modules\karma-webpack\lib\webpack\plugin.js:20:66
    at Array.forEach (<anonymous>)
    at C:\Work\WORLDEE_WEB\client\node_modules\karma-webpack\lib\webpack\plugin.js:16:29
    at Hook.eval [as callAsync] (eval at create (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:38:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\Hook.js:18:14)
    at Watching._done (C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:287:28)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:209:21
    at Compiler.emitRecords (C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:906:5)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Watching.js:187:22
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:872:14
    at Hook.eval [as callAsync] (eval at create (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (C:\Work\WORLDEE_WEB\client\node_modules\webpack\node_modules\tapable\lib\Hook.js:18:14)
    at C:\Work\WORLDEE_WEB\client\node_modules\webpack\lib\Compiler.js:869:27
    at C:\Work\WORLDEE_WEB\client\node_modules\neo-async\async.js:2818:7 {
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: 'C:\\Work\\WORLDEE_WEB\\client/../www/client//images/.storybook/mocks/photos/db/6.png?f53065b56022e5125e9bd2e02b12253d'
}
Webpack stopped watching.

my libs:

"devDependencies": {
    "@openapitools/openapi-generator-cli": "^2.2.3",
    "@storybook/addon-actions": "^6.4.19",
    "@storybook/addon-knobs": "^6.4.0",
    "@storybook/builder-webpack5": "^6.4.0-alpha.29",
    "@storybook/manager-webpack5": "^6.4.0-alpha.29",
    "@storybook/react": "^6.4.19",
    "@teamsupercell/typings-for-css-modules-loader": "^2.1.1",
    "@types/body-scroll-lock": "^3.1.0",
    "@types/date-fns": "^2.6.0",
    "@types/dompurify": "^2.0.4",
    "@types/draft-convert": "^2.1.1",
    "@types/draftjs-to-html": "^0.8.0",
    "@types/facebook-js-sdk": "^3.3.0",
    "@types/googlemaps": "^3.39.13",
    "@types/i18next": "^13.0.0",
    "@types/jasmine": "^3.5.10",
    "@types/jquery": "^3.3.34",
    "@types/lodash": "^4.14.165",
    "@types/react": "^17.0.3",
    "@types/react-beautiful-dnd": "^13.1.2",
    "@types/react-css-collapse": "^3.6.0",
    "@types/react-datepicker": "^4.3.4",
    "@types/react-dom": "^17.0.3",
    "@types/react-draft-wysiwyg": "^1.13.2",
    "@types/react-google-recaptcha": "^2.1.0",
    "@types/react-slider": "^1.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "circular-dependency-plugin": "^5.2.2",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.5.3",
    "css-loader-minify-class": "^1.3.0",
    "csstype": "^3.0.3",
    "del-cli": "^3.0.0",
    "exec-php": "0.0.5",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^3.0.1",
    "fork-ts-checker-webpack-plugin": "^6.0.4",
    "ignore-not-found-export-webpack-plugin": "^1.0.2",
    "ironbean-jasmine": "^1.0.0",
    "jasmine-core": "^3.5.0",
    "karma": "6.3.17",
    "karma-chrome-launcher": "^3.1.0",
    "karma-commonjs": "^1.0.0",
    "karma-jasmine": "^3.1.1",
    "karma-typescript": "^5.0.2",
    "karma-webpack": "5.0.0",
    "less": "^3.9.0",
    "less-loader": "^5.0.0",
    "mini-css-extract-plugin": "^0.6.0",
    "object-assign": "^4.1.1",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "storybook-addon-i18next": "^1.3.0",
    "storybook-icon-gallery": "^1.2.1",
    "style-loader": "^0.23.1",
    "svg-sprite-loader": "6.0.11",
    "svgo": "^1.3.2",
    "svgo-loader": "^2.2.1",
    "terser-webpack-plugin": "^4.1.0",
    "ts-loader": "^8.3.0",
    "ts-transform-async-to-mobx-flow": "^0.0.11",
    "typescript": "4.2.4",
    "video.js": "^7.8.3",
    "webpack": "^5.70.0",
    "webpack-bundle-analyzer": "^3.8.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "4.7.4",
    "webpack-manifest-plugin": "^5.0.0",
    "worker-loader": "^3.0.7"
  }

My karma.config.js

const webpackConfig = require('./webpack.config.js');
webpackConfig.entry = {};

module.exports = function (config) {
    config.set({
        frameworks: ["webpack"],
        files: [
            "./test/boot.spec.ts",
            "./src/app/**/*.ts", // *.tsx for React Jsx
            "./test/**/*.ts" // *.tsx for React Jsx
        ],
        preprocessors: {
            // add webpack as preprocessor
            'test/**/*.ts': ['webpack']
        },
        webpack: webpackConfig,
        browsers: ["Chrome"],
        logLevel: config.LOG_ERROR
    });
};

My webpack.config:

{
    mode: 'development',
    entry: {},
    output: {
        path: __dirname + "/../www/" + this.clientBasePath,
        filename: "js/[name].[hash].js",
        publicPath: this.basePath + this.clientBasePath,
        chunkFilename: "js/[name].chunk.[contenthash].js"
    },
    optimization: {
        minimizer: [new OptimizeCSSAssetsPlugin({})]
    },
    devServer: undefined,
    plugins:  [
        new MiniCssExtractPlugin({
            filename: "css/[name].[hash].css",
            chunkFilename: "[id].css"
        }),
        new SpritePlugin(),
        new WebpackManifestPlugin({
            publicPath: "",
            seed: {
                "app.css": "",
                "scripts.css": "",
                "adminApp.css": "",
                "adminAppEditor.css": ""
            }
        }),
        new CleanWebpackPlugin({
            cleanOnceBeforeBuildPatterns: ['js/*', 'css/*', 'sprites/*']
        }),
        new ForkTsCheckerWebpackPlugin(),
        new CircularDependencyPlugin({
            exclude: /node_modules/,
            cwd: process.cwd(),
            failOnError: true,
            allowAsyncCycles: false,
            onDetected({ module: webpackModuleRecord, paths, compilation }) {
                // `paths` will be an Array of the relative module paths that make up the cycle
                // `module` will be the module record generated by webpack that caused the cycle
                compilation.errors.push(new Error(paths.join(' -> ')))
            }
        }),
        new IgnoreNotFoundExportPlugin(),
        ...(this.enableAnalyzer ? [new BundleAnalyzerPlugin()] : [])
    ],
    resolve: { extensions: [ '.less', '.ts', '.tsx', '.jsx', '.js' ] },
    performance: { hints: false },
    module: {
        rules: [
            {
                test: /\.(html)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "[name].[ext]",
                            outputPath: ""
                        }
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "[name].[ext]",
                            outputPath: "fonts/"
                        }
                    }
                ]
            },
            {
                test: /\.svg$/,
                include: [
                    path.resolve(__dirname, "src/app/components/components/svgIcon/icons/")
                ],
                use: [
                    {
                        loader: 'svg-sprite-loader',
                        options: {
                            extract: true,
                            publicPath:  this.basePath + this.clientBasePath + 'sprites/',
                            outputPath: 'sprites/',
                            spriteFilename: filePath => {
                                const prefix = path.dirname(path.relative(__dirname, filePath))
                                    .split("\\")
                                    .join("_");

                                return prefix + 'sprite-[hash:6].svg'
                            },
                            symbolId: filePath => {
                                return path.basename(filePath, ".svg") + svgMinifier.getNextIdent(filePath);
                            }
                        }
                    },
                    {
                        loader: "svgo-loader"
                    }
                ]
            },
            {
                test: /\.svg$/,
                exclude: [
                    path.resolve(__dirname, "src/app/components/components/svgIcon/icons/")
                ],
                use: [
                    this.buildFileLoaderForImage(),
                    {
                        loader: "svgo-loader"
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif|webp)$/,
                use: [
                    this.buildFileLoaderForImage()
                ]
            },
            {
                test: /\.(css|less)$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: "../"
                        }
                    },
                    {
                        loader: "css-loader",
                        options: {
                            sourceMap: true,
                            url: true,
                            modules: false
                        }
                    },
                    "less-loader"
                ],
                exclude: /\.module\.(css|less)$/
            },
            {
                test: /\.(css|less)$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: "../"
                        }
                    },
                    "@teamsupercell/typings-for-css-modules-loader",
                    {
                        loader: "css-loader",
                        options: {
                            sourceMap: true,
                            url: true,
                            modules: {
                                auto: true,
                                getLocalIdent: createMinifier({prefix: "w_"})
                            },
                            localsConvention: "camelCaseOnly"
                        }
                    },
                    "less-loader"
                ],
                include: /\.module\.(css|less)$/
            },
            {
                test: /\.(ts|tsx)?$/,
                loader: "ts-loader",
                exclude: /node_modules/,
                options: {
                    transpileOnly: true,
                    getCustomTransformers: () => ({
                        before: [tsTransformAsyncToMobxFlow(/** options */)],
                    }),
                },
            },
            ...(this.devServer ? [{
                loader: 'worker-loader',
                options: { inline: 'fallback' },
            }] : []),
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: function (modulePath) {
                    if (!/node_modules/.test(modulePath)) {
                        return false
                    }

                    return transpileDependencies.every(function (dependence)  {
                        return modulePath.indexOf(dependence) === -1;
                    });
                },
                options: {
                    presets: [
                        ["env", {
                            "modules": false,
                            "targets": {
                                "browsers": [
                                    "IE 11"
                                ]
                            }
                        }]
                    ]
                }
            }
        ]
    },
    externals: { jquery: 'jQuery' },
    devtool: 'source-map'
}

Any idea?

codymikol commented 2 years ago

Does this solve your issue?

https://github.com/ryanclark/karma-webpack/issues/517#issuecomment-993088871

Merynek commented 2 years ago

Does this solve your issue?

#517 (comment)

You mean change name of output name?

package.json

 "file-loader": "6.2.0"
{
            loader: "file-loader",
            options: {
                name: (resourcePath, resourceQuery) => {
                    // `resourcePath` - `/absolute/path/to/file.js`
                    // `resourceQuery` - `?foo=bar`

                    if (this.isInDebugMode) {
                        return '[path][name].[ext]?[hash]';
                    }

                    return '[path][contenthash].[ext]';
                },
                outputPath: "images/",
                publicPath:  (this.cdnUrl || this.basePath) + this.clientBasePath + "images/"
            }
        };

Still got errors with this config of file-loader

codymikol commented 2 years ago

Nope, the bit about file-loader being deprecated, see the comment I linked.

Merynek commented 2 years ago

Nope, the bit about file-loader being deprecated, see the comment I linked.

I´am sorry but I dont understand where I have to set part from your linked comment. Here is my webpack config where is file-loader

file-loader version is: 6.2.0

{
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "[name].[ext]",
                            outputPath: "fonts/"
                        }
                    }
                ]
            },
            {
                test: /\.svg$/,
                include: [
                    svgIconsPath
                ],
                use: [
                    {
                        loader: 'svg-sprite-loader',
                        options: {
                            extract: true,
                            publicPath:  this.basePath + this.clientBasePath + 'sprites/',
                            outputPath: 'sprites/',
                            spriteFilename: filePath => {
                                const prefix = path.dirname(path.relative(__dirname, filePath))
                                    .split("\\")
                                    .join("_");

                                return prefix + 'sprite-[hash:6].svg'
                            },
                            symbolId: filePath => {
                                return path.basename(filePath, ".svg") + svgMinifier.getNextIdent(filePath);
                            }
                        }
                    },
                    {
                        loader: "svgo-loader"
                    }
                ]
            },
            {
                test: /\.svg$/,
                exclude: [
                    svgIconsPath
                ],
                use: [
                    this.buildFileLoaderForImage(),
                    {
                        loader: "svgo-loader"
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif|webp)$/,
                use: [
                    this.buildFileLoaderForImage()
                ]
            },

Here is buildFileLoaderForImage fnc:

buildFileLoaderForImage() {
        return {
            loader: "file-loader",
            options: {
                name: (resourcePath, resourceQuery) => {
                    // `resourcePath` - `/absolute/path/to/file.js`
                    // `resourceQuery` - `?foo=bar`

                    if (this.isInDebugMode) {
                        return '[path][name].[ext]?[fullhash]';
                    }

                    return '[path][contenthash].[ext]';
                },
                outputPath: "images/",
                publicPath:  (this.cdnUrl || this.basePath) + this.clientBasePath + "images/"
            }
        };
    }

Error with import png: Tsx Component:

import articleImg2 from "./images/buddy_itinerary_where.png";

Log:

[karma-server]: UnhandledRejection: Error: ENOENT: no such file or directory, open 'C:\Work\WORLDEE_WEB\client/../www/client//images/src/app/component
s/pages/Buddy-itinerary/images/buddy_itinerary_what.png?b50142538741d01bd7ac'
codymikol commented 2 years ago

Read this https://webpack.js.org/guides/asset-modules/

Merynek commented 2 years ago

Read this https://webpack.js.org/guides/asset-modules/

I understand it, I have this issue after migrate to webpack 5. But normal build works. I have own settings of file-loader and it is not problem in build. Just only in karma.

MS-elug commented 1 year ago

I fixed similar issue by removing the query value of my imported asset urls, e.g. change "buddy_itinerary_what.png?b50142538741d01bd7ac" to "buddy_itinerary_what.png"