All Projects → satya164 → Babel Test

satya164 / Babel Test

An opinionated library to make testing babel plugins easier.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Test

Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (+707.59%)
Mutual labels:  hacktoberfest, snapshot
Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (-59.49%)
Mutual labels:  babel, jest
Serverless Typescript Starter
🗄🙅‍♀️ Deploy your next serverless JavaScript function in seconds
Stars: ✭ 653 (+726.58%)
Mutual labels:  babel, jest
Snapshot Diff
Diffing snapshot utility for Jest
Stars: ✭ 490 (+520.25%)
Mutual labels:  jest, snapshot
React Boilerplate
Production-ready boilerplate for building universal web apps with React and Redux
Stars: ✭ 53 (-32.91%)
Mutual labels:  babel, jest
Express Babel
Express starter kit with ES2017+ support, testing, linting, and code coverage
Stars: ✭ 621 (+686.08%)
Mutual labels:  babel, jest
Yarn Package Boilerplate
An Yarn package with babel, jest, flow, prettier and more
Stars: ✭ 10 (-87.34%)
Mutual labels:  babel, jest
Ava
Node.js test runner that lets you develop with confidence 🚀
Stars: ✭ 19,458 (+24530.38%)
Mutual labels:  hacktoberfest, babel
Js Toolbox
CLI tool to simplify the development of JavaScript apps/libraries with little to no configuration. (WORK IN PROGRESS/PACKAGE NOT PUBLISHED).
Stars: ✭ 53 (-32.91%)
Mutual labels:  babel, jest
Todoist Js
!! OBSOLETE !! The (un)official Todoist javascript API library
Stars: ✭ 46 (-41.77%)
Mutual labels:  babel, jest
Ts Monorepo
Template for setting up a TypeScript monorepo
Stars: ✭ 459 (+481.01%)
Mutual labels:  babel, jest
Starter React Flux
Generate your React PWA project with TypeScript or JavaScript
Stars: ✭ 65 (-17.72%)
Mutual labels:  babel, jest
Opensource
Delivering delightful digital solutions. Open Source packages with combined ~85M/month downloads, semantically versioned following @conventional-commits. Fully powered by Jest, @Babel TypeScript, @Airbnb @ESLint + @Prettier, @YarnPKG + @Lerna independent versioning, GH @Actions & automated dep updates with @RenovateBot.
Stars: ✭ 459 (+481.01%)
Mutual labels:  babel, jest
Mostly
They mostly come at night; mostly.
Stars: ✭ 78 (-1.27%)
Mutual labels:  babel, jest
Entria Fullstack
Monorepo Playground with GraphQL, React, React Native, Relay Modern, TypeScript and Jest
Stars: ✭ 434 (+449.37%)
Mutual labels:  babel, jest
Preact Jest Snapshot Test Boilerplate
🚀 Test Preact components using Jest snapshots
Stars: ✭ 24 (-69.62%)
Mutual labels:  jest, snapshot
Webpack React Boilerplate
Minimal React 16 and Webpack 4 boilerplate with babel 7, using the new webpack-dev-server, react-hot-loader, CSS-Modules
Stars: ✭ 358 (+353.16%)
Mutual labels:  babel, jest
Nod
Node.js module generator/boilerplate with Babel, Jest, Flow, Documentation and more
Stars: ✭ 355 (+349.37%)
Mutual labels:  babel, jest
React Equalizer
Pure React Match Height Component
Stars: ✭ 32 (-59.49%)
Mutual labels:  babel, jest
Marvelheroes
Marvel Heroes
Stars: ✭ 54 (-31.65%)
Mutual labels:  babel, jest

babel-test

Build Status Code Coverage MIT License Version

An opinionated library to make testing babel plugins easier.

Demo

Why

I like to use fixture files instead of snapshots for testing my babel plugins because it's easier to read with proper syntax highlighting. But I missed the features of snapshots, i.e. creating and updating snapshots with press of a key. It was too annoying to copy paste transformed code from the terminal every time. So I wanted to make something which integrates with Jest's snapshot feature.

I also felt that the current solutions add too much abstraction and I wanted to make something simpler. I built this instead of contributing to existing tools because it's not addressing lack of features in existing tools, but exploring a different API.

The tool works with Jest (recommended) or any testing framework which provides the describe and it global functions, such as Mocha.

Installation

