# babel-plugin-arrow-test

.

## Usage no npm install needed!

``````<script type="module">
import babelPluginArrowTest from 'https://cdn.skypack.dev/babel-plugin-arrow-test';
</script>``````

# babel-plugin-arrow-test

Babel 6 plugin, allows you to test anonymous javascript arrow functions easily.

## The problem

I love to write declarative javascript code like

``````blabla
.map(({ x, y }) => ({z: x + y}))
.reduce((memo, { z }) =>  ... )
.filter(({ ... }) => ... )
.groupBy( ... )
``````

Writing full test of this flow can be not an easy task for real flows with many `map`, `reduce` and other functions we like. But almost every function inside flow is easily testable.

But even if any of arrow function in this flow is easily testable, to test each I need to write something like this.

``````// now I can import mappingFunction and test
export const mapFn = ({ x, y }) => ({z: x + y});
export const reduceFn = (memo, { z }) =>  ... ;
...

blabla
.map(mappingFunction)
.reduce(reduceFunction)
...
``````

This can easily be tested, but source is unreadable. Function definition is far outside from place I use it.

So this plugin allows you to solve this problem.

## Solution

``````// file.js
blabla
.map( /* @t(mapFn) */ ({ x, y }) => ({z: x + y}))
.reduce(
// @t(reduceFn)
(memo, { z }) =>  ...
)
...
``````

``````"env": {
"ARROW": {
"plugins": [
["arrow-test", {"regexp": "@t\\(([^\\)]+)\\)"}]
]
}
}
``````

Write test

``````// test anonymous mapFn reduceFn
describe('must import anonymouse functions', () => {
it('must import anonymouse functions', () => {
const { mapFn, reduceFn } = require('./file.js');
expect(mapFn({ x: 1, y: 2 })).toEqual({ z: 3});
});
}

``````

And run with

``````NODE_ENV=ARROW mocha
``````

All anonymous functions with comment you describe as plugin `regexp` parameter will be exported with `regexp` capture so you can test them

## Examples

### map reduce

Simple map reduce module and test

## Install

``````npm install --save-dev babel-plugin-arrow-test
``````

Add to `.babelrc`

``````"env": {
"ARROW": {
"plugins": [
["arrow-test", {"regexp": "@t\\(([^\\)]+)\\)"}]
]
}
}
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.56231064,"math_prob":0.82713765,"size":1936,"snap":"2021-43-2021-49","text_gpt3_token_len":523,"char_repetition_ratio":0.12732919,"word_repetition_ratio":0.13496932,"special_character_ratio":0.32799587,"punctuation_ratio":0.22615804,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96989286,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-08T03:03:53Z\",\"WARC-Record-ID\":\"<urn:uuid:a9463f95-75b6-4cb6-b0a4-5a6136e232b3>\",\"Content-Length\":\"40046\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9c1bdf57-e3a7-412f-bf51-3e9be44373ba>\",\"WARC-Concurrent-To\":\"<urn:uuid:7ed1dd07-536c-4422-aa6d-87c80d21f5e4>\",\"WARC-IP-Address\":\"76.76.21.21\",\"WARC-Target-URI\":\"https://www.skypack.dev/view/babel-plugin-arrow-test\",\"WARC-Payload-Digest\":\"sha1:DOMRHYY4MKPNDFQIUZFHM2MXG3KEV3KB\",\"WARC-Block-Digest\":\"sha1:BRZ2VML4423YW3MS2RAMHZHSC3447WW4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363437.15_warc_CC-MAIN-20211208022710-20211208052710-00216.warc.gz\"}"}