{"id":13603076,"url":"https://github.com/agnivade/wasmbrowsertest","last_synced_at":"2025-05-16T06:08:02.765Z","repository":{"id":36005615,"uuid":"140969738","full_name":"agnivade/wasmbrowsertest","owner":"agnivade","description":"Run WASM tests inside your browser","archived":false,"fork":false,"pushed_at":"2025-03-23T15:05:44.000Z","size":1080,"stargazers_count":196,"open_issues_count":5,"forks_count":24,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-08T16:06:43.924Z","etag":null,"topics":["browser","chromedp","exec","golang","runtime","testing","wasm"],"latest_commit_sha":null,"homepage":"","language":"Go","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/agnivade.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":"2018-07-14T18:42:24.000Z","updated_at":"2025-03-23T15:05:04.000Z","dependencies_parsed_at":"2025-04-01T15:07:57.498Z","dependency_job_id":"984cd500-b3d9-4e9f-a8b7-af7100dad0eb","html_url":"https://github.com/agnivade/wasmbrowsertest","commit_stats":{"total_commits":74,"total_committers":13,"mean_commits":"5.6923076923076925","dds":0.2432432432432432,"last_synced_commit":"6c05f7170988ac62b921a5a6a4951570e0fa1b01"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agnivade%2Fwasmbrowsertest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agnivade%2Fwasmbrowsertest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agnivade%2Fwasmbrowsertest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agnivade%2Fwasmbrowsertest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agnivade","download_url":"https://codeload.github.com/agnivade/wasmbrowsertest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254478193,"owners_count":22077676,"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":["browser","chromedp","exec","golang","runtime","testing","wasm"],"created_at":"2024-08-01T18:01:48.302Z","updated_at":"2025-05-16T06:07:57.734Z","avatar_url":"https://github.com/agnivade.png","language":"Go","funding_links":[],"categories":["WebAssembly","browser","Libraries for creating HTTP middlewares","Go","路由"],"sub_categories":["Routers","路由器","Utility/Miscellaneous","创建http中间件的代码库"],"readme":"# wasmbrowsertest [![Build Status](https://github.com/agnivade/wasmbrowsertest/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/agnivade/wasmbrowsertest/actions/workflows/ci.yml)\n\nRun Go wasm tests easily in your browser.\n\nIf you have a codebase targeting the wasm platform, chances are you would want to test your code in a browser. Currently, that process is a bit cumbersome:\n- The test needs to be compiled to a wasm file.\n- Then loaded into an HTML file along with the wasm_exec.js.\n- And finally, this needs to be served with a static file server and then loaded in the browser.\n\nThis tool automates all of that. So you just have to type `GOOS=js GOARCH=wasm go test`, and it automatically executes the tests inside a browser !\n\n## Quickstart\n\n- `go install github.com/agnivade/wasmbrowsertest@latest`. This will place the binary in $GOPATH/bin, or $GOBIN, if that has a different value.\n- Rename the binary to `go_js_wasm_exec`.\n- Add $GOBIN to $PATH if it is not already done.\n- Run tests as usual: `GOOS=js GOARCH=wasm go test`.\n- You can also take a cpu profile. Set the `-cpuprofile` flag for that.\n\n## Ok, but how does the magic work ?\n\n`go test` allows invocation of a different binary to run a test. `go help test` has a line: \n\n```\n-exec xprog\n\t    Run the test binary using xprog. The behavior is the same as\n\t    in 'go run'. See 'go help run' for details.\n```\n\nAnd `go help run` says:\n\n```\nBy default, 'go run' runs the compiled binary directly: 'a.out arguments...'.\nIf the -exec flag is given, 'go run' invokes the binary using xprog:\n\t'xprog a.out arguments...'.\nIf the -exec flag is not given, GOOS or GOARCH is different from the system\ndefault, and a program named go_$GOOS_$GOARCH_exec can be found\non the current search path, 'go run' invokes the binary using that program,\nfor example 'go_nacl_386_exec a.out arguments...'. This allows execution of\ncross-compiled programs when a simulator or other execution method is\navailable.\n```\n\nSo essentially, there are 2 ways:\n- Either have a binary with the name of `go_js_wasm_exec` in your $PATH.\n- Or set the `-exec` flag in your tests.\n\nUse whatever works for you.\n\n### How is a CPU profile taken ?\n\nA CPU profile is run during the duration of the test, and then converted to the pprof format so that it can be natively analyzed with the Go toolchain.\n\n### Can I run something which is not a test ?\n\nYep. `GOOS=js GOARCH=wasm go run main.go` also works. If you want to actually see the application running in the browser, set the `WASM_HEADLESS` variable to `off` like so `WASM_HEADLESS=off GOOS=js GOARCH=wasm go run main.go`.\n\n### Can I use this inside Travis ?\n\nSure.\n\nAdd these lines to your `.travis.yml`\n\n```\naddons:\n  chrome: stable\n\ninstall:\n- go install github.com/agnivade/wasmbrowsertest@latest\n- mv $GOPATH/bin/wasmbrowsertest $GOPATH/bin/go_js_wasm_exec\n- export PATH=$GOPATH/bin:$PATH\n```\n\nNow, just setting `GOOS=js GOARCH=wasm` will run your tests using `wasmbrowsertest`. For other CI environments, you have to do something similar.\n\n### Can I use this inside Github Action?\n\nSure.\n\nAdd these lines to your `.github/workflows/ci.yml`\n\nPS: adjust the go version you need in go-version section\n\n```\non: [push, pull_request]\nname: Unit Test\njobs:\n  test:\n    strategy:\n      matrix:\n        go-version: [1.xx.x]\n        os: [ubuntu-latest]\n    runs-on: ${{ matrix.os }}\n    steps:\n    - name: Install Go\n      uses: actions/setup-go@v2\n      with:\n        go-version: ${{ matrix.go-version }}\n    - name: Install chrome\n      uses: browser-actions/setup-chrome@latest\n    - name: Install dep\n      run: go install github.com/agnivade/wasmbrowsertest@latest\n    - name: Setup wasmexec\n      run: mv $(go env GOPATH)/bin/wasmbrowsertest $(go env GOPATH)/bin/go_js_wasm_exec\n    - name: Checkout code\n      uses: actions/checkout@v2\n```\n\n### What sorts of browsers are supported ?\n\nThis tool uses the [ChromeDP](https://chromedevtools.github.io/devtools-protocol/) protocol to run the tests inside a Chrome browser. So Chrome or any blink-based browser will work.\n\n### Why not firefox ?\n\nGreat question. The initial idea was to use a Selenium API and drive any browser to run the tests. But unfortunately, geckodriver does not support the ability to capture console logs - https://github.com/mozilla/geckodriver/issues/284. Hence, the shift to use the ChromeDP protocol circumvents the need to have any external driver binary and just have a browser installed in the machine.\n\n### A tip on coverage data using go 1.20 or later:\n\nCode coverage changes introduced in go 1.20 produce multiple \n[coverage data files](https://go.dev/testing/coverage/#working) in binary format.\n\nIn wasmbrowsertest, file system operations for coverage files occur via HTTP API calls.\n  \nPrefer using `-test.gocoverdir=/path/to/coverage` instead of `-test.coverprofile=coverage.out` \nwhen coverage data is needed. This will prevent http api calls that would read all the coverage data \nfiles and write the larger `coverage.out` file. \n\nIn a subsequent step, use `go tool covdata -i /path/to/coverage -o coverage.out` or similar to process coverage \ndata files into the desired output format. An additional benefit is that multiple test coverage runs that write \ntheir data to the same coverage directory can be merged together with this command.\n\n## Errors\n\n### `total length of command line and environment variables exceeds limit`\n\nIf the error `total length of command line and environment variables exceeds limit` appears, then\nthe current environment variables' total size has exceeded the maximum when executing Go Wasm binaries.\n\nTo resolve this issue, install `cleanenv` and use it to prefix your command.\n\nFor example, if these commands are used:\n```bash\nexport GOOS=js GOARCH=wasm\ngo test -cover ./...\n```\nThe new commands should be the following:\n```bash\ngo install github.com/agnivade/wasmbrowsertest/cmd/cleanenv@latest\n\nexport GOOS=js GOARCH=wasm\ncleanenv -remove-prefix GITHUB_ -- go test -cover ./...\n```\n\nThe `cleanenv` command above removes all environment variables prefixed with `GITHUB_` before running the command after the `--`.\nThe `-remove-prefix` flag can be repeated multiple times to remove even more environment variables.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagnivade%2Fwasmbrowsertest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagnivade%2Fwasmbrowsertest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagnivade%2Fwasmbrowsertest/lists"}