Support new Cache API
See original GitHub issue🚀 Feature Proposal
Support the Cache API in the same way localStorage is supported. One of the ways to access it would be window.caches
.
Note: this is an experimental API but I think right now would be a good moment to start review and prepare the changes to support it.
- https://developer.mozilla.org/en-US/docs/Web/API/Cache
- https://googlechrome.github.io/samples/service-worker/window-caches
Motivation
Be able to test classes using the Cache API without have to mock all methods.
Example
Code to be tested:
export class CacheAPI {
#cacheKey = 'container-key';
async get(request: RequestInfo): Promise<Response> {
const cache = await caches.open(this.#cacheKey);
await cache.add(request);
return await cache.match(request);
}
}
Test:
describe('CacheAPI', () => {
const api = new CacheAPI();
describe('get', () => {
it('should be able to execute get request', async () => {
const scope = nock('http://site.com')
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
.get('/sample')
.reply(200);
const response = await api.get('http://site.com/sample');
expect(response.status).toBe(200);
expect(scope.isDone()).toBeTruthy();
});
it('should throw an error if the request fails', async () => {
const scope = nock('http://site.com')
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
.get('/sample')
.reply(500);
try {
await api.get('http://site.com/sample');
} catch (err) {
expect(err.message).toBe('Internal server error');
}
expect(scope.isDone()).toBeTruthy();
});
});
});
Right now if I try to run this test the error that I receive is:
ReferenceError: caches is not defined
at CacheAPI._callee$ (src/service/CacheAPI.js:239:17)
at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
at asyncGeneratorStep (src/service/CacheAPI.js:192:130)
at _next (src/service/CacheAPI.js:194:194)
at src/service/CacheAPI.js:194:364
at CacheAPI.<anonymous> (src/service/CacheAPI.js:194:97)
at CacheAPI.get (src/service/CacheAPI.js:294:21)
Pitch
ServiceWorks usage has increased and this feature will help everyone who are looking to test their online/offline api calls…
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Cache - Web APIs | MDN
The Cache interface provides a persistent storage mechanism for Request / Response object pairs that are cached in long lived memory.
Read more >The Cache API: A quick guide - web.dev
The Cache API was created to enable service workers to cache network requests so that they can provide fast responses, regardless of network ......
Read more >An overview of Deno's new Cache API support - Bits and Pieces
This is an API that browsers can implement and allows them to cache (well, clearly) a response for every request made. Given the...
Read more >Enabling API caching to enhance responsiveness
You can enable API caching in Amazon API Gateway to cache your endpoint's responses. With caching, you can reduce the number of calls...
Read more >Working with the JavaScript Cache API - LogRocket Blog
This article shows how the Cache API can be used in a service worker, ... We can create a new cache object using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There is https://github.com/theneva/jest-environment-jsdom-twelve and https://github.com/theneva/jest-environment-jsdom-thirteen which are updated
This is a request to
jsdom
, Jest is DOM-independent. Please move this issue there. Also please note that we’re also stuck on older version of jsdom because it drops older Node version pretty fast – and we don’t. You can use a custom jsdom environment though where you can use whichever version you prefer (see this for example: https://github.com/dmnsgn/jest-environment-jsdom-latest).