preprocessor-loader icon indicating copy to clipboard operation
preprocessor-loader copied to clipboard

Test libraries

Open Vlad412 opened this issue 1 year ago • 1 comments

Hello, does it work with test libraries ?

How to use it with jest ?

Vlad412 avatar Mar 09 '23 14:03 Vlad412

Hey, not sure if you still need this, in my case, I'm using webpack with Quasar, and I don't want to compile the components using the Capacitor library when in PWA mode

First, Create a custom Webpack loader configuration for Jest. Create a file called jest-preprocessor-loader.js in your project's root directory with the following content:

module.exports = {
  process(src, filename, config, options) {
    const preprocessor = require('webpack-preprocessor-loader');
    const params = {
      capacitor: process.env.MODE === 'capacitor'
    };

    return preprocessor(src, filename, config, options, { params });
  },
};

Then, Configure Jest to use the custom loader in your jest.config.js file:

module.exports = {
  // maybe change "vue" to other file extension based on your needs
  moduleNameMapper: {
    '\\.(vue)$': '<rootDir>/jest-preprocessor-loader.js',
  },
  // ...
};

And it works like a champ

HuyHHV avatar Jun 05 '23 01:06 HuyHHV