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

@sanity/test

v0.0.1-alpha.1

Published

Sanity Testing Package

Downloads

16,840

Readme

@sanity/test

Required Env Variables

The tests expects to find the below env variables. Either define it in your shell, or add it to the .env.local file in the repository root.

  • SANITY_E2E_SESSION_TOKEN: Before you get started with writing and running tests, you need to get hold of a token - either using your own Sanity user token (sanity debug --secrets will give you the CLI token provided you are logged in sanity login), or by creating a project API token using https://sanity.io/manage.
  • SANITY_E2E_PROJECT_ID: Project ID of the studio
  • SANITY_E2E_DATASET: Dataset name of the studio

Setup

1. Initialize Playwright

Run Playwright Init Command: Start by initializing Playwright in your project. This sets up the necessary configuration and dependencies. Run the following command in your project directory:

npx playwright init

This command will create a default configuration file and install Playwright as a dependency in your project.

2. Configure Playwright

Paste the provided code into your playwright.config.ts file:

import {defineConfig} from '@playwright/test'
import {createPlaywrightConfig} from '@sanity/test'

const CI = process.env.CI // Assuming CI is set in your environment

const playwrightConfig = createPlaywrightConfig({
  projectId: process.env.SANITY_E2E_PROJECT_ID!,
  token: process.env.SANITY_E2E_SESSION_TOKEN!,
  playwrightOptions(config) {
    return {
      ...config,
      webServer: {
        ...config.webServer,
        command: CI ? 'yarn e2e:start' : 'yarn e2e:dev',
      },
    }
  },
})

export default defineConfig(playwrightConfig)

3. Implement Global Setup

Add a Global Setup File: The global setup file is used for setting up global preconditions for your tests. The @sanity/test package adds default function that authenticates the user to the studio.

import {globalSetup} from '@sanity/test'

export default globalSetup

Note: the default file for global setup is at './test/e2e/globalSetup' path. This can be changed in the playwright.config.ts

5. Write and Run Tests

Start Writing Tests: There are multiple ways to write Playwright tests:

Using Playwright Codegen: Run sanity-test codegen to open an interactive browser session and generate test scripts based on your interactions with the browser. Note: the env variables are required for codegen to work

Using the VSCode Extension: If you prefer, you can use the Playwright extension for Visual Studio Code to write and manage your tests.

Manually Writing Tests: Write test scripts manually in your test files, using the Playwright API.

Run the Tests: Once your tests are written, you can run them using the Playwright CLI. A common command to run tests is:

npx playwright test

Custom Fixtures

interface SanityFixtures {
  /**
   * This provides a Sanity client that can be used to interact with Sanity.
   *
   * @example
   * ```ts
   * function getRemoteValue() {
   *  return sanityClient
   *    .getDocument(`drafts.${documentId}`)
   *    .then((doc) => (doc ? doc.simple : null))
   * }
   *```
   */
  sanityClient: SanityClient
  /**
   * This fixture is used to create a new document in the dataset.
   * It will create a new document with a unique name and return the document.
   * It also navigates to the document given the path
   *
   * @example
   * ```ts
   * const documentId = await createDraftDocument('/test/content/input-ci;textsTest')
   * ```
   */
  createDraftDocument: (navigationPath: string) => Promise<string>
}