cashew
cashew copied to clipboard
TypeError when testing service with CacheBucket
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:
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();
});
});
});