首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aws当使用类型记录和ts-jest时,模拟而不是嘲笑

aws当使用类型记录和ts-jest时,模拟而不是嘲笑
EN

Stack Overflow用户
提问于 2021-03-02 16:36:01
回答 1查看 1.2K关注 0票数 1

我在使用aws-sdk-mock库和使用ts-jest的类型记录时遇到了一些困难。我正在从aws模拟主页运行示例测试,如下所示。然而,当我使用ts-jest运行这个测试时,我会得到以下异常:

代码语言:javascript
复制
ValidationException: 2 validation errors detected: Value '' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 3; Value '' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+

这告诉我,AWS还没有被模仿。我已经在下面的测试文件,package.jsonjest.config.jsjsconfig.jsontsconfig.json

有什么原因吗?

demo.test.ts

代码语言:javascript
复制
import * as AWSMock from 'aws-sdk-mock';
import * as AWS from 'aws-sdk';
import { GetItemInput } from 'aws-sdk/clients/dynamodb';

beforeAll(async (done) => {
  //get requires env vars
  done();
});

describe.only('the module', () => {
  it('should mock getItem from DynamoDB', async () => {
    // Overwriting DynamoDB.getItem()
    AWSMock.setSDKInstance(AWS);
    AWSMock.mock('DynamoDB', 'getItem', (params: GetItemInput, callback: Function) => {
      console.log('DynamoDB', 'getItem', 'mock called');
      callback(null, { pk: 'foo', sk: 'bar' });
    });

    let input: GetItemInput = { TableName: '', Key: {} };
    const dynamodb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
    expect(await dynamodb.getItem(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });

    AWSMock.restore('DynamoDB');
  });
});

package.json

代码语言:javascript
复制
{
  "name": "test-project",
  "version": "1.0.0",
  "description": "",
  "main": "app.ts",
  "repository": "",
  "author": "",
  "license": "MIT",
  "private": true,
  "scripts": {
    "test": "NODE_ENV=test jest",
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.71",
    "@types/axios": "^0.14.0",
    "@types/jest": "^26.0.20",
    "@types/node": "^14.14.25",
    "@types/uuid": "^8.3.0",
    "aws-sdk": "^2.841.0",
    "aws-sdk-mock": "^5.1.0",
    "jest": "^26.6.3",
    "ts-jest": "^26.5.1",
    "typescript": "^4.1.5"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "dotenv": "^8.2.0",
    "uuid": "^8.3.2"
  }
}

jest.config.js

代码语言:javascript
复制
module.exports = {
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(tsx?)$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};

jsconfig.json

代码语言:javascript
复制
{ "typeAcquisition": { "include": [ "jest" ] } }

tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "target": "es2017",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "outDir": "./dist/",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "include": ["src/*.ts", "__tests__/**/*.ts"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-03 11:26:33

我自己偶然找到了答案,但我不知道为什么会这样。

我改变了我的tsconfig.json,将esModuleInterop设置为false,我的所有问题都消失了。

代码语言:javascript
复制
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "target": "es2017",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "outDir": "./dist/",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    "esModuleInterop": false,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "include": ["src/*.ts", "__tests__/**/*.ts"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66443411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档