lundi 7 mai 2018

Run lint and tests separately inside of jest

We're using jest-runner-eslint to run our eslint config, and then also using jest for our normal tests. Per the suggestion in the docs, we have our normal tests and our lint tests set up as two separate projects in the "projects field" of our config.

Unfortunately, I don't see how to run the lint tests and the "real" tests separately. Running jest with the --projects flag errors out because it assumes our project is a monorepo and we're trying to run tests inside of the monorepo folder.

An example:

$ yarn test --projects lint
yarn run v1.6.0
$ node scripts/test.js --env=jsdom --projects lint
Error: Can't find a root directory while resolving a config file path.
Provided path to resolve: lint

Note that we can run our tests together just fine, but the two suites can't be run separately.

Our jest config (loosely based on create-react-app)

{
    "projects": [
      {
        "displayName": "test",
        "collectCoverage": true,
        "coverageDirectory": "<rootDir>/coverage",
        "collectCoverageFrom": [
          "src/**/*.{js,jsx,mjs}"
        ],
        "coverageReporters": [
          "text-summary",
          "lcov",
          "json"
        ],
        "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js",
        "setupFiles": [
          "<rootDir>/config/polyfills.js",
          "<rootDir>/src/setupTest.js"
        ],
        "snapshotSerializers": [
          "enzyme-to-json/serializer"
        ],
        "testMatch": [
          "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
          "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
        ],
        "testEnvironment": "node",
        "testURL": "http://localhost",
        "transform": {
          "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
          "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
          "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
        },
        "transformIgnorePatterns": [
          "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
        ],
        "moduleNameMapper": {
          "^react-native$": "react-native-web"
        },
        "moduleFileExtensions": [
          "web.js",
          "mjs",
          "js",
          "json",
          "web.jsx",
          "jsx",
          "node"
        ]
      },
      {
        "runner": "jest-runner-eslint",
        "displayName": "lint",
        "testMatch": [
          "<rootDir>/src/**/*.js"
        ]
      }
    ]
}

Various versions:

  • OS: macOS Sierra 10.12 (although I've also seen this on Ubuntu 16)
  • Node: v8.11.1
  • Jest: 22.4.3
  • jest-runner-eslint: 0.5.0

Aucun commentaire:

Enregistrer un commentaire