anchor icon indicating copy to clipboard operation
anchor copied to clipboard

Test generated by anchor init yields error "Provider env is not available on browser."

Open akutruff opened this issue 3 years ago • 10 comments

Description:

The project created from anchor init throws an error when trying to run the generated test.

Reproduction:

anchor init test_project
cd test_project
anchor build
anchor test

Result:

Shows the following error:

BPF SDK: /home/node/.local/share/solana/install/releases/1.8.5/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
warning: unused variable: `ctx`
 --> programs/test_project/src/lib.rs:8:23
  |
8 |     pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
  |                       ^^^ help: if this is intentional, prefix it with an underscore: `_ctx`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `test_project` (lib) generated 1 warning
    Finished release [optimized] target(s) in 0.12s
cargo-build-bpf child: /home/node/.local/share/solana/install/releases/1.8.5/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /workspaces/dapwords/test_project/target/deploy/test_project.so

To deploy this program:
  $ solana program deploy /workspaces/dapwords/test_project/target/deploy/test_project.so
The program address will default to this keypair (override with --program-id):
  /workspaces/dapwords/test_project/target/deploy/test_project-keypair.json
yarn run v1.22.15
warning package.json: No license field
$ /workspaces/dapwords/test_project/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'

Error: Provider env is not available on browser.
    at Function.env (/workspaces/dapwords/test_project/node_modules/@project-serum/anchor/src/provider.ts:68:13)
    at Suite.<anonymous> (/workspaces/dapwords/test_project/tests/test_project.ts:8:38)
    at Object.create (/workspaces/dapwords/test_project/node_modules/mocha/lib/interfaces/common.js:148:19)
    at context.describe.context.context (/workspaces/dapwords/test_project/node_modules/mocha/lib/interfaces/bdd.js:42:27)
    at Object.<anonymous> (/workspaces/dapwords/test_project/tests/test_project.ts:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/workspaces/dapwords/test_project/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/workspaces/dapwords/test_project/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.exports.requireOrImport (/workspaces/dapwords/test_project/node_modules/mocha/lib/nodejs/esm-utils.js:56:20)
    at Object.exports.loadFilesAsync (/workspaces/dapwords/test_project/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
    at singleRun (/workspaces/dapwords/test_project/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at Object.exports.handler (/workspaces/dapwords/test_project/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

akutruff avatar Dec 14 '21 19:12 akutruff

Also note that running the tests in the typescript directory in the main repo also has the same failure.

https://github.com/project-serum/anchor/tree/master/tests/typescript

akutruff avatar Dec 14 '21 20:12 akutruff

Sorry to bump, but any comment would be appreciated. This is in the main release and a blocker for starting a new project with TypeScript.

akutruff avatar Dec 17 '21 15:12 akutruff

Hi managed to figure this out. For me it was the environment variable BROWSER was set to firefox. I cleared this environment variable by doing export BROWSER= and this worked

daniel-lahlafi avatar Dec 18 '21 21:12 daniel-lahlafi

@daniel-lahlafi I cleared this environment variable by doing export BROWSER=

Would you mind expanding on this? I'm running into the same issue but am not sure where to export BROWSER=. I've added it to the CLI, like BROWSER= anchor test, or BROWSER='' anchor test but neither work.

bholtbholt avatar Jan 09 '22 04:01 bholtbholt

@daniel-lahlafi I cleared this environment variable by doing export BROWSER=

Would you mind expanding on this? I'm running into the same issue but am not sure where to export BROWSER=. I've added it to the CLI, like BROWSER= anchor test, or BROWSER='' anchor test but neither work.

Just run the command I wrote in the terminal before running anchor

daniel-lahlafi avatar Jan 09 '22 16:01 daniel-lahlafi

export BROWER= worked just fine here. Thanks !

KingpinFisk avatar Jan 31 '22 10:01 KingpinFisk

above comment is missing "S" in BROWSER. export BROWSER= worked fine also here thanks!

masamizu avatar Apr 02 '22 11:04 masamizu

In most recent versions of anchor, the exported 'Provider' was moved to 'AnchorProvider'.

Had the same issue, but this was the problem !

Plitskine avatar Jun 08 '22 10:06 Plitskine

a little graceful command:

unset BROWSER

whatever, just make sure BROWSER environment variable unset then this issue would be fixed. ; )

Yakkhini avatar Jun 17 '22 17:06 Yakkhini

Error: Provider env is not available on browser. I have the same error but in my front end anyone one can help me please image

Xmohamed1X avatar Aug 09 '22 01:08 Xmohamed1X

BROWSER= anchor test, or BROWSER='' anchor test Thank you. Above command works well.

toniandsol avatar Oct 12 '22 18:10 toniandsol