Search code examples
typescriptmocha.jsanchorsmartcontractssolana

`anchor test` error: Failed to run test: yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts: No such file or directory (os error 2)


I just initialized a solana dapp with anchor init. Then I built successfully with anchor build. I tried to test anchor test command too but it fails and I have not added any code yet. This is still untouched initialized code and this is the error message:

Failed to run test: yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts: No such file or directory (os error 2)

It says there is not file inside tests directory but there is mydapp.ts with this code

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { Mydapp } from "../target/types/mycalculatordapp";

describe("mydapp", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.Provider.env());

  const program = anchor.workspace.Mydapp as Program<Mydapp>;

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.rpc.initialize({});
    console.log("Your transaction signature", tx);
  });
});

Solution

  • I had to install yarn globally since I did not have it on my system

     sudo  npm i -g yarn
    

    Then I got ts-mocha command not found even though it was on package.json. so I run npm install to install all dependencies. Then I got ts node error

      sudo npm i -g ts-node
    

    Finally it works