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

146
Views
No se puede burlar de un método de un objeto con broma

Parece que no puedo burlarme de un método de un objeto usando broma.

El siguiente código a continuación mostrará una prueba exitosa cuando ejecute el comando jest -- Feature.test.js

 // ------ Feature.js import Helper from './Helper'; const Feature = (arg1) => { const helper = new Helper(); return helper.isValid(arg1 === 1, 'other'); ; }; export default Feature; ------------------------------------------- // ------ Feature.test.js //import Helper from './Helper'; import Feature from './Feature'; //jest.mock('./Helper'); describe('Feature', () => { it('test', ()=>{ //const helper = new Helper(); //helper.isValid.mockReturnValue(true); const result = Feature('fake value'); console.log(result); expect(result.toString()).toBe('false'); }); });

Pero en el momento en que descomento las líneas en Feature.test.js así:

 // ------ Feature.test.js import Helper from './Helper'; import Feature from './Feature'; jest.mock('./Helper'); describe('Feature', () => { it('test', ()=>{ const helper = new Helper(); helper.isValid.mockReturnValue(true); const result = Feature('fake value'); console.log(result); expect(result.toString()).toBe('false'); }); });

y jest -- Feature.test.js nuevamente, obtengo este resultado:

 > jest "Feature.test.js" console.log undefined at Object.<anonymous> (./Feature.test.js:11:13) FAIL ./Feature.test.js Feature ✕ test (21 ms) ● Feature › test TypeError: Cannot read property 'toString' of undefined 10 | const result = Feature('fake value'); 11 | console.log(result); > 12 | expect(result.toString()).toBe('false'); | ^ 13 | }); 14 | }); 15 | at Object.<anonymous> (./Feature.test.js:12:19) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 0.564 s, estimated 1 s Ran all test suites matching /Feature.test.js/i. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! john@1.0.0 test: `jest "Feature.test.js"` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the anzen@1.0.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

¿Qué estoy haciendo mal? ¿Por qué mi simulacro no devuelve un valor? ¿Cuál es la forma correcta de simular un método de un objeto que se usa en un módulo?

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

0

Esto muestra un ejemplo de cómo simular un método de un objeto:

 import Helper from './Helper'; import Feature from './Feature'; describe('Feature', () => { beforeEach(() => { jest.spyOn(Helper.prototype, 'isValid').mockImplementation(() => true); }); it('test1', () => { const result = Feature('fake value'); console.log(result); expect(result.toString()).toBe('true'); }); it('test2', () => { const result = Feature(1); console.log(result); expect(result.toString()).toBe('true'); }); });
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!