npm install --save-dev babel-test

or

yarn add --dev babel-test

Usage

The library exports a create function, which you can use to create helpers for your test using a babel config. The fixtures function from the returned object can be used to run tests against a directory containing fixtures:

import path from 'path';
import { create } from 'babel-test';

const { fixtures } = create({
  plugins: [require.resolve('./my-plugin')],
});

fixtures('my plugin', path.join(__dirname, '__fixtures__'));

API

Calling the create function with a babel config returns an object with test and fixtures functions:

import { create } from 'babel-test';

const { test, fixtures } = create({
  plugins: [require.resolve('./my-plugin')],
});

The create function accepts the same options object as Babel. It'll additionally set babelrc and configFile options to false by default if you haven't explicitly passed them. This avoids your tests being affected by external babel configuration.

Testing fixtures

To run the tests against a directory with fixtures, you can use the fixtures function returned from create:

fixtures('my plugin', path.join(__dirname, '__fixtures__'));

It accepts a title for the describe block and the absolute path to the directory containing the fixtures.

The fixtures directory should contain subdirectories with test files. Every test should contain a code.js file, which will be used as the input. If the transform should throw, it should have an error.js file. If the transform should pass, it should have an output.js file with the transformed code. The title for each test will be based the name of the directory. The first argument is used as the title for the describe block.

.
├── function-expression
│   ├── code.js
│   └── output.js
├── invalid-syntax
│   ├── code.js
│   └── error.js
└── simple-variable
    ├── code.js
    └── output.js

You can use fixtures.skip and fixtures.only, similar to Jest's describe.skip and describe.only. To skip an individual fixture, you can rename the fixture's directory to skip.name-of-the-fixture, and to run a specific fixture only, you can rename the fixture's directory to only.name-of-the-fixture.

To use hooks such as beforeEach, afterEach, beforeAll and afterAll, you can pass an object with these properties as the third argument:

fixtures('my plugin', path.join(__dirname, '__fixtures__'), {
  beforeEach() {
    // Do some setup here
  },
});

By default, it will compare the outputs with the files on the filesystem and you have to manually update the files in case of a mismatch. If you're using Jest, you can use the snapshot feature to automatically update the files with a keypress. (See below) on how to set it up.

Standalone test

To run a standalone test with some custom logic, you can use the test function returned from create:

test('transpiles const to var', ({ transform }) => {
  const { code } = await transform('const foo = 42', { filename: 'foo.js' });

  expect(code).toBe('var foo = 42');
});

It accepts a title for the test and a callback containing the test. The callback will receive a transform function, and should return a promise if it's asynchronous. The transform function takes an optional second parameter containing additional options for Babel, useful for passing additional information such as filename required by some plugins.

You can use test.skip and test.only, similar to Jest's it.skip and it.only.

Custom transform

Sometimes it's useful to test a plugin against a different babel instance. You can pass a transform function to create instead of a config object to test against a custom babel instance:

const { test, fixtures } = create((code, options) =>
  // transform function for babel 6
  require('babel-core').transform(
    code,
    Object.assign(
      {
        babelrc: false,
        plugins: [require.resolve('./my-plugin')],
      },
      options
    )
  )
);

The custom transform function will receive the code and additional options for babel (such as filename) and should return an object with code property containing the transformed code.

If you're using the same fixtures directory with a different transform function, keep in mind that the same plugins can produce slightly different code in a different Babel version, and it's likely that error stack traces will be different. In these cases, there will be conflicts when updating the snapshots. It'll be better to separately test those fixtures for the different transforms to avoid conflicts.

Integration with Jest snapshot

Integrating with Jest snapshots allows you to automatically create and update output files. To integrate with Jest snapshots, you need to:

Install and add the jest-file-snapshot matcher:

import { toMatchFile } from 'jest-file-snapshot';

expect.extend({ toMatchFile });

Then configure the Jest watcher to ignore output files by adding the following under the jest key in package.json:

"watchPathIgnorePatterns": [
  "__fixtures__\\/[^/]+\\/(output|error)\\.js"
]

Now you can create and update the output files like you would do with snapshots.

Alternatives

Contributing

Make sure your code passes the unit tests, ESLint and TypeScript. Run the following to verify:

yarn test
yarn lint
yarn typescript

To fix formatting errors, run the following:

yarn lint -- --fix
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].