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

jest-environment-hops

v15.2.1

Published

Opinionated Jest environment to run Puppeteer-powered tests against Hops apps

Downloads

73

Readme

jest-environment-hops

npm

Please see the main Hops Readme for general information and a Getting Started Guide.

This package is part of Hops's internal tooling. Hence, it doesn't provide an API contract. Breaking changes may be introduced even with patch releases. Use at your own risk.

Installation

npm install --save-dev jest-environment-hops

Usage

Add jest-environment-hops as testEnvironment to your Jest config. This can for example be done by adding it to your package.json.

{
  "jest": {
    "testEnvironment": "jest-environment-hops"
  }
}

jest-environment-hops launches a Puppeteer instance during the setup-phase by default. Check the API part of this document, to learn how to interact with the instance.

To disable Puppeteer for a test, add the following code comment to the top of the test file:

/**
 * @jest-hops-puppeteer off
 */

API

The API & certain values are exposed globally in the Jest environment, so there's no need to import anything.

cwd: string

jest-environment-hops creates a temporary folder, in which the test runs. cwd holds the absolute path of that folder.

browser: Puppeteer.Browser | undefined

The Puppeteer instance, jest-environment-hops launches, is exposed via the global browser-variable.

In case Puppetteer is disabled via the code pragma @jest-hops-puppeteer off, the browser-variable's value is undefined.

HopsCLI.build: (env?: NodeJS.ProcessEnv, ...cliArgs: string[]): Promise<{ stdout: string, stderr: string, error?: Error }>

Builds the local Hops app into a temporary folder.

const { stderr, error } = await HopsCLI.build('-p');

expect(stderr).toBe('');
expect(error).not.toBeDefined();

HopsCLI.start: (env?: NodeJS.ProcessEnv, ...cliArgs: string[]): { getUrl: () => Promise<string>, stopServer: () => Promise<void> }

Starts the local Hops app, so it's possible to visit it with Puppeteer.

const { getUrl, stopServer } = HopsCLI.start(
  { NODE_ENV: 'testing' },
  '--fast-dev'
);
const url = await getUrl();

// visit URL...

// Stop server explicitly
await stopServer();

stopServer is exposed for special needs. jest-environment-hops takes care of stopping the Hops server, when all tests are done.

createPage(): () => Promise<object>

Convenience function for creating a new Puppeteer.Page-instance, which resolves to an object, that holds the page-instance and three helper functions.

const { page, getProperty, getInnerText, getElementByText } =
  await createPage();
await page.goto('http://localhost:8080/');

const headingClass = await getProperty('className', '.heading');
const headingText = await getInnerText('.heading');
const button = await getElementByText('Submit');

getProperty: (property: string, selector: string): Promise<string | null>

Returned by createPage. Take a look at createPage() to see a usage example.

getInnerText: (selector: string): Promise<string | null>

Returned by createPage. Take a look at createPage() to see a usage example.

getElementByText: (text: string): Promise<Puppeteer.ElementHandle>

Returned by createPage. Take a look at createPage() to see a usage example.

Throws an error, when no element has been found.