在我的无服务器应用程序上使用 Jest 进行测试

Make test with Jest on my serverless application

我有一个无服务器应用程序,我想对其进行单元测试

我知道 Jest 有一个无服务器插件,但我现在使用经典的 Jest。

我在 tools.js 文件中有一个这样的函数

const ddb = require('./ddb');
const table = process.env.USER_TABLE;

...

module.exports.mailExist = async email => {
  if (!email) {
    throw new Error('Missing parameters');
  }
  return await ddb.scan({
    TableName: table,
    FilterExpression: 'email = :email',
    ExpressionAttributeValues: {
      ':email': email
    },
    ProjectionExpression: ['uid']
  });
};

我想在 __test__/tools.test.js 文件中测试它

describe('mailExist', async () => {
    const email = 'example@example.com';
    const mailExist = await tools.mailExist(email)
    it('should not exist', () => {
        expect(mailExist).toBe({})
    })
});

问题是,当我 运行 我的测试时,它 return

Missing required key 'TableName' in params

因为我的'table'变量是一个环境变量

如果我写我的 table 名字因为环境变量,它 return

Missing region in config

所以我的问题是,我想知道如何在特定文件中测试我的无服务器应用程序的功能以及如何将我的 AWS 配置传递给我的功能?

如有疑问,请告诉我

谢谢

PS : 我在 ddb 文件中的扫描函数如下所示

//ddb.js

module.exports.scan = async params => {
    const result = await DynamoDB.scan(params).promise();
    return result.Items;
};

这是一个单元测试解决方案:

tools.js:

const ddb = require('./ddb');

const table = process.env.TableName;

module.exports.mailExist = async email => {
  if (!email) {
    throw new Error('Missing parameters');
  }
  return await ddb.scan({
    TableName: table,
    FilterExpression: 'email = :email',
    ExpressionAttributeValues: {
      ':email': email
    },
    ProjectionExpression: ['uid']
  });
};

tools.spec.js,在需要tools.js之前,需要先设置环境变量。我们将原来的TableName环境变量存储到一个变量中,待所有单元测试完成后恢复环境变量

const ddb = require('./ddb');

describe('mailExist', () => {
  const TableName = process.env.TableName;
  const fakeTableName = 'fake table name';
  beforeAll(() => {
    process.env.TableName = fakeTableName;
  });
  afterAll(() => {
    process.env.TableName = TableName;
  });

  test('should not exist', async () => {
    const tools = require('./tools');
    const email = 'example@example.com';
    const scanSpy = jest.spyOn(ddb, 'scan').mockResolvedValueOnce({});
    const actualValue = await tools.mailExist(email);
    expect(actualValue).toEqual({});
    expect(scanSpy).toBeCalledWith({
      TableName: fakeTableName,
      FilterExpression: 'email = :email',
      ExpressionAttributeValues: {
        ':email': email
      },
      ProjectionExpression: ['uid']
    });
  });
});

单元测试结果:

PASS  src/Whosebug/58727351/tools.spec.js
  mailExist
    ✓ should not exist (112ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |       80 |    66.67 |    66.67 |       75 |                   |
 ddb.js   |       50 |      100 |        0 |       50 |                 3 |
 tools.js |     87.5 |    66.67 |      100 |    83.33 |                 7 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.095s, estimated 8s