Search code examples
typescriptmocha.jsblockchainsmartcontractssolana

TypeError: Unknown file extension ".ts" in a Solana Anchor project


I got this error after updating Solana-cli to 1.9.4 and Anchor-cli to 0.20.1 and other local npm versions...

% anchor test
....

TypeError: Unknown file extension ".ts" for /Users/me/Code/solana/myapp/tests/myapp.ts

Please advise. Thank you


Solution

  • This is actually an issue in ts-mocha which anchor uses by default to run tests. Since you are using ES6 imports, "esModuleInterop": true has to be set. This allows the inference of types from modules.

    Set your tsconfig.json file like this:

    {
      "compilerOptions": {
        "types": ["mocha", "chai"],
        "typeRoots": ["./node_modules/@types"],
        "lib": ["es2015"],
        "module": "commonjs",
        "target": "es6",
        "esModuleInterop": true
      }
    }
    

    Also note that there are other causes of this issue - this may be a .