Skip to content

TypeError when testing service with CacheBucket #72

@YoeriNijs

Description

@YoeriNijs

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

The TypeScript compiler throws a TypeError when one is testing a service that uses the CacheBucket.

Example service:

@Injectable()
export class DocumentService {

  private _documentBucket = new CacheBucket();

  constructor(private http: HttpClient) {}

  /**
   * Returns a cached document
   * @param documentId
   */
  getDocument(documentId: string): Observable<EbDocument> {
    return this.http.get<EbDocument>(`/api/documents/${documentId}`, {
      context: withCache({
        ttl: 10_000,
        bucket: this._documentBucket
      })
    });
  }
}

Which causes the following in a Spectator driven test that is executed by Jest (we do not use the createHttpFactory here since we explicitly want to validate the cache context):

describe('DocumentService', () => {
  const createService = createServiceFactory<DocumentService>({
    service: DocumentService,
    mocks: [HttpClient]
  });

  it('should call the endpoint', done => {
    const spectator = createService();
    const httpClient = spectator.inject(HttpClient);
    httpClient.get.andReturn(of({}));

    spectator.service
      .getDocument('documentId')
      .subscribe(() => {
        expect(httpClient.get).toHaveBeenCalledWith('/api/documents/documentId', {
          context: withCache({
            ttl: 10_000,
            bucket: new CacheBucket()
          })
        });
        done();
      });
  });
});
TypeError: Constructor Set requires 'new'
        at CacheBucket.Set (<anonymous>)

Since CacheBucket only extends Set without applying anything else, it is possible to fix this issue by typing the CacheBucket as Set here, but this is dangerous since the implementation may change over time.

Expected behavior

We should fix this, so we have a happy compiler.

Minimal reproduction of the problem with instructions

Angular >= 12 with TypeScript 4.3.5 and Cashew 2.3.2.

What is the motivation / use case for changing the behavior?

Environment


Angular version: 12.2.16


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions