1

What is the way to mock methods in unit tests using tape in Node js? I did not find anything in readme. I am pretty new to Node js, so this question seems pretty simple to some people.

Please provide some suggestions.

Chacha
  • 411
  • 1
  • 5
  • 12
  • Mocking is as simple as replacing a method. `object.method = function mock() { mockImplementation }` – Aluan Haddad Nov 02 '17 at 04:33
  • 1
    Still not clear how it can be done from test cases. Please provide some more context. – Chacha Nov 02 '17 at 21:24
  • @Chacha are you asking what is a mock? – Emobe Aug 12 '19 at 16:42
  • Let's say you have an object, myObject. It has a method: doSomethingExpensive(). You want to stub out that method for testing and have the stub return zero. All you need to do is put this line in your test case: myObject.doSomethingExpensive = function mock() { return 0; } – Dausuul Mar 23 '21 at 22:52

1 Answers1

1

tape is a lightweight test library. It doesn't support mock,spy,stub or fake features. It tends to do the underlying testing which means your code doesn't depend on any external resource, E.g. database, web service, http request and so on

If your code very depends on these external resource, you can use

  • sinon has mock/spy/stub/fake feature.
  • jest has mock/spy feature

Both of them are ok.

Lin Du
  • 88,126
  • 95
  • 281
  • 483