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

playwright-monaco

v1.2.3

Published

Test Monaco editor integrations using Playwright

Downloads

289

Readme

playwright-monaco

github actions npm version npm downloads codecov

Test Monaco editor integrations using Playwright.

Table of Contents

Installation

npm install playwright-monaco

Usage

First, create an entry point that sets up your Monaco editor integration. Your integration should not import Monaco editor. Instead, it should accept the Monaco editor module as an argument. This best practice makes sure your integration also works with other ways to load Monaco editor. For example many users load Monaco editor from a CDN. The ed and monaco variables are assigned globally for convenience, but you can import them for type safety.

// ./tests/setup-monaco.ts
import { ed, monaco } from 'playwright-monaco'

import { configure } from '../src/index.js'

configure(monaco)

In your Playwright config, call createServer with your entry points. This starts a webserver using esbuild serve. This serves a page containing a fully configured Monaco editor. All default web workers are configured and a full page editor instance is created.

// ./playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test'
import { createServer } from 'playwright-monaco'

const config: PlaywrightTestConfig = {
  use: {
    baseURL: await createServer('./tests/setup-monaco')
    // Alternatively, if you need a web worker
    // baseURL: await createServer({
    //   setup: './tests/setup-monaco',
    //   'your-integration': './src/your-integration.worker'
    // })

    // The editor respects dark mode if configured
    //  colorScheme: 'dark'
  }
}

export default config

Now create a test which uses the editor fixture. The editor fixture navigates to the page served, and contains some convenience functions for working with Monaco editor.

// ./tests/integration.spec.ts
import { test } from 'playwright-monaco'

test('your integration', async ({ editor }) => {
  await editor.createModel('console.log("Hello Monaco!")\n', 'file:///index.js', true)
})

API

createServer(entryPoints[, options])

Create an esbuild dev server which serves Monaco editor.

Parameters:

  • entryPoints: A string or object that contains the setup key. These entry points will be built and loaded using esbuild.
  • options: Optional additional configuration options. The following options are accepted:
    • alias: Substitute packages with an alternative.
    • port: The port on which to serve the page.

monaco

The global Monaco editor module. This can only be used in the browser.

ed

The global Monaco editor instance. This can only be used in the browser.

test(name, fn)

A Playwright test instance that has been extended with the editor fixture.

editor fixture

The editor fixture is a Playwright handle attached to the window. It’s configured to allow type-safe access to the ed and monaco globals as well. In addition, the editor fixture has the following helper functions:

createModel(value[, pathOrUri[, open[, language]]])

Create a Monaco editor model.

Parameters
  • value: The value to set. (string)
  • pathOrUri: The path or uri of the model. If a path is given, it will be converted to a file uri. (URL | string)
  • open: If set to true, the created model is opened in the editor. (boolean)
  • language: The model language. By default this is auto detected based on the uri. (string)
Returns

A Playwright handle for the model.

open(patterns[, options])

Open one or more files from the file system in Monaco editor.

Parameters
  • pattern: A glob pattern or array of glob patterns to open in the editor. (string | string[])
  • options: Options to pass to globby. The cwd option is also used to determine the base path of the models. See globby options for details. (object)

setModel(uri)

Open an existing model in the editor.

Parameters
  • uri: The uri of the model to open. (string)

setPosition(position)

Set the position in the editor

Parameters

  • position: THe position to set. (object)

trigger(handlerId[, payload])

Trigger an editor action.

Parameters

  • handlerId: The id of the action to trigger. (string)
  • payload: An additional payload to send with the action. (any)

Returns

The result of the action.

waitForMarkers(uri, fn)

Wait for marker data to be triggered for a resource.

Parameters

  • uri The resource uri to wait for markers for. (string)
  • fn A function to evaluate to trigger marker data. (function)

Returns

Marker data for the uri.

License

MIT © Remco Haszing