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-fixture

v4.1.0

Published

Helpers to create file based fixtures for use with jest.

Downloads

25

Readme

jest-fixture

Helpers to create file based fixtures for use with jest.

Installation & Usage

npm i jest-fixture --save-dev
import { environment, prepare, file, packageJson } from 'jest-fixture'
import { build } from '../my-build-plugin'

const [fixturePath, setCwd] = environment('build')

test('Build generates required files with modules.', async () => {
  const { dist } = prepare([
    file('index.js', "import 'imported.js'"),
    file('imported.js', "console.log('Hello World!')"),
    packageJson('build', { type: 'module' }),
  ])

  await build()

  const files = listFilesMatching('*.js.map', dist)

  // Source map file is generated.
  expect(files.length).toEqual(1)
  expect(files[0]).toEqual('index.js.map')

  const contents = contentsForFilesMatching('*.js', dist)

  // Contents of imported files included in generated file.
  expect(contents.length).toBeGreaterThan(0)
  expect(contents[0].name).toEqual('index.js')
  expect(contents[0].contents).toContain('Hello World')
})

readFile and writeFile

import { readFile, writeFile } from 'jest-fixture'

const fileContentsAsString = readFile('index.js')
const { name, version } = readFile('package.json')

// With options:
const { presets } = readFile('.babelrc', {
  // Default false, or detected from .json extension.
  json: true,
})

writeFile('index.js', `alert('Knock, knock. Who's there?')`)
writeFile('package.json', {
  name: 'my-app',
  version: '1.0.0',
})

// With options:
writeFile(
  '.babelrc',
  {
    presets: ['@babel/preset-env'],
  },
  {
    // Default false, or detected from .json extension.
    json: true,
    // Default true, will add newline if not present already.
    ensureNewLine: false,
  }
)

listFilesMatching and contentsForFilesMatching

Reads all files matching a glob pattern in a specific folder or process.cwd().

import { join } from 'path'
import { listFilesMatching, contentsForFilesMatching } from 'jest-fixture'

const files = listFilesMatching('*.js')
const sourceMapFilesInDist = listFilesMatching('*.map.js', join(process.cwd(), 'dist'))

// files === ['index.js', 'components.js']

const contents = contentsForFilesMatching('*.css')

// contents === [{ name: styles.css, contents: 'body { display: none; }' }]

wait

Wait for a number of seconds to pass.

import { wait } from 'jest-fixture'

// Waits 5 seconds.
await wait(5)

environment and prepare

Use the environment before a test suite to create a dedicated folder inside test/fixture where process.cwd is set to this folder beforeEach test and the contents are removed afterEach. Prepare will setup the passed files inside the fixturePath so these files can then be used by tests separate from the files of other test suites.

const [fixturePath, setCwd] = environment('suite')

test('Some test.', async () => {
  // fixturePath === 'test/fixture/suite'
  const { dist } = prepare([file('index.js', "console.log('hello')")])
  // dist === 'test/fixture/suite/dist'
  // test/fixture/suite/index.js created
})

file, json, packageJson and interface File

prepare expects a list of File[]s. Such a File object can be manually constructed or through one of the helpers in the second code example for commonly used files.

interface File {
  // Name of the file relative to the `fixturePath`.
  name: string
  // Optionally, write Object contents as JSON.
  json?: true
  // Contents of the file as a string or Object for JSON files.
  contents?: string | Object
  // Alternative to contents, filePath where to copy contents from.
  copy?: string
}
import { file, json, packageJson, prepare } from 'jest-fixture'

const javaScriptFile = file('index.js', "console.log('hello world')")
const jsonFile = json('phone-book.json', [
  { name: 'Bob', number: '123' },
  { name: 'John', number: '456' },
])
const packageJson = packageJson('my-plugin', {
  description: 'Meh',
  version: '1.0.0',
})

prepare([javaScriptFile, jsonFiles, packageJson])

vitest

Use the registerVitest polyfill method to use with vitest.

import { beforeEach, afterEach, vi } from 'vitest'
import { registerVitest } from 'jest-fixture'

registerVitest(beforeEach, afterEach, vi)

Or, use the following test setup code to polyfill jest globals with matching vitest globals.

import { beforeEach, afterEach, vi } from 'vitest'

global.jest = {
  spyOn: vi.spyOn,
}

global.beforeEach = beforeEach
global.afterEach = afterEach