teambit / bit

A build system for development of composable software.
https://bit.dev
Other
17.84k stars 924 forks source link

Failing jest test: tsconfig with type @testing-library/jest-dom and custom bit environment #7437

Closed webDnA2022 closed 10 months ago

webDnA2022 commented 1 year ago

Description

I have created a react library with a custom environment consisting of jest.config, jest.setup, tsconfig, etc.. Everything works fine, I can push my components to bit.cloud. I can also install and import components from bit.dev.

But when I changed one of the imported component (from bit.cloud) and run bit build I receive the following error:

env: webdna.react-library/envs/custom-react-env@0.0.7, task "teambit.compilation/compiler:TypescriptCompile" threw an error
error TS2688: Cannot find type definition file for '@testing-library/jest-dom'.
  The file is in the program because:
    Entry point of type library '@testing-library/jest-dom' specified in compilerOptions

  /Users/duc/Library/Caches/Bit/capsules/42500ebf15ac7ef9689ae43af59e721ff218902d/webdna.react-library_library_ui_button@0.0.8/tsconfig.json:22:7
    22       "@testing-library/jest-dom"
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    File is entry point of type library specified here.

Specifications

Context and additional information

The error refers to the tsconfig.json using @testing-library/jest-dom in types. Does the environment do not have access to the node_modules? Below is my tsconfig.json

{
  "extends": "./node_modules/@teambit/react.react-env/config/tsconfig.json",
  "compilerOptions": {
    "noEmit": true,
    "jsx": "react-jsx",
    "allowUmdGlobalAccess": true,
    "types": ["jest", "@testing-library/jest-dom"],
    "module": "CommonJS",
    "target": "ES6"
  },
  "include": ["**/*", "**/*.json", ".eslintrc.js"],
  "exclude": ["dist"]
}

When I run bit test I receive the following error message:

