Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何通过玩笑模仿AWS TimestreamWrite_Node.js_Amazon Web Services_Aws Lambda_Jestjs_Amazon Timestream - Fatal编程技术网

Node.js 如何通过玩笑模仿AWS TimestreamWrite

Node.js 如何通过玩笑模仿AWS TimestreamWrite,node.js,amazon-web-services,aws-lambda,jestjs,amazon-timestream,Node.js,Amazon Web Services,Aws Lambda,Jestjs,Amazon Timestream,这个项目是通过AWS Timestream记录数据的,它工作得很好 然而,我没有通过使用玩笑来模仿AWS TimestreamWrite。我尝试了一些方法,但没有奏效。有人能帮我吗 我的文件如下: ledger-service.js ledger-service.spec.js 我只是觉得我模仿的AWS没有传递到分类账服务.js。有办法解决这个问题吗 谢谢 更新:采纳hoangdv的建议 我在想jest.resetModules();jest.resetAllMocks()不工作。如果我把“它应

这个项目是通过AWS Timestream记录数据的,它工作得很好

然而,我没有通过使用玩笑来模仿AWS TimestreamWrite。我尝试了一些方法,但没有奏效。有人能帮我吗

我的文件如下:

ledger-service.js ledger-service.spec.js 我只是觉得我模仿的AWS没有传递到
分类账服务.js
。有办法解决这个问题吗

谢谢

更新:采纳hoangdv的建议 我在想
jest.resetModules();jest.resetAllMocks()不工作。如果我把“它应该在所有成功的时候写记录”作为第一个测试,它就会通过测试。然而,如果在它之前有一个,它将失败

通过 失败
ledger service.js
中,您在“
module.exports
之前调用
new AWS.TimestreamWrite
”,这意味着将使用实际逻辑而不是模拟调用它

解决方案是在调用
require(“./ledger service”)之前模拟AWS

分类账服务.spec.js

const AWS = require("aws-sdk");

describe("ledger-service", () => {
  let audit;
  let mockWriteRecords;

  beforeEach(() => {
    mockWriteRecords = jest.fn(() => {
      return { promise: () => Promise.resolve() }
    });

    jest.spyOn(AWS, 'TimestreamWrite');
    AWS.TimestreamWrite.mockImplementation(() => ({
      writeRecords: mockWriteRecords
    }));

    audit = require("./ledger-service"); // this line
  });

  afterEach(() => {
    jest.resetModules(); // reset module to update change for each require call
    jest.resetAllMocks();
  });

  it("It should write records when all success", async () => {
    const mockAudit = {
      name: 'testName',
      value: 'testValue',
      userId: 'testUserId',
      entity: 'testEntity',
      action: 'testAction',
      info: 'testInfo',
    };

    await audit.log(mockAudit);

    expect(AWS.TimestreamWrite).toHaveBeenCalledWith({
      maxRetries: 10,
      httpOptions: {
        timeout: 20000,
        agent: expect.any(Object),
      },
    });
    expect(mockWriteRecords).toHaveBeenCalled();
  });
});


谢谢,它对我有用,但我认为,
resetModules()
resetAllMocks
不起作用,请看我的更新
const AWS = require("aws-sdk");
const audit = require("./ledger-service");

describe("ledger-service", () => {

    beforeEach(async () => {
        jest.resetModules();
    });
  
    afterEach(async () => {
      jest.resetAllMocks();
    });

    it("It should write records when all success", async () => {
        const mockAudit={
            name: 'testName',
            value: 'testValue',
            userId: 'testUserId',
            entity: 'testEntity',
            action: 'testAction',
            info: 'testInfo',
        };

        const mockWriteRecords = jest.fn(() =>{
            console.log('mock success')
            return { promise: ()=> Promise.resolve()}
         });

        const mockTsClient={
            writeRecords: mockWriteRecords
        }

        jest.spyOn(AWS,'TimestreamWrite');
        AWS.TimestreamWrite.mockImplementation(()=>mockTsClient);

        //a=new AWS.TimestreamWrite();
        //a.writeRecords();   //these two lines will pass the test and print "mock success"

        await audit.log(mockAudit); //this line will show "ConfigError: Missing region in config"

        expect(mockWriteRecords).toHaveBeenCalled();
    });
});
  it("It should write records when all success", async () => {
    const mockAudit = {
      name: 'testName',
      value: 'testValue',
      userId: 'testUserId',
      entity: 'testEntity',
      action: 'testAction',
      info: 'testInfo',
    };

    await audit.log(mockAudit);

    expect(AWS.TimestreamWrite).toHaveBeenCalledWith({
      maxRetries: 10,
      httpOptions: {
        timeout: 20000,
        agent: expect.any(Object),
      },
    });
    expect(mockWriteRecords).toHaveBeenCalled();
  });

  it("It should throw error when audit is empty", async () => {
    const mockAudit = {};

    await expect(audit.log(mockAudit)).rejects.toThrow(`Audit object is empty`);
  });
  it("It should throw error when audit is empty", async () => {
    const mockAudit = {};

    await expect(audit.log(mockAudit)).rejects.toThrow(`Audit object is empty`);
  });

  it("It should write records when all success", async () => {
    const mockAudit = {
      name: 'testName',
      value: 'testValue',
      userId: 'testUserId',
      entity: 'testEntity',
      action: 'testAction',
      info: 'testInfo',
    };

    await audit.log(mockAudit);

    expect(AWS.TimestreamWrite).toHaveBeenCalledWith({
      maxRetries: 10,
      httpOptions: {
        timeout: 20000,
        agent: expect.any(Object),
      },
    });
    expect(mockWriteRecords).toHaveBeenCalled();
  });
const AWS = require("aws-sdk");

describe("ledger-service", () => {
  let audit;
  let mockWriteRecords;

  beforeEach(() => {
    mockWriteRecords = jest.fn(() => {
      return { promise: () => Promise.resolve() }
    });

    jest.spyOn(AWS, 'TimestreamWrite');
    AWS.TimestreamWrite.mockImplementation(() => ({
      writeRecords: mockWriteRecords
    }));

    audit = require("./ledger-service"); // this line
  });

  afterEach(() => {
    jest.resetModules(); // reset module to update change for each require call
    jest.resetAllMocks();
  });

  it("It should write records when all success", async () => {
    const mockAudit = {
      name: 'testName',
      value: 'testValue',
      userId: 'testUserId',
      entity: 'testEntity',
      action: 'testAction',
      info: 'testInfo',
    };

    await audit.log(mockAudit);

    expect(AWS.TimestreamWrite).toHaveBeenCalledWith({
      maxRetries: 10,
      httpOptions: {
        timeout: 20000,
        agent: expect.any(Object),
      },
    });
    expect(mockWriteRecords).toHaveBeenCalled();
  });
});