jest updateSnapshot for specific test file
Asked Answered
C

5

32

I'm trying to figure out how o update a single snapshot file. In the docs it says just add the -t and I presume the file name but thats not working for me.

Example, in terminal I use.

jest -u -t test/js/tests/components/myTestApp.test.js

Any ideas why this is not working?. I also added the name as a string.

Cheapjack answered 1/2, 2018 at 21:29 Comment(0)
S
18

According to doc https://facebook.github.io/jest/docs/en/cli.html -t option is searching not for file name, but for specific spec name. They provided an example

jest -u -t="ColorPicker"

where "ColorPicker" is name of the spec, given in describe or test blocks.

Simonesimoneau answered 4/2, 2018 at 15:10 Comment(2)
So, do I NEED to install jest-cli globally? Because it says "The term 'jest' is not recognized as the name of a cmdlet..." ?Carcassonne
@Carcassonne you don't have to install jest globally as you could pre-pend it with npx (i.e. npx jest -u -t="ColorPicker") to call the local instance of the package.Graber
C
22

In order to update particular snapshot, you can pass name as an argument to your script test command.

In package.json

"test": "jest --verbose"

In command line

npm test "<spec name>" -- -u

OR

npm test "<spec name>" -- --updateSnapshot
Cordellcorder answered 11/9, 2019 at 6:31 Comment(1)
thanks! both command line commands worked for me and I didn't have to install jest globally!Unsay
S
18

According to doc https://facebook.github.io/jest/docs/en/cli.html -t option is searching not for file name, but for specific spec name. They provided an example

jest -u -t="ColorPicker"

where "ColorPicker" is name of the spec, given in describe or test blocks.

Simonesimoneau answered 4/2, 2018 at 15:10 Comment(2)
So, do I NEED to install jest-cli globally? Because it says "The term 'jest' is not recognized as the name of a cmdlet..." ?Carcassonne
@Carcassonne you don't have to install jest globally as you could pre-pend it with npx (i.e. npx jest -u -t="ColorPicker") to call the local instance of the package.Graber
F
17

this works

jest /path/to/file --updateSnapshot
Finsen answered 8/9, 2019 at 7:52 Comment(0)
O
6

If you run Jest via npm test, you can use arguments by inserting a -- between npm test and the Jest arguments - path & snapshot flag:

npm test -- path/to/file -u

Opalescent answered 16/2, 2021 at 11:10 Comment(0)
C
0

Late reply, but want to contribute to this topic, in the specific doubt about needing to install jest globaly:

You don't need to install jest globally, and there is an alternative to create a script command in package.json. The option is to execute the command as:

npx jest -u -t="ColorPicker"

Npx let you execute locally installed packages and it's bundled in npm 5.2.0 and above

Cripple answered 30/4 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.