Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
How to print log in file used by moduleNameMapper configuration?

I create a styleMock file to stub out css style files used in the JS files.

jest.config.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  moduleNameMapper: {
    '\\.(css|less|scss)$': '<rootDir>/styleMock.js',
  }
};

styleMock.js:

console.log('style mock');

module.exports = new Proxy(
  {},
  {
    get: function getter(target, key) {
      console.log('key: ', key);
      if (key === '__esModule') {
        return false;
      }
      return key;
    },
  }
);

index.js:

import './index.css';

export function MyComponent() {
  return null;
}

index.css:

.my-component {
  background-color: #fff;
}

index.test.js:

import { MyComponent } from './';

describe('MyComponent', () => {
  test('should pass', () => {
    expect(MyComponent()).toBeNull();
  });
});

I try to print logs inside the getter method. But when I run the jest command:

 PASS  issues/console-log-in-module-name-mapper-file/index.test.js (9.146 s)
  MyComponent
    ✓ should pass (1 ms)

  console.log
    style mock

      at Object.<anonymous> (styleMock.js:1:9)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.2 s

Only the console.log used in module scope works. Why does the console.log in the getter method not print the log?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I figured it out. Because I didn't try to access the property of CSS style obj. We should use .css files as CSS modules.

import s from './index.css';

export function MyComponent() {
  console.log(s.MyComponent);
  return null;
}
 PASS  issues/console-log-in-module-name-mapper-file/index.test.js (9.639 s)
  MyComponent
    ✓ should pass (3 ms)

  console.log
    style mock

      at Object.<anonymous> (styleMock.js:1:9)

  console.log
    key:  __esModule

      at Object.getter [as get] (styleMock.js:7:15)

  console.log
    key:  MyComponent

      at Object.getter [as get] (styleMock.js:7:15)

  console.log
    MyComponent

      at Object.MyComponent (issues/console-log-in-module-name-mapper-file/index.js:4:11)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.7 s
Ran all test suites related to changed files.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!