user5520186
user5520186

Reputation:

Jest's --findRelatedTests doesn't notice JSON files

I'm using jest --findRelatedTests in my pre-commit hook and it works fine. The only thing that it doesn't notice are changed JSON files.

If I have a JSON file like this

{
  "message": "Hello"
}

And I change it to something like this

{
  "messsssage": "Hello"
}

The following code would break, but it wouldn't be tested by jest --findRelatedTests

import data from './data.json'

export default () => data.message.length

Is there an option to configure Jest to watch out for changed JSON files also?

Edit: It seems to be a bug in the Jest package, you can find the issue here: https://github.com/facebook/jest/issues/7499

Upvotes: 1

Views: 240

Answers (2)

Clare Macrae
Clare Macrae

Reputation: 3799

I tried "jest-json", and it didn't make jest --findRelatedTests actually detect any dependencies on imported JSON files.

So I resorted to making my pre-commit hooks run all the tests if any of the input JSON files have changed, on the grounds that safety is better than speed... (and committing with failing tests slows us down more, dealing with problems later on...)

lefthook.yml before:

pre-commit:
  parallel: true
  commands:
    test:
      glob: '*.ts'
      run: yarn test "{staged_files}" --findRelatedTests --passWithNoTests

lefthook.yml after:

pre-commit:
  parallel: true
    test-ts:
      glob: '*.ts'
      run: yarn test "{staged_files}" --findRelatedTests --passWithNoTests
    test-json:
      glob: 'tests/Obsidian/__test_data__/*.json'
      run: yarn test

Because we don't change the json files nearly as often as the typescript files, the extra tests run are not onerous...

Upvotes: 0

Narendra
Narendra

Reputation: 4574

You need to add jest-json to your jest config.

{
  "setupTestFrameworkScriptFile": "jest-json"
}

Or if you're already using another test framework, create a setup file and require each of them:

require("jest-json");

For further information, you can refer here

Upvotes: 0

Related Questions