Giter Site home page Giter Site logo

geek-fun / jest-search Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 0.0 451 KB

Jest preset for running tests with local ElasticSearch, OpenSearch and ZincSearch.

Home Page: https://www.npmjs.com/package/@geek-fun/jest-search

License: MIT License

JavaScript 4.03% TypeScript 95.97%
elasticsearch integration-test jest jest-preset opensearch tdd-javascript tdd-typescript es jest-environment tdd zincsearch elasticsearch-local es-local jest-elasticsearch jest-opensearch jest-zincsearch opensearch-local zincsearch-local

jest-search's Introduction

jest-search

Node.js CI .github/workflows/release.yml Known Vulnerabilities codecov npm version License: MIT

Jest preset for running tests with local ElasticSearch, OpenSearch and ZincSearch.

Usage

Prerequisite:

ElasticSearch and OpenSearch relies on Java, please make sure you have Java installed and JAVA_HOME is set.

jest-search provide two ways to set up, one is two set up globally, another is running in specific test only

set up globally

1. install library

npm install --save-dev @geek-fun/jest-search

2. create config file jest-search-config.js

all configuration items are optional, but it still requires you to module. exports the function in jest-search-config.js, there aren't any indexes created without passing the indexes configuration,

module.exports = () => {
  return {
    engine: 'elasticsearch', // or 'opensearch' or 'zincsearch'
    version: '8.8.2',
    port: 9200,
    binaryLocation: '', // optional
    clusterName: 'jest-search-local',
    nodeName: 'jest-search-local',
    zincAdmin: 'admin',
    zincPassword: 'Complexpass#123',
    indexes: [
      {
        name: 'index-name',
        body: {
          settings: {
            number_of_shards: '1',
            number_of_replicas: '1'
          },
          aliases: {
            'your-alias': {}
          },
          mappings: {
            dynamic: false,
            properties: {
              id: {
                type: 'keyword'
              }
            }
          }
        }
      }
    ]
  };
};
  • engine: specify startup search engine platform

    allowed value: elasticsearch, opensearch, zincsearch

    default: elasticsearch

  • version: specify startup search engine version

    allowed value: check the versions in each platform's release page

    default: 8.8.2

  • port: specify startup search engine port number

    allowed value: 1024 ~ 2147483647

    default: 9200

  • binaryLocation: use downloaded engine instead default: undefined

  • clusterName: engine's clusterName default: jest-search-local

  • nodeName: engine's nodeName default: jest-search-local

  • indexes: specify the configuration like index name, and mapping of indexes that you want to create during the startup, and indexes will get deleted once test is finished: default: []

  • zincAdmin: zincsearch requires pass env ZINC_FIRST_ADMIN_USER when starting zincsearch, default: admin,

  • zincPassword: : zincsearch requires pass env ZINC_FIRST_ADMIN_PASSWORD when starting zincsearch, default: Complexpass#123

3. create jest-global-setup.js

const { globalSetup } = require('@geek-fun/jest-search');
module.exports = async () => {
  await Promise.all([globalSetup()]);
};

4. create jest-global-teardown.js

const { globalTeardown } = require('@geek-fun/jest-search');
module.exports = async () => {
  await Promise.all([globalTeardown()]);
};

4. modify the jest-config.js

module.exports = {
	...
  globalSetup: '<rootDir>/jest-global-setup.js',
  globalTeardown: '<rootDir>/jest-global-teardown.js',
};

3. play with your tests

// tests/utils/helper.ts sample utils to add item for test
export const saveBook = async (bookDoc: { name: string; author: string }) => {
  await esClient.index({ index, body: bookDoc, refresh: true });
};

// tests/book.test.ts sample test
beforeAll(async () => {
  await saveBook(mockBook);
});

specific test only

the step 1 and 2 are the same as above, jest-search export two methods startEngine and stopEngine to start and stop the search engine, you can manually call them in your test file, the startEngine accepts same argument object as defined in jest-search-config.js file

import { startEngine, stopEngine } from '@geek-fun/jest-search';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import loadConfig from '../jest-search-config.js';


describe('integration test for elasticsearch', () => {
  beforeAll(async () => {
    await startEngine(loadConfig());
    await saveBook(mockBook);
  });
  afterAll(async () => {
    await stopEngine();
  });
  it('should get book when search with valid book name', async () => {
    // ...
  });
});

Known issues

  1. Windows is not on the support list yet, I didn't see the necessity of it yet, feel free to reach out if you have the needs to use it on Windows, then will prioritize it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.