Code Monkey home page Code Monkey logo

plugin-test-internal's Introduction

Test the internals of a JS or TS module Build Status

Available as

  • a babel plugin npm version
  • a typescript compiler extension npm version

Usage

Babel plugin

  1. Install package
$ npm install -D babel-plugin-test-internal
$ yarn add -D babel-plugin-test-internal
  1. Add the plugin to Babel configuration in your babel.config.js
// configuration example

module.exports = api => {
  //  1. ๐Ÿ‘‡ are we in a test context?
  const isTest = api.env('test')
  // 2. if yes register the plugin ๐Ÿ‘‡
  const plugins = isTest ? ["test-internal"]: []

  return {
  presets: [
  "@babel/preset-env",
  "@babel/preset-typescript",
  "@babel/preset-react",
  ],
  plugins, // ๐Ÿ‘ˆ  3. don't forget to add the plugins property to the configuration object
  }
}
  1. In your code, where you want to use internal content (i.e: your tests)
// 1. ๐Ÿ‘‡ import __internal__ from the module you want to test
import {__internal__} from "./your-file"

test("testing some internal things", () => {
  const expected = ...
  // 2. use properties you need ๐Ÿ‘‡
  assert(__internal__.doSomething(), expected)
})

Typescript compiler plugin

  1. Install package
$ npm install -D typescript-plugin-test-internal
$ yarn add -D typescript-plugin-test-internal
  1. Add the plugin to ts-loader configuration in your webpack.config.js
// configuration example

// ๐Ÿ‘‡ 1. import the plugin
const testInternalTranformer = require('typescript-plugin-test-internal').default;

module.exports = {
  mode: 'development',
  entry: './index.ts',
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        options: { // ๐Ÿ‘ˆ 2. add ts-loader 'options' object
          // 3. ๐Ÿ‘‡ add the method 'getCustomTransformers'
          getCustomTransformers: program => ({
            // 4. register the plugin ๐Ÿ‘‡
            before: [testInternalTransformer(program)]
          })
        }
      }
    ]
  }
}
  1. In your code, where you want to use internal content (i.e: your tests)
// 1. ๐Ÿ‘‡ import '__internal__' from the module you want to test
import {__internal__} from "./your-file"

test("testing some internal things", () => {
  const expected = ...
  // 2. use properties you need ๐Ÿ‘‡
  assert(__internal__.doSomething(), expected)
})

Development

Install all dependencies, build and test everything

# from the project root
$ yarn install
$ yarn build
$ yarn test

Babel plugin

$ cd ./babel
$ yarn install
$ yarn test
$ yarn build

Typescript plugin

$ cd ./typescript
$ yarn install
$ yarn test
$ yarn build

plugin-test-internal's People

Contributors

dependabot[bot] avatar dgellow 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.