node.js - Google Cloud Datastore 开 Jest nodejs node_modules

标签 node.js unit-testing npm jestjs google-cloud-datastore

我正在尝试对以下内容进行单元测试 listEntities模拟功能 runQuerycreateQuery职能。也许我应该放弃并使用模拟器进行集成测试。无论如何,这是我的代码
执行:

const Datastore = require('@google-cloud/datastore');
const datastore = Datastore();

const runQueryDS = (query) => datastore.runQuery(query);
const createQueryDS = (kind) => datastore.createQuery(kind);

export const listEntities = (kind, runQuery = runQueryDS, createQuery = createQueryDS) => {
  console.log('listEntities');
  const query = createQuery(kind);
  runQuery(query).then((results) => results[0]);
};
测试:
import { listEntities } from './datastore.api';

describe('datastore api', () => {
  describe('listEntities', () => {
    test('should return list of items', () => {
      console.log('begin test');
      const kind = 'TestRun';
      const createdQuery = 'createdQuery';
      const expectedResult = ['returnedFromQuery'];
      const returnedFromExecutedQuery = [expectedResult];

      const createQuery = jest.fn().mockImplementation(() => (createdQuery));
      const runQuery = jest.fn().mockImplementation(() => (returnedFromExecutedQuery));

      const result = listEntities(kind, runQuery, createQuery);
      expect(result).toEqual(expectedResult);
    });
  });
});
这是我得到的错误
 FAIL  app/datastore.api.test.js
● Test suite failed to run

Cannot find module './datastore_client_config' from 'datastore_client.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
  at Object.<anonymous> (node_modules/@google-cloud/datastore/src/v1/datastore_client.js:30:18)
谢谢!

最佳答案

应该首先完成单元测试,然后是集成测试。单元测试比集成测试更容易编写,并且隔离性好,不依赖外部服务,没有副作用,可以在不同环境下运行。
这是单元测试解决方案:index.js :

const Datastore = require('@google-cloud/datastore');
const datastore = Datastore();

const runQueryDS = (query) => datastore.runQuery(query);
const createQueryDS = (kind) => datastore.createQuery(kind);

const listEntities = (kind, runQuery = runQueryDS, createQuery = createQueryDS) => {
  console.log('listEntities');
  const query = createQuery(kind);
  return runQuery(query).then((results) => results[0]);
};

module.exports = { listEntities };
index.test.js :
const { listEntities } = require('./');
const Datastore = require('@google-cloud/datastore');

jest.mock('@google-cloud/datastore', () => {
  const mDatasotre = {
    runQuery: jest.fn(),
    createQuery: jest.fn(),
  };
  return jest.fn(() => mDatasotre);
});

describe('47128513', () => {
  describe('#listEntities', () => {
    afterAll(() => {
      jest.resetAllMocks();
    });
    it('should list entities', async () => {
      const mDatastore = Datastore();
      mDatastore.createQuery.mockReturnValueOnce('fake query');
      mDatastore.runQuery.mockResolvedValueOnce([{ id: 1 }]);
      const actual = await listEntities('kind');
      expect(actual).toEqual({ id: 1 });
      expect(mDatastore.createQuery).toBeCalledWith('kind');
      expect(mDatastore.runQuery).toBeCalledWith('fake query');
    });
  });
});
带有覆盖率报告的单元测试结果:
 PASS  src/stackoverflow/47128513/index.test.js (12.111s)
  47128513
    #listEntities
      ✓ should list entities (12ms)

  console.log src/stackoverflow/47128513/index.js:355
    listEntities

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.js |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        13.865s, estimated 15s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/47128513

关于node.js - Google Cloud Datastore 开 Jest nodejs node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128513/

相关文章:

javascript - 如何检查函数是否抛出包含 Jest 文本的错误?

npm - 错误 : No provider for "framework:jasmine"!(正在解析:框架:jasmine)

node.js - TypeError : callSite. getFileName 不是 npm start 上 callSiteLocation (/node_modules/depd/index.js:252:23) 的函数

node.js - Express 应用中的 XSS 保护

node.js - Express js 多种操作gm(GraphicsMagick)模块

javascript - 如何在 Angular 中对组件进行单元测试?

android - 使用真实实现(类)进行单元测试

node.js - docker 容器中安装的 npm 不可靠

javascript - 如何在 socketio 返回中中继 PHP 函数/变量

javascript - 如何强制 JSON.parse 抛出数字?