Skip to content

Commit

Permalink
Add test/docs for astBody option
Browse files Browse the repository at this point in the history
  • Loading branch information
josephfrazier committed Aug 26, 2017
1 parent f3901b2 commit 23aa746
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ The `force` option forces the re-execution of an already memoized function and t
memoize.fn(fun, { force: true }).then(...
```
#### astBody
If you want to use the function AST instead the function body when generating the hash ([see serialization](#serialization)), set the option `astBody` to `true`. This allows the function source code to be reformatted without busting the cache. See https://github.com/borisdiakur/memoize-fs/issues/6 for details.
```javascript
memoize.fn(fun, { astBody: true }).then(...
```
#### noBody
If for some reason you want to omit the function body when generating the hash ([see serialization](#serialization)), set the option `noBody` to `true`.
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,45 @@ describe('memoize-fs', function () {
})
})

describe('astBody', function () {
it('should cache the result of a memoized function on second execution with option astBody set to true with equivalent function ASTs', function (done) {
var cachePath = path.join(__dirname, '../build/cache')
var memoize = memoizeFs({cachePath: cachePath})
memoize.fn(function foo () {
// double quoted
return "string" // eslint-disable-line quotes
}, {
cacheId: 'foobar',
serialize: 'qux',
astBody: true
}).then(function (memFn) {
return memFn()
}).then(function (result) {
assert.strictEqual(result, 'string', 'expected result to strictly equal "string"')
return memoize.fn(function foo () {
// single quoted
return 'string'
}, {
cacheId: 'foobar',
serialize: 'qux',
astBody: true
})
}).then(function (memFn) {
return memFn()
}).then(function (result) {
assert.strictEqual(result, 'string', 'expected result to strictly equal "string"')
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) {
if (err) {
done(err)
} else {
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar')
done()
}
})
}).catch(done)
})
})

describe('invalidate cache', function () {
it('should recache the result of a memoized function after invalidating the cache before the second execution', function (done) {
var cachePath = path.join(__dirname, '../build/cache')
Expand Down

0 comments on commit 23aa746

Please sign in to comment.