Search code examples
testingwebpackjasminekarma-runnerminify

How to disable webpack minification for classes names


I use jasmine, karma and webpack to test my module. The webpack preprocesses my tests files before initiating tests.

In my tests I have the class Name{...} to be tested. I create new Name instance and then, in my tests I expect(myInstance.constructor.name).toBe("Name")

class Name{}

const myInstance = new Name();

describe("The object",function(){
  it("should be the instance of Name class",function(){
    expect(myInstance.constructor.name).toBe("Name");  // Expected 't' to be 'Name'.
  })
});

But it returns failed tests. I figured out that my Name class is parsed by webpack to the t class in the bundled file and myInstance.constructor.name equals "t".

Can I prevent webpack to change the names of classes/constructors?


Solution

  • Have a build setup for development and production separately, whenever in development mode(which you can mention in the webpack config object), don't apply minification plugin(might be there in your webpack config).

    Help links:

    1. Bundling Modes
    2. Minification plugin Webpack v4
    3. Minification plugin Webpack v5

    You can use 'keep_classnames' option provided by the minification plugin to keep the class names intact.