Function Computeの自動テスト

Function Computeに対するテストはfc-helpertestを使って行います。

ここではアクセスがあったらhello world!\nというbodyと200のステータスを返すエンドポイントを想定します。

// index.js
const { hook } = require('fc-helper');

exports.handler = hook(async (ctx) => {
  ctx.body = 'hello world!\n';
});

このコードに対するテストコードは以下のようになります。

// test/index.test.js
const assert = require('assert');
const { test } = require('fc-helper');
const index = require('../index.js')

describe('hello world', () => {
  it('should return correct response', async () => {
    const res = await test(index.handler).run('{}', '{}');
    assert.equal(res.statusCode, 200, 'status code')
    assert.equal(res.body, 'hello world!\n', 'http body')
  });
});

めちゃくちゃ便利ですね。