Test suite failed to run

    Cannot find module 'babel-plugin-macros'
    Require stack:
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/lib/config/files/plugins.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/lib/config/files/index.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/lib/index.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+babel-plugin-transform-typescript-metadata@0.3.2_@babel+core@7.20.2/node_modules/babel-plugin-transform-typescript-metadata/lib/parameter/parameterVisitor.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+babel-plugin-transform-typescript-metadata@0.3.2_@babel+core@7.20.2/node_modules/babel-plugin-transform-typescript-metadata/lib/plugin.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/@teambit+defender.jest-tester@0.0.8_@teambit+legacy@..+..+..+..+.bvm+versions+0.1.42+bit-0.1._krtfdkgkhpfbhzwecvasuotvay/node_modules/@teambit/defender.jest-tester/dist/config/transformers/base-transformer-plugins.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/@teambit+defender.jest-tester@0.0.8_@teambit+legacy@..+..+..+..+.bvm+versions+0.1.42+bit-0.1._krtfdkgkhpfbhzwecvasuotvay/node_modules/@teambit/defender.jest-tester/dist/config/transformers/index.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/@teambit+defender.jest-tester@0.0.8_@teambit+legacy@..+..+..+..+.bvm+versions+0.1.42+bit-0.1._krtfdkgkhpfbhzwecvasuotvay/node_modules/@teambit/defender.jest-tester/dist/config/index.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/@teambit+defender.jest-tester@0.0.8_@teambit+legacy@..+..+..+..+.bvm+versions+0.1.42+bit-0.1._krtfdkgkhpfbhzwecvasuotvay/node_modules/@teambit/defender.jest-tester/dist/index.js
    - /Users/duc/Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/webdna.react-library_envs_custom-react-env@0.0.7/dist/custom-react-env.bit-env.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/aspect-loader/dist/plugin.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/aspect-loader/dist/plugins.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/aspect-loader/dist/aspect-loader.main.runtime.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/bit/dist/load-bit.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/bit/dist/app.js
    - /Users/duc/.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/bit/bin/bit

      at resolve (../../../../.bvm/versions/0.1.42/bit-0.1.42/node_modules/@teambit/toolbox.performance.v8-cache/v8-compile-cache.ts:62:23)
      at tryRequireResolve (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/files/plugins.ts:129:34)
      at tryRequireResolve (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/files/plugins.ts:154:19)
      at resolveStandardizedNameForRequire (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/files/plugins.ts:180:12)
      at sync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:182:19)
      at ../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:210:24
      at resolveStandardizedName (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/files/plugins.ts:32:17)
          at resolvePlugin.next (<anonymous>)
      at resolvePlugin (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/files/plugins.ts:44:27)
          at loadPlugin.next (<anonymous>)
      at resolver (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-descriptors.ts:322:35)
          at createDescriptor.next (<anonymous>)
      at evaluateSync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:251:28)
      at ../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:31:34
          at Array.map (<anonymous>)
      at Function.sync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:31:22)
      at Function.all (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:210:24)
      at all (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-descriptors.ts:263:38)
          at createDescriptors.next (<anonymous>)
      at createDescriptors (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-descriptors.ts:253:17)
          at createPluginDescriptors.next (<anonymous>)
      at fn (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/gensync-utils/functional.ts:10:54)
      at plugins (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-chain.ts:725:34)
          at mergeChainOpts.next (<anonymous>)
      at mergeChainOpts (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-chain.ts:662:14)
          at chainWalker.next (<anonymous>)
      at loadFileChainWalker (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-chain.ts:397:24)
          at loadFileChain.next (<anonymous>)
      at loadFileChain (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/config-chain.ts:185:27)
          at buildRootChain.next (<anonymous>)
      at loadPrivatePartialConfig (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/partial.ts:110:44)
          at loadPrivatePartialConfig.next (<anonymous>)
      at Function.loadPrivatePartialConfig (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/partial.ts:173:12)
      at evaluateSync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:251:28)
      at Function.sync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+gensync@1.0.0-beta.2/node_modules/gensync/index.js:89:14)
      at sync (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@babel+core@7.20.2/node_modules/@babel/core/src/config/index.ts:57:62)
      at ScriptTransformer._getCacheKey (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@jest+transform@29.5.0/node_modules/@jest/transform/build/ScriptTransformer.js:228:41)
      at ScriptTransformer._getFileCachePath (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@jest+transform@29.5.0/node_modules/@jest/transform/build/ScriptTransformer.js:289:27)
      at ScriptTransformer.transformSource (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@jest+transform@29.5.0/node_modules/@jest/transform/build/ScriptTransformer.js:522:32)
      at ScriptTransformer._transformAndBuildScript (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@jest+transform@29.5.0/node_modules/@jest/transform/build/ScriptTransformer.js:671:40)
      at ScriptTransformer.transform (../../../../Library/Caches/Bit/capsules/1cab4fb06fff627161777e5eb3389d50baba7405/node_modules/.pnpm/registry.npmjs.org+@jest+transform@29.5.0/node_modules/@jest/transform/build/ScriptTransformer.js:723:19)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.467 s

Some other configs that I am using:

//babel-jest.config.json

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "@babel/preset-typescript",
    [
      "@babel/preset-react",
      {
        "runtime": "automatic"
      }
    ]
  ],
  "plugins": ["macros"]
}
//jest.config.js

/**
 * @see https://bit.dev/reference/jest/jest-config
 */
const { jestConfig } = require('@teambit/react.react-env');
const {
  generateNodeModulesPattern,
} = require('@teambit/dependencies.modules.packages-excluder');

const packagesToExclude = ['a-package-to-exclude'];

/**
 * by default, jest excludes all node_modules from the transform (compilation) process.
 * the following config excludes all node_modules, except for Bit components, style modules, and the packages that are listed.
 */
module.exports = {
  ...jestConfig,
  transformIgnorePatterns: [
    '^.+.module.(css|sass|scss)$',
    generateNodeModulesPattern({
      packages: packagesToExclude,
      excludeComponents: true,
    }),
  ],
  transform: {
    '\\.[jt]sx?$': [
      'babel-jest',
      { cwd: __dirname, configFile: './babel-jest.config.json' },
    ],
  },
  passWithNoTests: true,
  setupFilesAfterEnv: [require.resolve('./jest.setup.js')],
};

How can I solve this issue? I am happy for any hints.

Thank you! Duc

GiladShoham commented 1 year ago

