Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Datastore jest nodejs node_modules

I am trying to unit test the below listEntities function by mocking runQuery and createQuery functions. Maybe I should just give up and do an integration test with an emulator. Anyway, here is my code

Implementation:

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]);
};

Test:

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);
    });
  });
});

This is the error I get

 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)

Thank you!

like image 567
jcgh582 Avatar asked Nov 06 '17 01:11

jcgh582


1 Answers

Unit testing should be completed first, followed by integration testing. Unit tests are easier to write than integration tests, and have good isolation, do not rely on external services, have no side effects, and can run in different environments.

Here is the unit test solution:

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');
    });
  });
});

unit test result with coverage report:

 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

source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/47128513

like image 163
slideshowp2 Avatar answered Oct 31 '22 21:10

slideshowp2