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

Importing @lodestar/types fails, it's unusable #6076

Closed
mbaranovski opened this issue Oct 30, 2023 · 2 comments
Closed

Importing @lodestar/types fails, it's unusable #6076

mbaranovski opened this issue Oct 30, 2023 · 2 comments
Labels
meta-bug Issues that identify a bug and require a fix.

Comments

@mbaranovski
Copy link

Describe the bug

Importing @lodestar/types fails, cannot make it work.

Expected behavior

One can import and use the @lodestar/types module

Steps to reproduce

  1. Clone repo: https://github.com/mbaranovski/deleteme
  2. Run npm i
  3. Run npm run test
  4. Obeserve ERROR: src/main.ts:5:21 - error TS2307: Cannot find module '@lodestar/types' or its corresponding type declarations.
  5. Change moduleResolution in tsconfig.json to node, re-run 3. and observe: Cannot find module '@lodestar/types' from 'src/main.ts'

Additional context

No response

Operating system

macOS

Lodestar version or commit hash

@lodestar/types 1.11.3

@mbaranovski mbaranovski added the meta-bug Issues that identify a bug and require a fix. label Oct 30, 2023
@mbaranovski
Copy link
Author

mbaranovski commented Oct 31, 2023

I had to do a full conversion to ESM modules for the Jest to start working. I followed this guide.
Pasting my files in case someone has similar issues.

package.json - must include "type": "module"
tsconfig.json

{
  "compilerOptions": {
    "target": "es2022",
    "module": "NodeNext",
    "lib": ["ES2022"],
    "moduleResolution": "NodeNext",
    "rootDir": "./",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "baseUrl": "./",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

setup-jest.js

import * as td from 'testdouble'
import { jest } from '@jest/globals'
import matchers from 'jest-extended'

expect.extend(matchers)

afterEach(function () {
  td.reset()
  jest.useRealTimers()
})

jest.config.cjs

module.exports = {
  transform: {
    '^.+\\.(t|j)sx?$': '@swc/jest',
  },
  extensionsToTreatAsEsm: ['.ts', '.tsx'],
  setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^(\\.{1,2}/.*)\\.js$': '$1',
  },
  resetMocks: true,
  workerThreads: true,
}

Important

You need to run tests with node --experimental-vm-modules ./node_modules/jest/bin/jest.js

@nflaig
Copy link
Member

nflaig commented Oct 31, 2023

I quickly looked at this yesterday but couldn't figure it out, wanted to look at it again today but glad you figured out a solution and thanks for sharing it here.

We recently started to migrate from mocha to vitest, and jest was also considered as an option ,see #6028 for details.

This PR highlights the following for jest

As of latest version of Jest 29.7 the Jest support for ESM modules are still experimental while the Vitest is all build on EMS and support it natively.

Maybe there is a simpler way to make your setup work, @nazarhussain might have some suggestions.

Wondering if there is anything we could do on our end to make this less painful, but I guess at some point ESM will be the default everywhere and those issues will go away..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta-bug Issues that identify a bug and require a fix.
Projects
None yet
Development

No branches or pull requests

2 participants