Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when testing service with CacheBucket #72

Open
YoeriNijs opened this issue May 4, 2022 · 1 comment
Open

TypeError when testing service with CacheBucket #72

YoeriNijs opened this issue May 4, 2022 · 1 comment

Comments

@YoeriNijs
Copy link

YoeriNijs commented May 4, 2022

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:

@Laurens-makel
Copy link
Contributor

What about the following? It's not ideal but does the trick I think.

Adding another expect expect(service.bucket).toBeInstanceOf(Set); will still assert the type of the bucket to ensure the implementation did not change, making it less dangerous to use as Set or service.bucket directly.

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: service.bucket
          })
        });
        expect(service.bucket).toBeInstanceOf(Set);
        done();
      });
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants