You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用aws-sdk-client-mock无法Mock关联本地依赖的DynamoDB调用

问题:使用aws-sdk-client-mock Mock本地依赖中的DynamoDB调用失败

我正尝试使用aws-sdk-client-mock库来Mock DynamoDB调用。主服务目录包含测试文件test.main-service.js和主文件index.js,同时依赖本地模块dynamoDbUtil.js(封装了DynamoDB调用逻辑)。

通过npm i和npm link关联本地依赖后,无法Mock DynamoDB的GetCommand调用——执行const mockDbClient = mockClient(DynamoDBDocumentClient);代码并未生效,测试时会直接调用真实的DynamoDB表。目前只有将本地模块复制到主服务目录并直接安装依赖,Mock才能正常工作,求关联本地模块时的解决办法。

代码库结构

-- local-modules
   -- @api
      -- dynamo-util
         -- dynamoUtil.js
         -- package.json
-- Services
   -- main-service
      -- test
         -- test.main-service.js
      -- index.js
      -- package.json

本地依赖代码:dynamoUtil.js

import {DynamoDBClient} from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
const dynamoClient = new DynamoDBClient({
    region: 'us-east-1'
});
const docClient = DynamoDBDocumentClient.from(dynamoClient);

const getTableItems = async (id) => {
    const params = {
        TableName: 'My-Demo-Table-sdkv3',
        Key: {
            Id: id
        }
    };
    const getTableCommand = new GetCommand(params);
    const result = await docClient.send(getTableCommand);
    console.log(result);
    return result;
}
// getTableItems();
export {getTableItems};

dynamo-util的package.json

{
  "name": "@api/dynamo-util",
  "version": "1.0.0",
  "main": "dynamoUtil.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@aws-sdk/client-dynamodb": "^3.744.0",
    "@aws-sdk/lib-dynamodb": "^3.744.0"
  }
}

主服务测试文件:test.main-service.js

import { should } from 'chai';
const chaiShould = should();

import { mockClient } from 'aws-sdk-client-mock';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
import { getItemsFromDB } from '../index.js';


describe('test case1', () => {
    const mockDbClient = mockClient(DynamoDBDocumentClient);
    beforeEach(() => {
        mockDbClient.reset();
    })
    it('test case for dynamodb', async () => {
        const id = 1;
        mockDbClient.on(GetCommand).resolves(
            {
                Item: {
                    id: 1,
                    name: "koko",
                    age: 32
                }
            }
        )
        const item = await getItemsFromDB(id);
        console.log(item);
    })
});

主服务文件:index.js

import { getTableItems } from '@api/dynamo-util';

const getItemsFromDB = async (id) => {
    // console.log(await getTableItems(id));
    const data = await getTableItems(id);
    return data.Item;
}

export { getItemsFromDB };

主服务的package.json

{
  "name": "main-service",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "nyc mocha --timeout=9000000"
  },
  "dependencies": {
    "@aws-sdk/client-dynamodb": "^3.750.0",
    "@aws-sdk/lib-dynamodb": "^3.750.0",
    "@api/dynamo-util": "file:./local-modules/@api/dynamo-util",
    "aws-sdk-client-mock": "^4.1.0",
    "chai": "^5.1.2",
    "mocha": "^11.1.0",
    "nyc": "^17.1.0"
  },
  "nyc": {
    "exclude": [
      "local-modules/**"
    ]
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

内容的提问来源于stack exchange,提问作者Vaibhav Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:04:58