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

145
Views
Cómo simular una llamada al constructor de clase en un módulo de terceros llamado desde el mismo módulo usando broma

Tengo un módulo de terceros como este

 class Test { async doSomething() {} } export const testObject = new Test(); <--- I want to mock this part because constructor requires some input which I don't want to provide as it is not required for tests

otro módulo que importa el módulo anterior

 import { testObject } from 'module1'; function foo() { testObject.doSomething() }

Ahora estoy tratando de escribir pruebas unitarias como a continuación

 describe('test', ()=> { test('', ()=> { foo() }) })

new Test() depende de una entrada externa que no quiero proporcionar, así que cuando ejecuto pruebas, falla debido a la falta de entrada y no estoy seguro de cómo evitar que new Test() se ejecute tal cual y en su lugar es un simulacro las funciones deben ejecutarse en su lugar

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

0

Prueba algo como esto. Si ejecuta esta prueba, pasará:

archivo de clase.

 class SoundPlayer { foo: string constructor() { this.foo = 'bar' } playSoundFile(fileName: any) { console.log('Playing sound file ' + fileName) } } export const testObject = new SoundPlayer()

Archivo de función

 import { testObject } from './test' export default function test() { testObject.playSoundFile('testing') }

archivo de prueba

 import { testObject } from '../test' import test from '../test1' jest.mock('../test') it('should run test', () => { test() expect(testObject.playSoundFile).toHaveBeenCalledTimes(1) })

Este es un ejemplo muy básico y hay 4 formas diferentes de burlarse. Consulte la documentación de Jest: https://jestjs.io/docs/es6-class-mocks#the-4-ways-to-create-an-es6-class-mock

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!