我正在运行玩笑测试来测试dynamodb.js文件和使用dynamodb.js文件的create.js文件。 create.js模块是通用的,可以通过构造并传递param对象将其插入任何表中。但是,我在下面遇到了错误,对此我需要帮助。
TypeError: AWS.DynamoDB.DocumentClient is not a constructor
__mock__
文件夹
const getMock = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
const putMock = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
// eslint-disable-next-line func-names
function DynamoDB() {
return {
DocumentClient: jest.fn(() => ({
get: getMock,
put: putMock
}))
};
}
const AWS = { DynamoDB, getMock, putMock };
module.exports = AWS;
dynamodb.js
const AWS = require('aws-sdk');
const http = require('http');
const https = require('https');
const url = require('url');
module.exports = endpoint => {
const { protocol } = url.parse(endpoint || '');
const agentConfig = {
keepAlive: true,
keepAliveMsecs: 20000
};
const httpOptions =
protocol === 'http:' ? { agent: new http.Agent(agentConfig) } : { agent: new https.Agent(agentConfig) };
const db = new AWS.DynamoDB({
endpoint,
httpOptions
});
const docClient = new AWS.DynamoDB.DocumentClient({
service: db
});
return {
docClient,
db
};
};
dynamodb.spec.js
const AWS = require('aws-sdk');
const dynamodb = require('../../../src/dynamodb');
describe('dynamodb.js', () => {
beforeEach(() => {
// jest.resetModules();
});
test('calls generic-dynamodb-lib dynamodb', async () => {
dynamodb('http://localhost:8001');
expect(AWS.DynamoDB).toHaveBeenCalled();
expect(AWS.DynamoDB.DocumentClient).toHaveBeenCalled();
});
});
create.js
// Imports here
const create = async (log, docClient, table, tableRecord) => {
try {
await docClient.put({ TableName: table, Item: tableRecord }).promise();
} catch (error) {
log.error({ message: 'DynamoDB error', ...error });
throw Error.internal();
}
return tableRecord;
};
module.exports = create;
我还尝试用doMock块替换 mock 中的手动模拟,但仍然继续遇到上述相同错误。 一旦克服了这个问题,考虑到docClient.js传递到函数中时,如何测试create.js?非常感谢。
答案 0 :(得分:1)
DocumentClient
应该是静态属性,而被模拟为实例属性。
应该是:
const DynamoDB = jest.fn().mockReturnValue({});
DynamoDB.DocumentClient = jest.fn().mockReturnValue({
get: getMock,
put: putMock
});
答案 1 :(得分:0)
非常感谢您的回复。 在这里看到响应之前,我已经找到了解决问题的方法。
我最终不需要在@NgModule({
imports: [
MyService // wrong here
],
providers: [
MyService // should add here
]
})
export class EventsModule { }
目录中放置任何模拟。
请查看我提出的测试:
create.spec.js
__mock__
dynamodb.spec.js
const AWS = require('aws-sdk');
const dynamodb = require('../../../src/dynamodb');
const create = require('../../../src/create');
describe('create.js', () => {
beforeEach(() => {
jest.resetModules();
});
test('calls DocumentClient put with a successful outcome', async () => {
const log = { error: jest.fn() };
const fakePut = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
AWS.DynamoDB.DocumentClient = jest.fn(() => ({
put: fakePut
}));
const document = {
brands: ['visa', 'mc', 'amex', 'maestro', 'diners', 'discover', 'jcb']
};
const { docClient } = dynamodb('https://localhost:8001');
await create(log, docClient, 'a-table-name', {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
});
expect(create).toEqual(expect.any(Function));
expect(fakePut).toHaveBeenCalled();
expect(fakePut).toHaveBeenCalledWith({
TableName: 'a-table-name',
Item: {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
}
});
});
test('calls DocumentClient put with unsuccessful outcome', async () => {
const log = { error: jest.fn() };
const fakePut = jest.fn().mockImplementation(() => {
throw Error.internal();
});
AWS.DynamoDB.DocumentClient = jest.fn(() => ({
put: fakePut
}));
const document = {
brands: ['visa', 'mc', 'amex', 'maestro', 'diners', 'discover', 'jcb']
};
const { docClient } = dynamodb('https://localhost:8001');
let thrownError;
try {
await create(log, docClient, 'a-table-name', {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
});
} catch (e) {
thrownError = e;
}
expect(create).toEqual(expect.any(Function));
expect(fakePut).toHaveBeenCalled();
expect(fakePut).toHaveBeenCalledWith({
TableName: 'a-table-name',
Item: {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
}
});
expect(thrownError).toEqual(Error.internal());
});
});