Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda