afterwind-io / preprocessor-loader

Bring the awesome "Conditional Compilation" to the Webpack, and more.
MIT License
40 stars 12 forks source link

Test libraries #36

Open Vlad412 opened 1 year ago

Vlad412 commented 1 year ago

Hello, does it work with test libraries ?

How to use it with jest ?

HuyHHV commented 1 year ago

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