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

408
Visualizações
Why are dynamic imports unexpectedly coupled between tests when using mock-fs?

I'm trying to use mock-fs to unit test code which uses ES6 dynamic imports.

There seems to be an unexpected coupling between tests when I'm using dynamic imports, even though I call restore() after each test. It appears as though fs.readFile() behaves as expected between tests (no coupling), but await import() has coupling (it returns the result from the previous test).

I've created a minimal Jest test case that reproduces the issue. The tests pass individually, but not when run together. I notice that if I change the directory value so it's different between each test, then they pass together.

Can you help me understand why this doesn't work, whether it's a bug, and what I should do here?

import path from 'path';
import { promises as fs } from 'fs';
import mockFs from 'mock-fs';

const fsMockModules = {
  node_modules: mockFs.load(path.resolve(__dirname, '../node_modules')),
};

describe('Reproduce dynamic import coupling between tests', () => {
  afterEach(() => {
    mockFs.restore();
  });
  it('first test', async () => {
    const directory = 'some/path';
    mockFs({
      ...fsMockModules,
      [directory]: {
        'index.js': ``,
      },
    });
    await import(path.resolve(`${directory}/index.js`));
    //not testing anything here, just illustrating the coupling for next test
  });
  it('second tests works in isolation but not together with first test', async () => {
    const directory = 'some/path';
    mockFs({
      ...fsMockModules,
      [directory]: {
        'index.js': `export {default as migrator} from './migrator.js';`,
        'migrator.js':
          'export default (payload) => ({...payload, xyz: 123});',
      },
    });
    const indexFile = await fs.readFile(`${directory}/index.js`, 'utf-8');
    expect(indexFile.includes('export {default as migrator}')).toBe(true);
    const migrations = await import(path.resolve(`${directory}/index.js`));
    expect(typeof migrations.migrator).toBe('function');
  });
});
about 4 years ago · Juan Pablo Isaza
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