Giter Site home page Giter Site logo

shelfio / jest-elasticsearch Goto Github PK

View Code? Open in Web Editor NEW
9.0 24.0 3.0 126 KB

Jest preset for running tests with local ElasticSearch

License: MIT License

JavaScript 10.25% Shell 13.15% TypeScript 76.60%
elasticsearch jest jest-preset npm-package

jest-elasticsearch's Introduction

jest-elasticsearch

Jest preset for running tests with local ElasticSearch

How to mock Elasticsearch with Jest?

Usage

0. Install

$ yarn add @shelf/jest-elasticsearch --dev

1. Create jest.config.js

module.exports = {
  preset: '@shelf/jest-elasticsearch'
};

If you have a custom jest.config.js make sure you remove testEnvironment property, otherwise it will conflict with the preset.

2. Create jest-es-config.js

module.exports = () => {
  return {
    esVersion: '8.4.0', // ! must be exact version. Ref: https://github.com/elastic/elasticsearch-js .
    // don't be shy to fork our code and update deps to correct.
    clusterName: 'your-cluster-name',
    nodeName: 'your-node-name',
    port: 9200,
    indexes: [
      {
        name: 'your-index-name',
        body: {
          settings: {
            number_of_shards: '1',
            number_of_replicas: '1'
          },
          aliases: {
            'your-alias': {}
          },
          mappings: {
            dynamic: false,
            properties: {
              //here you should paste your mapping
              //Example:
              id: {
                type: 'keyword'
              }
            }
          }
        }
      }
    ]
  };
};

4. PROFIT! Write tests

it();

Monorepo Support

By default the jest-es-config.js is read from cwd directory, but this might not be suitable for monorepos with nested jest projects with nested jest.config.* files nested in subdirectories.

If your jest-es-config.js file is not located at {cwd}/jest-es-config.js or you are using nested jest projects, you can define the environment variable JEST_ELASTICSEARCH_CONFIG with the absolute path of the respective jest-es-config.js file.

Example Using JEST_ELASTICSEARCH_CONFIG in nested project

// src/nested/project/jest.config.js
const path = require('path');

// Define path of project level config - extension not required as file will be imported
// via `require(process.env.JEST_ELASTICSEARCH_CONFIG)`
process.env.JEST_ELASTICSEARCH_CONFIG = path.resolve(__dirname, './jest-es-config');

module.exports = {
  preset: '@shelf/jest-elasticsearch'
  displayName: 'nested-project',
};

Troubleshooting

Issue running tests locally - Exception in thread "main"
Exception in thread "main'
java.lang.UnsupportedOperationException The Security Manager is deprecated and will be removed in a future release
at java.base/java.lang.System.setSecurityManager(System. java: 416)
at ora.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.iava:71

The main reason why this issue appears is that you have an incompatible java version installed to run elastic locally.

What to do?

  1. List current java versions
$ /usr/libexec/java_home -V
  1. If you see version 18.0.x Add this command to your bashrc, zshrc, etc
$ /usr/libexec/java_home -v 18
  1. If you see no versions or do not have a compatible version installed - Install version 18 https://www.oracle.com/java/technologies/downloads/#java18

  2. Reload the console and check the java version with

$ java -version

Output for proper work

$ java -version
java version "18.0.2.1"
Java(TM) SE Runtime Environment (build 18.0.2.1+1-1)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2.1+1-1, mixed mode, sharing)
  1. Go to step 2 and set version 18.xx as a default for the shell

Note: If you need to run elastic <= v7.17.x locally, then perform the steps above but for the java version 1.8.xxx

See Also

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

Create and publish a GitHub release with your tag

  1. Go to repository
  2. Select Releases
  3. Select Draft a new release
  4. Choose a tag, fill title and describe changes
  5. Press a Publish release

License

MIT ยฉ Shelf

jest-elasticsearch's People

Contributors

batovpasha avatar gregholland avatar harazdovskiy avatar knupman avatar randomhash avatar renovate-bot avatar renovate[bot] avatar siloss avatar slavik-chapelskyi avatar terret avatar vladgolubev avatar vladholubiev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jest-elasticsearch's Issues

be able to customize es version

instead of forking, why not enable customization esVersion

is there any block of this?

We are willing to contribute with the fix

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency prettier to v2.8.3
  • chore(deps): update dependency eslint to v8.33.0
  • chore(deps): update dependency lint-staged to v13
  • chore(deps): update jest monorepo to v29 (major) (@types/jest, jest)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • node 5.0.2
npm
package.json
  • @shelf/elasticsearch-local 3.2.0
  • cwd 0.10.0
  • @babel/cli 7.19.3
  • @babel/core 7.20.2
  • @elastic/elasticsearch 8.4.0
  • @shelf/babel-config 1.2.0
  • @shelf/eslint-config 0.19.0
  • @shelf/prettier-config 0.0.7
  • @shelf/tsconfig 0.0.8
  • @types/cwd ^0.10.0
  • @types/jest 28.1.8
  • eslint 8.28.0
  • husky 7.0.4
  • jest 28.1.3
  • lint-staged 12.1.2
  • prettier 2.8.0
  • typescript 4.8.4
  • node >=16

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>shelfio/renovate-config)

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.