Google Cloud Datastore jest nodejs node_modules

时间:2017-11-06 01:29:10

标签: node.js npm google-cloud-datastore jest

我试图通过模拟runQuery和createQuery函数对下面的listEntities函数进行单元测试。也许我应该放弃并与模拟器进行集成测试。无论如何,这是我的代码

实现:

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)

谢谢!

1 个答案:

答案 0 :(得分:1)

首先应该完成单元测试,然后是集成测试。单元测试比集成测试更容易编写,并且具有良好的隔离性,不依赖外部服务,没有副作用并且可以在不同的环境中运行。

这是单元测试解决方案:

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