Giter VIP home page Giter VIP logo

jest-fixture's Introduction

jest-fixture

jest-fixture Logo

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

jest-fixture's People

Contributors

tobua avatar

Stargazers

 avatar

Watchers

 avatar

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.