nrwl / nx

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

Using affected:test with multiple Jest reporters fails because of flags merging #3450

Closed aramperes closed 4 years ago

aramperes commented 4 years ago

Current Behavior

Running the command:

nx affected:test --reporters=default --reporters=jest-junit --base=master

Nx merges the --reporters flags into one:

>  NX  Running target test for projects:

  [ snip ]

  With flags:
    --reporters=default,jest-junit

Which causes the following error:

> ng run [library]:test --reporters=default,jest-junit 
An unhandled exception occurred: Could not resolve a module for a custom reporter.
  Module name: default,jest-junit
See "/tmp/ng-hrBzEh/angular-errors.log" for further details.

In fact, Jest doesn't know to split the reporters by commas. It expects two flags.

Expected Behavior

Multiple flags being passed should be preserved that way, instead of merging with , when passing down to the actual script.

Steps to Reproduce

  1. Run nx affected:test with two flags of the same name, e.g. nx affected:test --blah=a --blah=b
  2. Observe that the With flags: section lists --blah=a,b

Failure Logs

The failure on the Jest side:

[error] Error: Could not resolve a module for a custom reporter.
  Module name: default,jest-junit
    at node_modules/jest-config/build/normalize.js:567:15
    at Array.map (<anonymous>)
    at normalizeReporters (node_modules/jest-config/build/normalize.js:549:33)
    at normalize (node_modules/jest-config/build/normalize.js:642:5)
    at readConfig (node_modules/jest-config/build/index.js:220:68)
    at async readConfigs (node_modules/jest-config/build/index.js:401:26)
    at async Object.runCLI (node_modules/@jest/core/build/cli/index.js:198:59

Environment

  @nrwl/angular : 10.0.6
  @nrwl/cli : 10.0.6
  @nrwl/cypress : 10.0.6
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : Not Found
  @nrwl/jest : 10.0.6
  @nrwl/linter : Not Found
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 10.0.6
  @nrwl/web : Not Found
  @nrwl/workspace : 10.0.6
  typescript : 3.9.7
aramperes commented 4 years ago

As a workaround, we can specify reporters in jest.config.js. Since we wanted to have different reporters for different commands, we can use environment variables in there:

// jest.config.js
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'],
    reporters: process.env['CI'] === '1' ? ['default', 'jest-junit'] : ['default']
};
ggwzrd commented 2 years ago

I am still experiencing this bug on v12.9.0 when vscode-jest is running the command

nx run pwa:test --testLocationInResults --json --useStderr --outputFile=out/dir/folder --watch --coverage --reporters=~/.vscode/extensions/orta.vscode-jest-4.2.1/out/reporter.js --colors

Overwriting reporters in config-overrides.js for react-app-rewired does not work.

Jest Config

    config.resolver = '@nrwl/jest/plugins/resolver';
    config.reporters = process.env['CI'] === '1' ? ['default', 'jest-junit'] : ['default']
    config.collectCoverageFrom = [
      'src/**/*.{js,ts,tsx}',
      '!**/*story.{js,ts,tsx}',
      '!**/*styles.{js,ts,tsx}',
      '!**/*connect.{js,ts,tsx}',
    ];
    config.moduleDirectories = ["<rootDir>/src", "node_modules"];
    config.setupFilesAfterEnv = ["./src/setupTests.js"];
    config.snapshotSerializers = ["enzyme-to-json/serializer"];
    config.testEnvironment = "jsdom";
    config.testPathIgnorePatterns = ["<rootDir>/src/Storyshots.test.js"];
    config.testRunner = "jest-circus/runner";
    config.transform = {
      "^.+\\.[t|j]sx?$": "babel-jest",
      ".+\\.(css|styl|less|sass|scss)$": "jest-transform-css",
      "^.+.(svg|png|jpg)$": "jest-transform-stub",
    };
    config.transformIgnorePatterns = [
      "[/]node_modules[/](?!quill).+.(js|jsx|ts|tsx)$",
      "^.+.module.(css|sass|scss)$",
    ];
    config.watchPathIgnorePatterns = ["/node_modules/", "/.visualTesting/"];

devDependencies

"devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/preset-react": "^7.9.0",
    "@babel/preset-typescript": "^7.12.7",
    "@nrwl/cli": "12.9.0",
    "@nrwl/cypress": "12.9.0",
    "@nrwl/eslint-plugin-nx": "12.9.0",
    "@nrwl/jest": "12.9.0",
    "@nrwl/linter": "12.9.0",
    "@nrwl/react": "^12.9.0",
    "@nrwl/tao": "12.9.0",
    "@nrwl/web": "12.9.0",
    "@nrwl/workspace": "12.9.0",
    "@storybook/addon-actions": "^6.1.21",
    "@storybook/addon-docs": "^6.1.21",
    "@storybook/addon-storyshots": "^6.1.21",
    "@storybook/addon-storyshots-puppeteer": "^6.1.21",
    "@storybook/addon-viewport": "^6.1.21",
    "@storybook/addons": "^6.1.21",
    "@storybook/preset-create-react-app": "^3.1.4",
    "@storybook/react": "^6.1.21",
    "@testing-library/react-hooks": "^5.1.0",
    "@types/classnames": "^2.2.11",
    "@types/jest": "26.0.20",
    "@types/lodash": "^4.14.168",
    "@types/node": "^14.14.20",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-loadable": "^5.5.6",
    "@types/react-redux": "^7.1.16",
    "@types/react-router-dom": "^5.1.7",
    "@types/redux-form": "^8.3.1",
    "@types/smoothscroll-polyfill": "^0.3.1",
    "@typescript-eslint/eslint-plugin": "^4.13.0",
    "@typescript-eslint/parser": "^4.13.0",
    "@wojtekmaj/enzyme-adapter-react-17": "^0.4.1",
    "ansi-parser": "^3.2.9",
    "babel-eslint": "8.2.5",
    "babel-jest": "^26.3.0",
    "babel-loader": "^8.1.0",
    "cross-env": "^7.0.2",
    "cypress": "^8.3.0",
    "enzyme": "^3.10.0",
    "enzyme-to-json": "^3.4.3",
    "eslint": "^7.17.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-airbnb-typescript": "^12.0.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-config-react-app": "^5.2.1",
    "eslint-plugin-cypress": "^2.10.3",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jest": "^23.6.0",
    "eslint-plugin-jsx-a11y": "^6.4.0",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "express": "^4.17.1",
    "fs-extra": "^8.0.1",
    "husky": "^4.3.0",
    "jest": "^26.3.0",
    "jest-canvas-mock": "^2.2.0",
    "jest-circus": "^26.3.0",
    "jest-junit": "^10.0.0",
    "jest-transform-css": "^2.0.0",
    "jest-transform-stub": "^2.0.0",
    "jest-watch-typeahead": "0.6.5",
    "lint-staged": "^10.3.0",
    "node-fetch": "^2.6.0",
    "open": "^7.0.2",
    "prettier": "^2.1.1",
    "pretty-quick": "^3.0.1",
    "puppeteer": "^2.1.1",
    "react-app-rewired": "^2.1.8",
    "redux-mock-store": "^1.5.3",
    "require-context.macro": "^1.2.2",
    "source-map-explorer": "^2.5.0",
    "storybook-react-router": "^1.0.8",
    "ts-jest": "^26.5.3",
    "typescript": "^4.1.3"
  }

Stack Trace

Error: Could not resolve a module for a custom reporter.
  Module name: default,/User/username/.vscode/extensions/orta.vscode-jest-4.2.1/out/reporter.js
    at /User/username/path/to/project/node_modules/jest-config/build/normalize.js:558:15
    at Array.map (<anonymous>)
    at normalizeReporters (/User/username/path/to/project/node_modules/jest-config/build/normalize.js:540:33)
    at normalize (/User/username/path/to/project/node_modules/jest-config/build/normalize.js:633:5)
    at readConfig (/User/username/path/to/project/node_modules/jest-config/build/index.js:224:68)
    at readConfigs (/User/username/path/to/project/node_modules/jest-config/build/index.js:406:32)
    at runCLI (/User/username/path/to/project/node_modules/@jest/core/build/cli/index.js:231:29)
    at Object.run (/User/username/path/to/project/node_modules/jest-cli/build/cli/index.js:163:62)
    at Object.<anonymous> (/User/username/path/to/project/node_modules/react-scripts/scripts/test.js:138:6)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
ERROR: Something went wrong in @nrwl/run-commands - Command failed: node ../../node_modules/.bin/react-app-rewired test --testLocationInResults=true --json=true --useStderr=true --outputFile=/var/folders/62/nr65kd5559j3c7kmyyj2zhr80000gn/T/jest_runner_projet.json --watch=true --coverage=true --reporters=default,/User/username/.vscode/extensions/orta.vscode-jest-4.2.1/out/reporter.js --colors=true
ggwzrd commented 2 years ago

UPDATE: I manage to fix the bug by completely bypassing nx and directly running jest using react-app-rewire since I am migrating from CRA.

Below my settings.js configs:

{
  "jest.jestCommandLine": "node ../../node_modules/.bin/react-app-rewired test",
  "jest.showCoverageOnLoad": true,
  "jest.debugMode": true,
  "jest.autoRun": "off",
  "jest.rootPath":"/apps/your-app"
}

Hope this helps ✌️

@bekos or @aramperes would be nice to hear from you too since this was the issue you guys worked on. Maybe I did setup something wrongly.

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.