{"id":23274320,"url":"https://github.com/luncheon/esbuild-plugin-run-node-test","last_synced_at":"2026-05-14T23:14:29.297Z","repository":{"id":81428712,"uuid":"487011476","full_name":"luncheon/esbuild-plugin-run-node-test","owner":"luncheon","description":"Please write tests in your source file. This esbuild plugin runs the tests and remove them from the bundle.","archived":false,"fork":false,"pushed_at":"2022-05-10T07:36:38.000Z","size":50,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-10T22:16:13.101Z","etag":null,"topics":["esbuild","esbuild-plugin","nodejs","runner","test","testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/esbuild-plugin-run-node-test","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luncheon.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-29T14:56:00.000Z","updated_at":"2022-05-17T04:30:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"e53b909c-4448-49c4-a86b-9683e3effa03","html_url":"https://github.com/luncheon/esbuild-plugin-run-node-test","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/luncheon/esbuild-plugin-run-node-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fesbuild-plugin-run-node-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fesbuild-plugin-run-node-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fesbuild-plugin-run-node-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fesbuild-plugin-run-node-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luncheon","download_url":"https://codeload.github.com/luncheon/esbuild-plugin-run-node-test/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fesbuild-plugin-run-node-test/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278981512,"owners_count":26079640,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["esbuild","esbuild-plugin","nodejs","runner","test","testing"],"created_at":"2024-12-19T20:12:50.805Z","updated_at":"2025-10-08T16:55:59.078Z","avatar_url":"https://github.com/luncheon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# esbuild-plugin-run-node-test\n\nPlease write tests in your source file.  \nThis [`esbuild`](https://esbuild.github.io/) plugin runs the tests and remove them from the bundle.\n\n## Installation\n\n```\nnpm i -D esbuild esbuild-plugin-run-node-test\n```\n\n## Usage Example\n\nThe following example uses [`Preact`](https://preactjs.com/), but this plugin is framework-agnostic.\n\n### Source Code\n\n- src/app.tsx\n\n```tsx\nimport { Fragment, h, render } from \"preact\";\nimport { add } from \"./add\";\nimport { mul } from \"./mul\";\n\nconst App = () =\u003e (\n  \u003c\u003e\n    \u003cp\u003e20 + 22 = {add(20, 22)}\u003c/p\u003e\n    \u003cp\u003e6 × 7 = {mul(6, 7)}\u003c/p\u003e\n  \u003c/\u003e\n);\n\nrender(\u003cApp /\u003e, document.body);\n```\n\n- src/add.ts\n\n```ts\nimport assert from \"node:assert/strict\";\nimport test from \"node:test\";\n\nexport const add = (x: number, y: number) =\u003e x + y;\n\ntest(\"add(1, 2) should be 3\", () =\u003e {\n  assert.equal(add(1, 2), 3);\n});\n```\n\n- src/mul.js\n\n```ts\nimport assert from \"node:assert/strict\";\nimport test from \"node:test\";\n\nexport const mul = (x, y) =\u003e x * y;\n\ntest(\"mul(3, 2) should be 6\", () =\u003e {\n  assert.equal(mul(3, 2), 6);\n});\n```\n\n### Build Script\n\n- build.mjs\n\n```js\nimport esbuild from \"esbuild\";\nimport runNodeTest from \"esbuild-plugin-run-node-test\";\n\nesbuild.build({\n  entryPoints: [\"src/app.tsx\"],\n  outdir: \"dist/\",\n  format: \"esm\",\n  jsxFactory: \"h\",\n  jsxFragment: \"Fragment\",\n  bundle: true,\n  plugins: [runNodeTest()],\n});\n```\n\n### Execution\n\n```js\n$ node build.mjs\n\nok 1 - mul(3, 2) should be 6\n  ---\n  duration_ms: 0.000448975\n  ...\nok 2 - add(1, 2) should be 3\n  ---\n  duration_ms: 0.00015755\n  ...\n1..2\n# tests 2\n# pass 2\n# fail 0\n# skipped 0\n# todo 0\n# duration_ms 0.133946737\n```\n\n```js\n$ cat dist/app.js\n\n// node_modules/preact/dist/preact.module.js\n/* ... */\n\n// src/add.ts\nvar add = (x2, y2) =\u003e x2 + y2;\n\n// src/mul.ts\nvar mul = (x2, y2) =\u003e x2 * y2;\n\n// src/app.tsx\nvar App = () =\u003e /* @__PURE__ */ v(d, null, /* @__PURE__ */ v(\"div\", null, \"20 + 22 = \", add(20, 22)), /* @__PURE__ */ v(\"div\", null, \"6 \\xD7 7 = \", mul(6, 7)));\nS(/* @__PURE__ */ v(App, null), document.body);\n```\n\n## Behavior\n\nWhen bundling source files using `esbuild` with this plugin,\n\n\u003c!-- prettier-ignore --\u003e\n1. `let testSourceCode = \"\"`\n1. each source file to be bundled is parsed using [`swc`](https://swc.rs/)\n    - if a source file includes `import test from \"node:test\"` and the imported `test` is called:\n        - remove all `test(...)`\n        - `` testSourceCode += `import \"${the source file path}\";` ``\n    - remove the following import statements (by default):\n        - `\"node:test\"`\n        - `\"node:assert\"`\n        - `\"node:assert/strict\"`\n1. finally, when the bundling is complete, `testSourceCode` is also bundled using `esbuild` and run\n\n## Options\n\nThe options for this plugin and their default values are as follows:\n\n```js\nrunNodeTest({\n  // narrow down the files to which this plugin should be applied.\n  // https://esbuild.github.io/plugins/#filters\n  filter: /\\.[cm]?[jt]sx?$/,\n\n  // if `false`, just remove import statements and `test(...)`, and tests are not run.\n  run: true,\n\n  // modules to be removed.\n  removeImports: [\n    // \"node:test\" is removed even if not specified.\n    \"node:assert\",\n    \"node:assert/strict\",\n  ],\n\n  // esbuild options used to bundle tests.\n  // https://esbuild.github.io/api/\n  testBuildOptions: {\n    // define, external, loader, target, etc.\n  },\n});\n```\n\n## Limitations\n\n\u003c!-- prettier-ignore --\u003e\n- Currently, the module format of the test bundle is CommonJS, not ES Module. This imposes some limitations (e.g., top-level `await` cannot be used).\n- Currently, tests are not run in [`esbuild serve mode`](https://esbuild.github.io/api/#serve). Test code removal is fine.\n    - Plugins' onEnd callback isn't triggerred in serve mode · Issue #1384 · evanw/esbuild  \n      https://github.com/evanw/esbuild/issues/1384\n- Regardless of scope, function call statements whose names match the default import from `\"node:test\"` are removed.\n    ```js\n    import foo from \"node:test\";\n\n    {\n      const foo = () =\u003e {};\n      foo(); // will be removed\n    }\n    ```\n\n## With `esbuild-plugin-pipe`\n\nIf you use this plugin with [`esbuild-plugin-pipe`](https://github.com/nativew/esbuild-plugin-pipe), pass the same plugin instance to both `esbuild-plugin-pipe` and `esbuild`.\n\n```js\nimport esbuild from \"esbuild\";\nimport pipe from \"esbuild-plugin-pipe\";\nimport runNodeTest from \"esbuild-plugin-run-node-test\";\n\nconst runNodeTestInstance = runNodeTest({ filter: /^$/ });\n\nesbuild.build({\n  entryPoints: [\"src/app.ts\"],\n  outdir: \"dist/\",\n  bundle: true,\n  minify: true,\n  plugins: [\n    pipe({\n      filter: /\\.[cm]?[jt]sx?$/,\n      plugins: [runNodeTestInstance],\n    }),\n    runNodeTestInstance,\n  ],\n});\n```\n\n## License\n\n[WTFPL](http://www.wtfpl.net/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluncheon%2Fesbuild-plugin-run-node-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluncheon%2Fesbuild-plugin-run-node-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluncheon%2Fesbuild-plugin-run-node-test/lists"}