title: Why is the spyOn function in the test not working with sendGrid?
body: <p>I am setting up a graphql server with <code>graphql-yoga</code> and `prisma using Typescript. When a user signs up, an email with a link for validation will be sent to the given email address.
  Everything is working fine, but i want to write a test for the mutation before refactoring the functionality, which checks if the 'send' function of SendGrid hast been called.</p>
  <p>I tried spying on the function with <code>jest.spyOn</code>, but all I get is an error, that comes from not providing an API key for SendGrid in the tesing environment.</p>
  <p>I have used spyOn before, and it worked, though this is the first time I am using jest with Typescript.</p>
  <p>SignUp Mutation</p>
  <pre><code>import * as sgMail from '@sendgrid/mail';
  
  sgMail.setApiKey(process.env.MAIL_API_KEY);
  
  export const Mutation = {
      async signUpUser(parent, { data }, { prisma }, info) {
          [...]
          const emailData = {
              from: 'test@test.de',
              to: `${user.email}`,
              subject: 'Account validation',
              text: `validation Id: ${registration.id}`
          };
          await sgMail.send(emailData);
  
          return user;
      }
  }
  </code></pre>
  <p>Trying spyOn</p>
  <pre><code>import * as sgMail from '@sendgrid/mail';
  
  const signUpUserMutation = gql`
      mutation($data: ValidationInput) {
          signUpUser (data: $data) {
              id
              email
          }
      }
  `;
  
  it('should send a registration email, with a link, containing the id of the registration', async () =&gt; {
      spyOn(sgMail, &quot;send&quot;).and.returnValue(Promise.resolve('Success'));
      const variables = {
          data: {
              email: &quot;test@test.de&quot;,
              password: &quot;anyPassword&quot;
          }
      };
  
      await client.mutate({ mutation: signUpUserMutation, variables});
      expect(sgMail.send).toHaveBeenCalled();
  });
  </code></pre>
  <p>Running the test gives me:</p>
  <blockquote>
  <p>Error: GraphQL error: Unauthorized</p>
  </blockquote>
  <p>Commenting out the function call of send in the mutation and running the test gives me:</p>
  <blockquote>
  <p>Error: expect(spy).toHaveBeenCalled()</p>
  <p>Expected spy to have been called, but it was not called.</p>
  </blockquote>