{"id":15714548,"url":"https://github.com/drsensor/rs-jest","last_synced_at":"2025-04-30T15:23:24.484Z","repository":{"id":32927224,"uuid":"146596897","full_name":"DrSensor/rs-jest","owner":"DrSensor","description":"Jest preprocessor/transformer for Rust","archived":false,"fork":false,"pushed_at":"2022-12-07T23:53:16.000Z","size":665,"stargazers_count":17,"open_issues_count":27,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T03:03:31.683Z","etag":null,"topics":["jest","jest-transform","my-experiment","rust","webassembly"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DrSensor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-29T12:31:22.000Z","updated_at":"2024-09-17T09:53:15.000Z","dependencies_parsed_at":"2023-01-14T22:42:38.663Z","dependency_job_id":null,"html_url":"https://github.com/DrSensor/rs-jest","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frs-jest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frs-jest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frs-jest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frs-jest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrSensor","download_url":"https://codeload.github.com/DrSensor/rs-jest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222666838,"owners_count":17019883,"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":["jest","jest-transform","my-experiment","rust","webassembly"],"created_at":"2024-10-03T21:38:21.132Z","updated_at":"2024-11-02T03:03:36.334Z","avatar_url":"https://github.com/DrSensor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/rust-lang/rust\"\u003e\n    \u003cimg height=\"200\" alt=\"binaryen logo\" src=\"https://www.rust-lang.org/logos/rust-logo-blk.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/rollup/rollup\"\u003e\n    \u003cimg height=\"200\" alt=\"webpack logo\" src=\"https://jestjs.io/img/jest.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![size][size]][size-url]\n[![npm][npm-download]][npm-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n\n\u003c!-- [![cover][cover]][cover-url] --\u003e\n\n# rs-jest\n\n\u003csup\u003e\u003csup\u003etl;dr -- see [examples](#examples)\u003c/sup\u003e\u003c/sup\u003e\n\nThis is a jest transformer that loads Rust code so it can be interop with Javascript base project. Currently, the Rust code will be compiled as:\n\n- [x] WebAssembly module/instance\n- [ ] Node.js addon/ffi\n\n## Requirements\n\n\u003cul\u003e\n\u003cli\u003eNode v8 or later\u003c/li\u003e\n\u003cli\u003eJest v23 or later\u003c/li\u003e\n\u003cli\u003e\u003cdetails\u003e\n\u003csummary\u003eRust v1.28.0 with wasm32-uknown-unknown installed\u003c/summary\u003e\n\n```console\nrustup default 1.28.0\nrustup target add wasm32-unknown-unknown\n```\n\n\u003c/details\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\n## Getting Started\n\nTo begin, you'll need to install `rs-jest`:\n\n```console\nnpm install rs-jest --save-dev\n```\n\nThen configure Jest to make `rs-jest` to transform the Rust (`*.rs`) file. For example:\n\n\u003cb\u003ejest.config.js\u003c/b\u003e\n\n```js\nmodule.exports = {\n  transform: {\n    \"^.+\\\\.rs$\": \"rs-jest\"\n  }\n};\n```\n\nor if you prefer to put the config in\n\u003cb\u003epackage.json\u003c/b\u003e\n\n```json\n  \"jest\": {\n    \"transform\": {\n      \"^.+\\\\.rs$\": \"rs-jest\"\n    }\n  }\n```\n\n\u003cdetails\u003e\n\u003csummary\u003equick usage\u003c/summary\u003e\n\n\u003cb\u003elib.rs\u003c/b\u003e\n\n```rust\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    a + b\n}\n```\n\n\u003cb\u003eindex.js\u003c/b\u003e\n\n```js\nimport wasm from \"lib.rs\";\n\nexport async function increment(a) {\n  const { instance } = await wasm;\n  return instance.exports.add(1, a);\n}\n```\n\n\u003c/details\u003e\n\nAnd run `jest` via your preferred method.\n\n## Options\n\nPretty much like [ts-jest][], you can configure `rs-jest` by using global variables under the `\"rs-jest\"` key:\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003eexport\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\nHow wasm code would be exported. This options is identical with [option `export` in webassembly-loader][webassembly-loader]. (see [examples](#examples))\n\n```json\n{\n  \"globals\": {\n    \"rs-jest\": {\n      \"export\": \"instance\"\n    }\n  },\n  \"transform\": {\n    \"^.+\\\\.rs$\": \"rs-jest\"\n  }\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003etarget\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `String`\n- Default: `wasm32-unknown-unknown`\n- Expected value: see [supported platform](https://forge.rust-lang.org/platform-support.html)\n\nThe Rust target to use. Currently it **only support [wasm related target](https://kripken.github.io/blog/binaryen/2018/04/18/rust-emscripten.html)**\n\n```json\n{\n  \"globals\": {\n    \"rs-jest\": {\n      \"target\": \"wasm32-unknown-emscripten\"\n    }\n  },\n  \"transform\": {\n    \"^.+\\\\.rs$\": \"rs-jest\"\n  }\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003erelease\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `Boolean`\n- Default: `true`\n\nWhether to compile the Rust code in debug or release mode.\n\n```json\n{\n  \"globals\": {\n    \"rs-jest\": {\n      \"release\": false\n    }\n  },\n  \"transform\": {\n    \"^.+\\\\.rs$\": \"rs-jest\"\n  }\n}\n```\n\n\u003c/details\u003e\n\n## Examples\n\nSee the test cases and example projects in [fixtures](./test/fixtures) and [examples](./examples/) for more insight.\n\n\u003cdetails\u003e\n\u003csummary\u003eThe exported module are pretty much like \u003ca href=\"https://github.com/DrSensor/rollup-plugin-rust\"\u003erollup-plugin-rust\u003c/a\u003e so it can be used alongside with it\u003c/summary\u003e\n\n### Given this Rust code\n\n\u003cb\u003elib.rs\u003c/b\u003e\n\n```rust\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    a + b\n}\n```\n\n\u003cb\u003eCargo.toml\u003c/b\u003e\n\n```toml\n[package]\nname = \"adder\"\nversion = \"0.1.0\"\nauthors = [\"Full Name \u003cemail@site.domain\u003e\"]\n\n[lib]\ncrate-type = [\"cdylib\"]\npath = \"lib.rs\"\n```\n\n### With options\n\n#### `{export: 'buffer'}`\n\n```js\nimport wasmCode from \"./lib.rs\";\n\nWebAssembly.compile(wasmCode).then(module =\u003e {\n  const instance = new WebAssembly.Instance(module);\n  console(instance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n#### `{export: 'module'}`\n\n```js\nimport wasmModule from \"./lib.rs\";\n\nconst instance = new WebAssembly.Instance(wasmModule);\nconsole(instance.exports.add(1, 2)); // 3\n```\n\n---\n\n#### `{export: 'instance'}`\n\n```js\nimport wasm from \"./lib.rs\";\n\nconsole(wasm.exports.add(1, 2)); // 3\n```\n\n---\n\n#### `{export: 'async'}`\n\n```rust\nextern {\n    fn hook(c: i32);\n}\n\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    hook(a + b)\n}\n```\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmInstantiate(importObject | undefined).then(({ instance, module }) =\u003e {\n  console(instance.exports.add(1, 2)); // 3\n\n  // create different instance, extra will be called in different environment\n  const differentInstance = new WebAssembly.Instance(module, {\n    env: {\n      hook: result =\u003e result * 2\n    }\n  });\n  console(differentInstance.exports.add(1, 2)); // 6\n});\n```\n\n---\n\n#### `{export: 'async-instance'}`\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmInstantiate(importObject | undefined).then(instance =\u003e {\n  console(instance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n#### `{export: 'async-module'}`\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmCompile(importObject | undefined).then(module =\u003e {\n  const differentInstance = new WebAssembly.Instance(module);\n  console(differentInstance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n\u003c/details\u003e\n\n## You may also want to look at\n\n- [rollup-plugin-rust](https://github.com/DrSensor/rollup-plugin-rust)\n- [webassembly-loader](https://github.com/DrSensor/webassembly-loader) + [rust-native-wasm-loader](https://github.com/dflemstr/rust-native-wasm-loader)\n\n## Who use this?\n\n- [example-stencil-rust](https://github.com/DrSensor/example-stencil-rust)\n- [add yours 😉]\n\n## Contributing\n\n- [CONTRIBUTING.md](./.github/CONTRIBUTING.md) for how you can make contribution\n- [HACKING.md](./.github/HACKING.md) for technical details\n\n## Credits\n\n- [typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter)\n- [webpack-defaults](https://github.com/webpack-contrib/webpack-defaults)\n\n---\n\n## License\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FDrSensor%2Frs-jest.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FDrSensor%2Frs-jest?ref=badge_large)\n\n[ts-jest]: https://github.com/kulshekhar/ts-jest/\n[webassembly-loader]: https://github.com/DrSensor/webassembly-loader#export\n[npm]: https://img.shields.io/npm/v/rs-jest.svg\n[npm-url]: https://npmjs.com/package/rs-jest\n[npm-download]: https://img.shields.io/npm/dm/rs-jest.svg\n[deps]: https://david-dm.org/DrSensor/rs-jest.svg\n[deps-url]: https://david-dm.org/DrSensor/rs-jest\n[tests]: https://img.shields.io/circleci/project/github/DrSensor/rs-jest.svg\n[tests-url]: https://circleci.com/gh/DrSensor/rs-jest\n[cover]: https://codecov.io/gh/DrSensor/rs-jest/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/DrSensor/rs-jest\n[size]: https://packagephobia.now.sh/badge?p=rs-jest\n[size-url]: https://packagephobia.now.sh/result?p=rs-jest\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrsensor%2Frs-jest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrsensor%2Frs-jest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrsensor%2Frs-jest/lists"}