Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

node.js - How to stub nested dependecies with ts-sinon

I got a simple unit test with the following code:

my-pubsub.spec.ts

import * as tsSinon from 'ts-sinon';

import { myPubSubFunction } from './my-pubsub';
import * as sendEmail from './send-mail';


describe("Notifications PubSub tests", () => {

  it("Should trigger audit", (done) => {
    const today = new Date()
    const data = {
     ( my data )
    }

    const spy = tsSinon.default.spy(sendEmail, "sendNotificationMessage")

    const dataBuffer = Buffer.from(JSON.stringify(data))

    // Call tested function and verify its behavior
    myPubSubFunction(dataBuffer)

    setTimeout(() => {
      // check if spy was called
      tsSinon.default.assert.calledOnce(spy)
      done()
    }, 100)
  })
})

And my-pubsub.ts got a call to a function from send-mail with contains a function to set the Api key

import * as sgMail from '@sendgrid/mail';

sgMail.setApiKey(
  "MyKey"
) // error in here

export function sendNotificationMessage(mailConfig: any) {
const defaultConfig = {
    from: {
      email: "noreply@mymail.com",
      name: "my name",
    },
    template_id: "my template",
  }

  const msg = { ...defaultConfig, ...mailConfig }

  return sgMail.send(msg)
}

However when running my tests I got the following error TypeError: sgMail.setApiKey is not a function

Edit: added a bit more code to the send-mail code. Bellow you can find a bit more code about my-pubsub.ts

my-pubsub.ts

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import moment = require('moment');

import { IModel, ModelType } from '../models/model.model';
import { sendNotificationMessage } from '../shared/send-mail';

const { PubSub } = require("@google-cloud/pubsub")

try {
  admin.initializeApp()
} catch (e) {}
const db = admin.firestore()
const pubSubClient = new PubSub()

export const myPubSubTrigger = functions.pubsub
  .topic("on-trigger")
  .onPublish(async (message) => {
    console.log("version 1")

    const myMessage = Buffer.from(message.data, "base64").toString("utf-8")

    const data: IModel = JSON.parse(myMessage)

    ( logic to create my object )

    /**
     * Send email
     */
    const result: any = await sendNotificationMessage(myObject)

    /**
     * Check result
     */
    if (result[0].statusCode === 202) {
        await docRef.update({ emailSent: true })
    } 
   ( another publish to audit the action )
  })
question from:https://stackoverflow.com/questions/65900637/how-to-stub-nested-dependecies-with-ts-sinon

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is not with tests per se, but incorrect types definition of @sendgrid/mail:

// OK
import sgMail from "@sendgrid/mail";
import { default as sgMail2 } from "@sendgrid/mail";

console.log(sgMail === sgMail2);

sgMail.setApiKey("SG.key");
sgMail2.setApiKey("SG.key2");

// BROKEN
// type definition does not match runtime shape
import * as sgMailIncorrectlyTyped from "@sendgrid/mail";
console.log(sgMailIncorrectlyTyped, sgMailIncorrectlyTyped.setApiKey === undefined);

STACKBLITZ


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...