npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@geek-fun/jest-search

v1.1.0

Published

Jest preset for running tests with local search platform

Downloads

235

Readme

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