Search code examples
javascripttypescriptmocha.jsprobot

How to correctly configure unit tests for Probot with Scheduler Extension?


I am using the following minimal probot app and try to write Mocha unit tests for it.

Unfortunately, it results in the error below, which indicates that some of my setup for the private key or security tokens is not picked up.

I assume that the configuration with my .env file is correct since I do not get the same error when I start the probot via probot-run.js.

Are there any extra steps needed to configure probot when used with Mocha? Any suggestions on why the use of the scheduler extension may result in such issue would be great.

Code and error below:

app.ts

import createScheduler from "probot-scheduler";
import { Application } from "probot";

export = (app: Application) => {

  createScheduler(app, {
    delay: !!process.env.DISABLE_DELAY, // delay is enabled on first run
    interval: 24 * 60 * 60 * 1000 // 1 day
  });

  app.on("schedule.repository", async function (context) {
    app.log.info("schedule.repository");
    const result = await context.github.pullRequests.list({owner: "owner", repo: "test"});
    app.log.info(result);
  });
};

test.ts

import createApp from "../src/app";

import nock from "nock";
import { Probot } from "probot";

nock.disableNetConnect();

describe("my scenario", function() {
  let probot: Probot;
  beforeEach(function() {
    probot = new Probot({});
    const app = probot.load(createApp);
  });

  it("basic feature", async function() {
    await probot.receive({name: "schedule.repository", payload: {action: "foo"}});
  });
});

This unfortunately results in the following error:

 Error: secretOrPrivateKey must have a value
  at Object.module.exports [as sign] (node_modules/jsonwebtoken/sign.js:101:20)
  at Application.app (node_modules/probot/lib/github-app.js:15:39)
  at Application.<anonymous> (node_modules/probot/lib/application.js:260:72)
  at step (node_modules/probot/lib/application.js:40:23)
  at Object.next (node_modules/probot/lib/application.js:21:53)

Solution

  • Turns out that new Probot({}); as suggested in the documentation initializes the Probot object without any parameters (the given options object {} is empty after all).

    To avoid the error, one can provide the information manually:

    new Probot({
      cert: "...",
      secret: "...",
      id: 12345
    });