In your babel-jest.config.json try to actually require the packages otherwise jest will try to resolve it from the wrong location. Also, since these plugins are only used in the babel config file, check if bit recognized these as dependencies of your env. you can check it with bit show <env-id> if not, you need to manually set them by bit deps set <env-id> <babel-plugin>

webDnA2022 commented 1 year ago

Thanks for the tips! So do I need to convert my json to a js-file to apply require? This example indicates that I can use the config as usual https://bit.dev/reference/babel/babel-config#installing-presets-and-plugins, doesn't it?

I have now added the preset and plugins to the env with bit deps set as you have described and they are listed when running bit show.

I have installed, compiled, tagged and exported the component with the updated env and use bit import --merge to merge it with my local component changes in my other project. But I still receive the error regarding @testing-library/jest-dom in my tsconfig.json when running bit build. It seems that tsconfig.json does not have access to the type:

✖ env: webdna.react-library/envs/custom-react-env@0.0.9, task "teambit.compilation/compiler:TypescriptCompile" threw an error
error TS2688: Cannot find type definition file for '@testing-library/jest-dom'.
  The file is in the program because:
    Entry point of type library '@testing-library/jest-dom' specified in compilerOptions

  /Users/duc/Library/Caches/Bit/capsules/42500ebf15ac7ef9689ae43af59e721ff218902d/webdna.react-library_library_ui_button@0.0.10/tsconfig.json:22:7
    22       "@testing-library/jest-dom"
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    File is entry point of type library specified here.

Do you have any other ideas?

webDnA2022 commented 1 year ago

I could resolved the part with the macro error. My mistake was that I set macro as dependency instead of babel-plugin-macros.

I have also checked on the type for @testing-library/jest-dom and have replaced it with @types/testing-library__jest-dom but still the same error message

webDnA2022 commented 1 year ago

I could resolved the part with the macro error. My mistake was that I set macro as dependency instead of babel-plugin-macros.

I have also checked on the type for @testing-library/jest-dom and have replaced it with @types/testing-library__jest-dom but still the same error message

If I remove @types/testing-library__jest-dom and implement @testing-library/jest-dom as instructed by its creators like:

// In tsconfig.json
  "include": [
    ...
    "./jest-setup.ts"
  ],
// In your own jest-setup.js (or any other name)
import '@testing-library/jest-dom'

it does not recognized the extended test methods

The following errors were found while running the build pipeline
Failed task 1: "teambit.compilation/compiler:TypescriptCompile" of env "webdna.react-library/envs/custom-react-env@0.0.13"
component: webdna.react-library/library/ui/button@0.0.14
/Users/duc/Library/Caches/Bit/capsules/42500ebf15ac7ef9689ae43af59e721ff218902d/webdna.react-library_library_ui_button@0.0.14/button.spec.tsx:29:40 - error TS2339: Property 'toBeDisabled' does not exist on type 'JestMatchers<HTMLElement>'.

29     expect(screen.getByRole('button')).toBeDisabled();
                                          ~~~~~~~~~~~~

Found 1 errors in 1 components

✖ Total 10 tasks. 1 succeeded. 1 failed. 8 skipped. Total errors: 1.
webDnA2022 commented 1 year ago

@GiladShoham I was able to resolve the issue. I needed to add the following in the env.jsonc of my custom environment in addition to set the dependency with bit deps set <my-env> @types/testing-library__jest-dom:

{
        "name": "@types/testing-library__jest-dom",
        "version": "^5.14.6",
        "hidden": true,
        "force": true
},

The issue is resolved for now but it left me with open questions.

  1. Why is it not enough to set @types/testing-library__jest-dom as env dependency to make it available for the components which uses the custom env?
  2. I understand that adding dependencies to env.jsonc is required when I do not import that specific package in my component but still want to provide it and set force: true. In the case of @types/testing-library__jest-dom it provided it to each component which eventually made it work. Is my understanding correct?
  3. Is there a way to add dependencies to the env.jsonc from the terminal or do I need to add in manually in the file?

Thanks in advance for your effort and time

GiladShoham commented 1 year ago

I'm not available for a few days but I'll try to write you an answer on Sunday

itaymendel commented 10 months ago

env templates solved to fix this issue. this ticket is stale anyway. closing.

benjgil commented 8 months ago

Just to actually respond to the questions left open here: