使用 jest 对 aws lambda 进行单元测试 [英] Unit test for aws lambda using jest

查看:41
本文介绍了使用 jest 对 aws lambda 进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const invokeApi = require("/opt/nodejs/kiwiCall");
const decrypt = require("/opt/nodejs/encryption");
const cors = require("/opt/nodejs/cors");

当我通过手动模拟 mocks 目录中的这些依赖项来测试我的 index.js 文件时,如下所示:

When I am testing my index.js file by manual mocking these dependencies in mocks directory as follows:

__mocks__ 
    |_invokeApi
    |_decrypt
    |_cors

它说

FAIL  ./index.test.js
  ● Test suite failed to run

    Cannot find module '/opt/nodejs/kiwiCall' from 'index.js'

    However, Jest was able to find:
        '../../../../lambdas/Flights/Locations/index.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      1 | "use strict";
      2 | 
    > 3 | const invokeApi = require("/opt/nodejs/kiwiCall");

想知道如何在 inedx.test.js 文件中模拟 AWS lambda 的依赖项

Wanted to know how can I mock the dependencies of AWS lambda in inedx.test.js file

推荐答案

在你的 package.json 或 jest.config 中,你可以为那个目录添加一个 moduleNameMapper.

In your package.json or jest.config you could add a moduleNameMapper for that directory.

  "jest": {
    "moduleNameMapper": {
      "/opt/nodejs/(.*)": "<rootDir>/../nodejs/$1"
    },
  },

这篇关于使用 jest 对 aws lambda 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