nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.63k stars 2.36k forks source link

Running nx migrate update-jest-config-to-use-util results in Cannot read property 'filter' of undefined #6563

Closed aspergillusOryzae closed 3 years ago

aspergillusOryzae commented 3 years ago

Current Behavior

Migration failure nx migrate --run-migrations=migrations.json --verbose fails with (absolute paths removed):

...
---------------------------------------------------------
Running migration support-jest-27
Successfully finished support-jest-27
---------------------------------------------------------
Running migration update-ts-jest-and-jest-preset-angular
Successfully finished update-ts-jest-and-jest-preset-angular
---------------------------------------------------------
Running migration update-jest-config-to-use-util
Cannot read property 'filter' of undefined
TypeError: Cannot read property 'filter' of undefined
    at determineUncoveredJestProjects (./node_modules/@nrwl/jest/src/migrations/update-12-6-0/update-base-jest-config.js:13:29)
    at updateBaseJestConfig (./node_modules/@nrwl/jest/src/migrations/update-12-6-0/update-base-jest-config.js:28:35)
    at ./node_modules/@nrwl/jest/src/migrations/update-12-6-0/update-base-jest-config.js:36:9
    at Generator.next (<anonymous>)
    at ./node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (./node_modules/tslib/tslib.js:113:16)
    at update (./node_modules/@nrwl/jest/src/migrations/update-12-6-0/update-base-jest-config.js:35:20)
    at /private/var/folders/9w/5cny_c711x340yk5j0kq_23c0000gn/T/tmp-6964-ZXlphEt740h4/node_modules/@nrwl/tao/src/commands/migrate.js:542:15
    at Generator.next (<anonymous>)
Command failed: /var/folders/9w/5cny_c711x340yk5j0kq_23c0000gn/T/tmp-6964-ZXlphEt740h4/node_modules/.bin/tao migrate --run-migrations --verbose

Expected Behavior

Successful migration

 >  NX  Successfully finished running migrations from 'migrations.json'

Steps to Reproduce

 >  git clean -f
 >  rm -rf node_modules
 >  yarn install
 >  npx nx migrate latest
 >  yarn install
 >  npx nx migrate --run-migrations=migrations.json --verbose

This is my first time using nx migrate, but if I take update-jest-config-to-use-util out of migrations.json it succeeds.

Failure Logs

Included in current behavior section above.

Environment

> npx nx report

> NX Report complete - copy this into the issue template

Node : 12.16.3 OS : darwin x64 yarn : 1.22.4

nx : Not Found @nrwl/angular : 12.6.3 @nrwl/cli : 12.6.3 @nrwl/cypress : 12.6.3 @nrwl/devkit : 12.6.3 @nrwl/eslint-plugin-nx : Not Found @nrwl/express : Not Found @nrwl/jest : 12.6.3 @nrwl/linter : 12.6.3 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/nx-cloud : Not Found @nrwl/react : Not Found @nrwl/schematics : Not Found @nrwl/tao : 12.6.3 @nrwl/web : Not Found @nrwl/workspace : 12.6.3 @nrwl/storybook : 12.6.3 @nrwl/gatsby : Not Found typescript : 4.3.5

From the migration.json file:

    {
      "version": "12.6.0-beta.0",
      "cli": "nx",
      "description": "Uses `getJestProjects()` to populate projects array in root level `jest.config.js` file.",
      "factory": "./src/migrations/update-12-6-0/update-base-jest-config",
      "package": "@nrwl/jest",
      "name": "update-jest-config-to-use-util"
    }
TassiloSinghammerVolue commented 3 years ago

Experienced the same issue after updating to 12.6.3

leosvelperez commented 3 years ago

Thanks for reporting this!

I'm not able to reproduce this in a fresh workspace using a previous version. It would seem that there might be something in the jest.config.js configuration you have that the migration doesn't expect.

Could you please share your jest.config.js file content? Or better yet, would you be able to provide a minimal repo where the issue can be reproduced?

TassiloSinghammerVolue commented 3 years ago

Hi, thanks for replying!

Here is the content of my jest.config.js already:

module.exports = { testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], transform: { '^.+\\.(ts|js|html)$': 'ts-jest', }, resolver: '@nrwl/jest/plugins/resolver', moduleFileExtensions: ['ts', 'js', 'html'], coverageReporters: ['html'], testEnvironment: 'jsdom', }; I will also try to setup a small repo, but it may take some time to recrate the exact configuration.

Could it also be a Windows problem again? I guess most people are working on a Unix system, but I'm "still" on Windows 10.

leosvelperez commented 3 years ago

Is that the jest.config.js at the root of the workspace?

If that's the case, the issue is happening because the migration is expecting it to have a projects property, which is what gets generated by default in Nx workspaces. That said, we can/should handle root configs without that property to avoid the migration to fail.

TassiloSinghammerVolue commented 3 years ago

It is at the root of the workspace (same place as the workspace.json). However it is not at the root of the GitHub repository but in a subfolder called "web" for historical reasons. Can I just add the projects property?

leosvelperez commented 3 years ago

Yes, you can either add projects: [] before running the migration (recommended), or just remove the update-jest-config-to-use-util migration from the migrations.json file if you prefer to keep your root config as it is now.

TassiloSinghammerVolue commented 3 years ago

Ok, cool! Will give it a try soon.

TassiloSinghammerVolue commented 3 years ago

The migration went through now! The only thing is after the successful migration my production build is now failing :-)

nx run navigator:build:production webpack.WebpackError is not a constructor

But that is probably not to be discussed here

leosvelperez commented 3 years ago

Yeah, that'd be a different issue.

aspergillusOryzae commented 3 years ago

This is my jest.config.js contents:

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|js|html)$': 'ts-jest',
  },
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageReporters: ['html'],
};

It is located at the root of my workspace, alongside package.json and migrations.json.

I also have two libs directories with the following jest.config.js contents at the parent level of each:

module.exports = {
  name: '<lib-name>',
  preset: '../../jest.config.js',
  coverageDirectory: '../../coverage/libs/<lib-name>',
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
};

module.exports = {
  name: '<lib-name1>',
  preset: '../../jest.config.js',
  coverageDirectory: '../../coverage/libs/<lib-name1>',
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
};
leosvelperez commented 3 years ago

@aspergillusOryzae I'll be pushing a fix for this issue soon. In the meantime, you can read this comment where a workaround was provided.

aspergillusOryzae commented 3 years ago

I'll give that a try, thanks!

aspergillusOryzae commented 3 years ago

I tried with the latest 12.6.4 and not adding projects: [] to my jest.config.js, but got the same error as before. When I added projects: [] to the parent jest.config.js the migration completed successfully. I did not need to modify my libs/<lib-name>/jest.config.js files (in case anyone else is in a similar situation). Post-migration the parent jest.config.js looks like:

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|js|html)$': 'ts-jest',
  },
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageReporters: ['html'],
  projects: getJestProjects(),
};
leosvelperez commented 3 years ago

@aspergillusOryzae the fix is in master now but it hasn't been released yet, it will be included in the next patch release.

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.