medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

Best way to test memoized functions? #88

Closed plasticrake closed 7 years ago

plasticrake commented 7 years ago

I'm trying to write a test to check that my function is using the cached value. From what I can tell the only exposed properties are __memoized__ and _has() and _get().

Also many of my memoized functions take no arguments and _has() and _get() don't seem to work on those.

var fn = (a) => { return a;};
var mfn = require('memoizee')(fn);
mfn._has(); // false
mfn('test');
mfn._has('test'); // true
var fn = () => { return 'test';};
var mfn = require('memoizee')(fn);
mfn._has(); // **Uncaught TypeError: get is not a function(…)**
mfn();
mfn._has(); // **Uncaught TypeError: get is not a function(…)**

Is there an example on the best way to test this? I looked at some of the test scripts here but it looked like it kept a 'cache miss' counter in the original function.

medikoo commented 7 years ago

@plasticrake Firstly, tests confirming that memoization works as expected are included in memoizee. I don't think it make sense to double them in any utilities that rely on memoizee.

Still I understand that you may want to conduct some sanity check, to confirm that indeed given function is memoized (so wrappped with memoizee or any other memoization provider).

Doing that library agnostic way would be to just run function twice and configure tests against observed side effects. Here I'm not sure what exactly your function does, so I'm not sure how such test should look on your side

Or if that for some reason is difficult (still I would wonder why?), then you may test it in less agnostic way as e.g.:

fn.__memoized__ === true

I'm closing it, but you still have some doubts let's continue discussion here

epayet commented 7 years ago

I think @plasticrake pointed out a real issue.

A few tests, running on node:8.4.0:

Everything works fine with this:

> var fn = (a) => { return a;};
> var mfn = require('memoizee')(fn);
> mfn.__memoized__
true
> mfn._has();
false
> mfn('test');
'test'
> mfn._has('test'); // true
true

But it crashes with the example he gave:

> var fn = () => { return 'test';};
> var mfn = require('memoizee')(fn);
> mfn.__memoized__
true
> mfn._has();
TypeError: get is not a function
    at Function.<anonymous> (/node_modules/memoizee/lib/configure-map.js:172:8)
    at repl:1:5
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)
    at REPLServer.Interface._onLine (readline.js:279:10)
> mfn();
'test'

It seems that the function get is undefined when the original function doesn't take any parameters?

medikoo commented 7 years ago

Great thanks @epayet indeed, I didn't read as through the example

Fixed with 7cb1c7a603abcedc9cc021dc700a3d14b4791c80

Published as v0.4.11