Skip to content

Latest commit

Β 

History

History
130 lines (102 loc) Β· 2.98 KB

README.md

File metadata and controls

130 lines (102 loc) Β· 2.98 KB

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