From d2f3a514b480d99ced6441ddf93a571809ff8a51 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Fri, 10 May 2024 23:43:29 +0200 Subject: [PATCH 1/3] Remove @babel/preset-env 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. --- pnpm-lock.yaml | 3 --- sdk/babel.config.js | 1 - sdk/package.json | 1 - 3 files changed, 5 deletions(-) delete mode 100644 sdk/babel.config.js diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f973a758f..2db3f9d37 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -151,9 +151,6 @@ importers: specifier: ^6.10.0 version: 6.10.0 devDependencies: - '@babel/preset-env': - specifier: ^7.23.7 - version: 7.23.7(@babel/core@7.23.3) '@ethersproject/bignumber': specifier: ^5.7.0 version: 5.7.0 diff --git a/sdk/babel.config.js b/sdk/babel.config.js deleted file mode 100644 index bb1dd9599..000000000 --- a/sdk/babel.config.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = { presets: ["@babel/preset-env"] } diff --git a/sdk/package.json b/sdk/package.json index f8aeb9002..808b8d711 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -14,7 +14,6 @@ "test": "jest --verbose" }, "devDependencies": { - "@babel/preset-env": "^7.23.7", "@ethersproject/bignumber": "^5.7.0", "@thesis-co/eslint-config": "github:thesis/eslint-config#7b9bc8c", "@types/jest": "^29.5.11", From ddfaae425aedcd13c795bd2bcb9fb6ab9dc75648 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Fri, 10 May 2024 23:48:56 +0200 Subject: [PATCH 2/3] Use JestConfigWithTsJest type in jest config file We use the JestConfigWithTsJest type to be explicit about the type of the config object we're dealing with. --- sdk/jest.config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdk/jest.config.ts b/sdk/jest.config.ts index 37ce37c74..d15006424 100644 --- a/sdk/jest.config.ts +++ b/sdk/jest.config.ts @@ -1,4 +1,8 @@ -export default { +import type { JestConfigWithTsJest } from "ts-jest" + +const jestConfig: JestConfigWithTsJest = { preset: "ts-jest", testPathIgnorePatterns: ["/dist/", "/node_modules/"], } + +export default jestConfig From 553b5be6ab75587dcfc3a17c00ee25b3e19ae4dc Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Sat, 11 May 2024 00:18:27 +0200 Subject: [PATCH 3/3] Fix Jest and @orangekit/sdk support 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+sdk@1.0.0-beta.9/node_modules/@orangekit/sdk/dist/src/index.js:1 ({"Object.":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/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:1505:14) at Object. (src/acre.ts:1:1) at Object. (src/index.ts:9:1) at Object. (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 --- sdk/jest.config.ts | 3 ++- sdk/tsconfig.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sdk/jest.config.ts b/sdk/jest.config.ts index d15006424..04e040912 100644 --- a/sdk/jest.config.ts +++ b/sdk/jest.config.ts @@ -1,8 +1,9 @@ import type { JestConfigWithTsJest } from "ts-jest" const jestConfig: JestConfigWithTsJest = { - preset: "ts-jest", + preset: "ts-jest/presets/js-with-ts", testPathIgnorePatterns: ["/dist/", "/node_modules/"], + transformIgnorePatterns: ["/node_modules/(?!@orangekit/sdk)/"], } export default jestConfig diff --git a/sdk/tsconfig.json b/sdk/tsconfig.json index 4ef31968f..df2c1f6d0 100644 --- a/sdk/tsconfig.json +++ b/sdk/tsconfig.json @@ -11,7 +11,8 @@ "esModuleInterop": true, "moduleResolution": "Bundler", "resolveJsonModule": true, - "skipLibCheck": true + "skipLibCheck": true, + "allowJs": true }, "include": ["src", "test", "src/typings.d.ts", "jest.config.js"] }