1

I get the following error when I run anchor test to test my smart contract using typescript:

Error snippet

    abortSignal: options?.abortSignal,
                           ^
    SyntaxError: Unexpected token '.'

Full error log

warning: /home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/programs/alignment-negotiation/Cargo.toml: unused manifest key: package.author
    Finished release [optimized] target(s) in 0.21s

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/Anchor.toml"

yarn run v1.22.19
$ /home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'

/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/@solana/web3.js/lib/index.cjs.js:2022
      abortSignal: options?.abortSignal,
                           ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/@coral-xyz/anchor/src/nodewallet.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/@coral-xyz/anchor/src/index.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/tests/src-alignment-negotiation.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Module.m._compile (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Object.require.extensions.<computed> [as .ts] (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.implementationOfRequireOrImportForUnstableEsm [as requireOrImport] (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/nodejs/esm-utils.js:116:12)
    at Object.exports.loadFilesAsync (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/nodejs/esm-utils.js:103:34)
    at Mocha.loadFilesAsync (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/mocha.js:448:19)
    at singleRun (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/cli/run-helpers.js:125:15)
    at exports.runMocha (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/cli/run-helpers.js:190:10)
    at Object.exports.handler (/home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/mocha/lib/cli/run.js:374:11)
    at /home/darren/git/projects/github.com/tome-gg/negotiation-protocols/src/alignment-negotiation/node_modules/yargs/build/index.cjs:443:71
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What does it mean? How do I fix it?

1 Answer 1

1

Context

The error message specifically complains about the . which is the Optional Chaining feature in Javascript.

The reason why optional chaining did not make sense (i.e. SyntaxError) is because the current version of node I was using does not have support for Optional Chaining, because it was an old version of node.

Optional chaining was supported officially supported in ES2020.

Solution

Use nvm to use a more updated node version when running typescript/javascript programs (like ts-mocha, which anchor uses for running tests).

nvm use v16.14.0

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.