question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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.

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:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
thymikeecommented, Nov 14, 2018

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found