{"id":16527680,"url":"https://github.com/benoitzugmeyer/babel-plugin-test-export","last_synced_at":"2025-06-25T03:08:51.053Z","repository":{"id":57188922,"uuid":"76801700","full_name":"BenoitZugmeyer/babel-plugin-test-export","owner":"BenoitZugmeyer","description":"Export private variables and functions to your tests","archived":false,"fork":false,"pushed_at":"2018-06-16T20:24:40.000Z","size":99,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-02T18:53:38.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BenoitZugmeyer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-18T19:18:16.000Z","updated_at":"2018-06-26T01:49:00.000Z","dependencies_parsed_at":"2022-08-28T12:51:40.300Z","dependency_job_id":null,"html_url":"https://github.com/BenoitZugmeyer/babel-plugin-test-export","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/BenoitZugmeyer/babel-plugin-test-export","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fbabel-plugin-test-export","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fbabel-plugin-test-export/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fbabel-plugin-test-export/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fbabel-plugin-test-export/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BenoitZugmeyer","download_url":"https://codeload.github.com/BenoitZugmeyer/babel-plugin-test-export/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenoitZugmeyer%2Fbabel-plugin-test-export/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261795334,"owners_count":23210620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T17:36:26.670Z","updated_at":"2025-06-25T03:08:51.031Z","avatar_url":"https://github.com/BenoitZugmeyer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# babel-plugin-test-export\n\n[![Build Status](https://travis-ci.org/BenoitZugmeyer/babel-plugin-test-export.svg?branch=master)](https://travis-ci.org/BenoitZugmeyer/babel-plugin-test-export)\n\nExport private variables and functions to your tests. Annotate variable and function declarations\nwith a `// @test-export` comment and they will be exported in a `__test__` property of the module.\nUse values exported like this to write more focused tests or to mock things.\n\n## Install\n\n```\n$ yarn add --dev babel-plugin-test-export\n```\n\nMake sure to add this plugin only for tests. Example, in your `.babelrc`:\n```js\n{\n  // ... your common configuration ...\n  env: {\n    test: {\n      plugins: [ \"test-export\" ]\n    }\n  }\n}\n```\n\n## Example\n\n`mylib.js`:\n```js\n// @test-export\nfunction appendFoo(str) {\n    return str + \"foo\"\n}\n\nmodule.exports = function (str) {\n    return appendFoo(str).toUpperCase()\n}\n```\n\n`mylibSpec.js` (`jasmine` example):\n```js\nconst mylib = require(\"./mylib\")\n\n// Test private functions!\ndescribe(\"appendFoo\", () =\u003e {\n    const { appendFoo } = mylib.__test__\n    it(\"appends foo\", () =\u003e {\n        expect(appendFoo(\"a\")).toBe(\"afoo\")\n    })\n})\n\n// Mock private functions!\ndescribe(\"mylib\", () =\u003e {\n    it(\"appends foo and turn value in uppercase\", () =\u003e {\n        spyOn(mylib.__test__, \"appendFoo\")\n        expect(mylib(\"a\")).toBe(\"AFOO\")\n        expect(mylib.__test__.appendFoo).toHaveBeenCalled()\n    })\n})\n\n```\n\n## How it works\n\nAll references of the annotated variables and functions are rewritten to use the exported reference\ninstead.  The above example is rewritten as:\n\n```js\nconst __test__ = {};\n\n// @test-export\n__test__.appendFoo = function appendFoo(str) {\n    return str + \"foo\";\n};\n\nmodule.exports = function (str) {\n    return (void 0, __test__.appendFoo)(str).toUpperCase();\n};\nmodule.exports.__test__ = __test__\n```\n\n## Drawbacks\n\n* Currently, it only works with commonjs exported modules.\n* Function and variable declarations are not hoisted anymore, so make sure to always write\n  declarations before using them.\n\n## Inspiration and thanks\n\nI used [babel-strip-test-code](https://github.com/madole/babel-strip-test-code) as a start to write\nthis plugin.  Its goal is quite similar: export private values via a `export __test__ = { ... }` and\nit will strip this export for you in production.  This approach, while quite simpler than mine,\nis a bit more verbose and doesn't allow mocks.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitzugmeyer%2Fbabel-plugin-test-export","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenoitzugmeyer%2Fbabel-plugin-test-export","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenoitzugmeyer%2Fbabel-plugin-test-export/lists"}