babylonchain / simple-staking

Simple staking application
Other
4 stars 13 forks source link

Set up unit tests that works with staking ts lib #253

Closed supertong closed 3 months ago

supertong commented 3 months ago

The main problem of those import complains are caused by the testEnvironment setup in Jest config.

When we set testEnvironment: jsdom, the jest default resolver would assume it's in browser environment which means it will consume the browser condition from the exports. For example, the @bitcoin-js/tiny-secp256k1-asmjs has the following exports field

  "exports": {
    ".": {
      "node": {
        "module": "./lib/index.js",
        "require": "./lib/cjs/index.cjs",
        "import": "./lib/index.js"
      },
      "browser": "./lib/index.js",
      "default": "./lib/index.js"
    }
  },

The Jest resolver will pick up "browser": "./lib/index.js", however what we want is the cjs version for the test.

We had tried a few fix and it didn't quite work and these fixes includes

I have also updated jest configs to use the one from next/jest to simplify things a bit.

Also, the CI build requires the PR https://github.com/babylonchain/btc-staking-ts/pull/27 to be released into NPM and bump the package.

jrwbabylonlab commented 3 months ago

@supertong i added below in the jest config to get away with the node annotation in each ts file

projects: [
    {
      displayName: "node",
      testEnvironment: "node",
      testMatch: ["<rootDir>/tests/**/*.test.ts"],
    },
    {
      displayName: "jsdom",
      testEnvironment: "jsdom",
      testMatch: ["<rootDir>/tests/**/*.test.tsx"],
    },
  ],
jrwbabylonlab commented 3 months ago

@gbarkhatov and @jeremy-babylonchain i will let u guys review this as i heavily involved in making of this PR. there is no point for me to approve commits from myself