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.

[RESTDataSource] Unable to run tests with Jest

See original GitHub issue

Whenever I’m trying to run a test using a class that extends RESTDataSource and call the this.get(...) method, I have the following error TypeError: Cannot read property 'fetch' of undefined The thing is I want to test the real output of the request, so mocking the fetch method doesn’t seems to be an appropriate solution.

Some related files are: https://github.com/apollographql/apollo-server/blob/master/__mocks__/apollo-server-env.ts https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource-rest/src/HTTPCache.ts#L11

How can I get ride of this error ? Is there any example that could show me how test a rest data source ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
Boralescommented, Sep 7, 2018

@melkir try to call it like

const gApi = new GoogleAPI();
// this method is being called by ApolloServer
gApi.initialize({ context:{} }):

// your tests go below 
gApi.getGroups();

6reactions
iamam34commented, Jan 4, 2019

I had the same issue. This line makes it compile with no typescript warnings:

import { KeyValueCache } from 'apollo-server-caching';
...
gApi.initialize({ context: {}, cache: {} as KeyValueCache });

But if you need a real cache to fix [TypeError: this.keyValueCache.get is not a function, use

import { InMemoryLRUCache } from 'apollo-server-caching';
...
gApi.initialize({ context: {}, cache: new InMemoryLRUCache() });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest test fials with error "DataSource is not set for this entity"
The issue appears in tests made with jest and I got the following error: "DataSource is not set for this entity". Am i...
Read more >
How to write tests for Prisma with Docker and Jest
src/prisma/schema.prisma datasource db { provider = "mysql" url ... We can't run tests unless we write a function to test first .
Read more >
Jest CLI Options
Run tests related to changed files based on hg/git (uncommitted files): ... Exit the test suite immediately upon n number of failing test...
Read more >
apollo-datasource-rest - npm
This package exports a ([`RESTDataSource`](https://github.com/ ... Start using apollo-datasource-rest in your project by running `npm i ...
Read more >
please make sure that the argument datasource at index [0] is ...
mockResolvedValue(mockContact), find: jest.fn(). ... when i run the test,fail with Nest can't resolve dependencies of the OrderService (?).
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