Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Jest and @orangekit/sdk support #418

Merged
merged 3 commits into from
May 12, 2024

Conversation

nkuba
Copy link
Member

@nkuba nkuba commented May 10, 2024

Depends on: #417

After we added @orangekit/sdk import Jest started to complain with:

Jest encountered an unexpected token

Details:

/Users/jakub/workspace/acre/acre/node_modules/.pnpm/@[email protected]/node_modules/@orangekit/sdk/dist/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "./lib";
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

> 1 | import { OrangeKitSdk } from "@orangekit/sdk"
    | ^
  2 | import { JsonRpcProvider } from "ethers"
  3 | import { AcreContracts } from "./lib/contracts"
  4 | import { EthereumNetwork, getEthereumContracts } from "./lib/ethereum"

  at Runtime.createScriptFromCode (../node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1505:14)
  at Object.<anonymous> (src/acre.ts:1:1)
  at Object.<anonymous> (src/index.ts:9:1)
  at Object.<anonymous> (test/modules/tbtc/Tbtc.test.ts:7:1)

The @orangekit/sdk module exports dist/ directory containing JS files in ESM syntax.
Jest doesn't support ESM syntax, so we have to convert these JS files to
CommonJS syntax with ts-jest/presets/js-with-ts preset.
We have to define transformIgnorePatterns property as node_modules/
directory is excluded from transformations by default.

We also had to add allowJs: true in tsconfig.json to support
transformation of JS files.

Unfortunately, it increased test execution time, so it may be better to mock the @orangekit/sdk in unit tests instead of transforming the code.

Reference:

nkuba added 3 commits May 11, 2024 00:15
According to the [jest
documentation](https://jestjs.io/docs/getting-started#using-typescript)
there are two ways to get TypeScript to work with Jest:
- babel
- ts-jest

We already use ts-jest preset in jest configuration, so we don't need to
import babel presets.
We use the JestConfigWithTsJest type to be explicit about the type of
the config object we're dealing with.
After we added @orangekit/sdk import Jest started to complain with:
```
Jest encountered an unexpected token

Details:

/Users/jakub/workspace/acre/acre/node_modules/.pnpm/@orangekit[email protected]/node_modules/@orangekit/sdk/dist/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "./lib";
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

> 1 | import { OrangeKitSdk } from "@orangekit/sdk"
    | ^
  2 | import { JsonRpcProvider } from "ethers"
  3 | import { AcreContracts } from "./lib/contracts"
  4 | import { EthereumNetwork, getEthereumContracts } from "./lib/ethereum"

  at Runtime.createScriptFromCode (../node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1505:14)
  at Object.<anonymous> (src/acre.ts:1:1)
  at Object.<anonymous> (src/index.ts:9:1)
  at Object.<anonymous> (test/modules/tbtc/Tbtc.test.ts:7:1)
```

The `@orangekit/sdk` module exports `dist/` directory containing JS files in ESM syntax.
Jest doesn't support ESM syntax, so we have to convert these JS files to
CommonJS syntax with `ts-jest/presets/js-with-ts` preset.
We have to define `transformIgnorePatterns` property as `node_modules/`
directory is excluded from transformations by default.

We also had to add `allowJs: true` in tsconfig.json to support
transformation of JS files.

Reference:
- https://stackoverflow.com/questions/75452411/why-isnt-jest-handling-es-module-dependencies
- https://stackoverflow.com/questions/49263429/jest-gives-an-error-syntaxerror-unexpected-token-export?noredirect=1&lq=1
@nkuba nkuba requested a review from r-czajkowski May 10, 2024 22:29
Copy link
Contributor

@r-czajkowski r-czajkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@r-czajkowski r-czajkowski marked this pull request as ready for review May 12, 2024 18:02
@r-czajkowski r-czajkowski merged commit a3b16d1 into bitcoin-native-experience May 12, 2024
19 of 21 checks passed
@r-czajkowski r-czajkowski deleted the jest-orangekit-fix branch May 12, 2024 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants