https://github.com/dgellow/plugin-test-internal
🕵️♀️ JS/TS compilers plugin to safely export internal functions and variables, for testing or meta programming purpose
https://github.com/dgellow/plugin-test-internal
babel-plugin babel7 testing typescript typescript-transformer
Last synced: 5 months ago
JSON representation
🕵️♀️ JS/TS compilers plugin to safely export internal functions and variables, for testing or meta programming purpose
- Host: GitHub
- URL: https://github.com/dgellow/plugin-test-internal
- Owner: dgellow
- Created: 2020-04-05T18:37:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-18T12:13:25.000Z (about 2 years ago)
- Last Synced: 2024-09-19T04:09:12.331Z (over 1 year ago)
- Topics: babel-plugin, babel7, testing, typescript, typescript-transformer
- Language: TypeScript
- Homepage:
- Size: 793 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Test the internals of a JS or TS module [](https://travis-ci.org/dgellow/plugin-test-internal)
Available as
- a babel plugin [](https://badge.fury.io/js/babel-plugin-test-internal)
- a typescript compiler extension [](https://badge.fury.io/js/typescript-plugin-test-internal)
## Usage
### Babel plugin
1. Install package
```sh
$ npm install -D babel-plugin-test-internal
$ yarn add -D babel-plugin-test-internal
```
2. Add the plugin to Babel configuration in your `babel.config.js`
```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
}
}
```
3. In your code, where you want to use internal content (i.e: your tests)
```js
// 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
```
2. Add the plugin to [ts-loader](https://github.com/TypeStrong/ts-loader/) configuration in your `webpack.config.js`
```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)]
})
}
}
]
}
}
```
3. In your code, where you want to use internal content (i.e: your tests)
```ts
// 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
```sh
# from the project root
$ yarn install
$ yarn build
$ yarn test
```
### Babel plugin
```sh
$ cd ./babel
$ yarn install
$ yarn test
$ yarn build
```
### Typescript plugin
```sh
$ cd ./typescript
$ yarn install
$ yarn test
$ yarn build
```