Do you want to request a _feature_ or report a _bug_?
_bug_
What is the current behavior?
Jest runs tests on branches in git when the branch name ends with ".spec.js" or ".test.js" (e.g. ".git/refs/remotes/origin/example.spec.js").
If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.
What is the expected behavior?
Jest ignores/excludes files under ".git" by default (or perhaps even under any directory that begins with a dot).
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
The Jest philosophy is to work great by default and the question is whether to ignore tests under .git or any . prefixed directory.
I'm not sure though if this is a broad use case.
What do you say @thymikee?
In any case, if we decide to go with one of the solutions:
We can also ignore the .git directory as a whole using testPathIgnorePatterns: ["/node_modules/", ".git"] as default.
By looking into the default regex of testMatch we can see that jest does go into directories that start with a dot ?(*.), so by removing that part from the regex jest won't search for test files in hidden directories.
Honestly that never happened to me. Do you have a repro?
I am not able to reproduce on jest@latest, here's the repo

@mfulton26 if you can't push a repo could you at least post your jest config?
Cannot repro this either
Here is a script to reproduce the issue:
#!/bin/bash -ex
# set-up working directory
rm -rf repro
mkdir repro
cd repro
git init
echo "node_modules/" >> .gitignore
npm init --yes
# assume jest is installed globally (uncomment if not)
#npm install --save jest
# create a test
echo "test("something", function() {}" > some.spec.js
# add the single test to the version control system
git add .
git commit -m "Silly commit"
# checkout a different branch
git checkout -b repro.spec.js
# run the test(s) -- 1 test is expected but 3 will be executed
jest
Running the bash script will result in output similar to the following:
+ rm -rf repro
+ mkdir repro
+ cd repro
+ git init
Initialized empty Git repository in ~/repro/.git/
+ echo node_modules/
+ npm init --yes
Wrote to ~/repro/package.json:
{
"name": "repro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
+ echo 'test(something, function() {}'
+ git add .
+ git commit -m 'Silly commit'
[master (root-commit) 18bc607] Silly commit
3 files changed, 14 insertions(+)
create mode 100644 .gitignore
create mode 100644 package.json
create mode 100644 some.spec.js
+ git checkout -b repro.spec.js
Switched to a new branch 'repro.spec.js'
+ jest
FAIL ./some.spec.js
● Test suite failed to run
~/repro/some.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){test(something, function() {}
^
SyntaxError: missing ) after argument list
at ScriptTransformer._transformAndBuildScript (../../../../../usr/lib/node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:318:17)
FAIL .git/logs/refs/heads/repro.spec.js
● Test suite failed to run
~/repro/.git/logs/refs/heads/repro.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){0000000000000000000000000000000000000000 18bc607a353633b2c31527bd66dd25510b4aac28 Mark Fulton <[email protected]> 1519054507 -0600 branch: Created from HEAD
^^
SyntaxError: Unexpected number
at ScriptTransformer._transformAndBuildScript (../../../../../usr/lib/node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:318:17)
FAIL .git/refs/heads/repro.spec.js
● Test suite failed to run
~/repro/.git/refs/heads/repro.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){18bc607a353633b2c31527bd66dd25510b4aac28
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (../../../../../usr/lib/node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:318:17)
Test Suites: 3 failed, 3 total
Tests: 0 total
Snapshots: 0 total
Time: 1.797s
Ran all test suites.
Still cannot repro. Here is my output:
Projects ❯ #!/bin/bash -ex
# set-up working directory
rm -rf repro
mkdir repro
cd repro
git init
echo "node_modules/" >> .gitignore
npm init --yes
# assume jest is installed globally (uncomment if not)
#npm install --save jest
# create a test
echo "test("something", function() {}" > some.spec.js
# add the single test to the version control system
git add .
git commit -m "Silly commit"
# checkout a different branch
git checkout -b repro.spec.js
# run the test(s) -- 1 test is expected but 3 will be executed
jest
Initialized empty Git repository in /Users/satya/Workspace/Projects/repro/.git/
Wrote to /Users/satya/Workspace/Projects/repro/package.json:
{
"name": "repro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}
[master (root-commit) 4d74016] Silly commit
3 files changed, 14 insertions(+)
create mode 100644 .gitignore
create mode 100644 package.json
create mode 100644 some.spec.js
Switched to a new branch 'repro.spec.js'
FAIL ./some.spec.js
● Test suite failed to run
/Users/satya/Workspace/Projects/repro/some.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){test(something, function() {}
^
SyntaxError: missing ) after argument list
at ScriptTransformer._transformAndBuildScript (../../../../../usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:318:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.715s
Ran all test suites.
What's your jest version?
@mfulton26 those are the steps I ran in the above repo. Can you pull that repo down and run the tests?
My jest version is 22.3.0.
If I pull down your repo and do not checkout foo.spec.js then I don't see the issue but once I checkout foo.spec.js branch locally I then do see the issue. I'm on Windows and I've tried this with both Git Bash and with Bash on Ubuntu on Windows.
I'm on Windows
Maybe that's the culprit? I'm on a Mac.
Anyone with Windows can reproduce this?
I suspect someone on Ubuntu would see this as well as I see it on Bash on Ubuntu on Windows...
I just ran into this issue and noticed something silly ... I accidentally created a branch name that contains a file path, in my case it was src/reducers/product.test.js then Jest was also trying to run the test under .git/refs/heads/src/reducers/product.test.js.
Maybe the Switched to a new branch 'repro.spec.js' in the post of @satya164 above suggests a similar situation.
can you setup a repository on github we can pull down, along with the steps required to see the error?
Same here!
Error: Cannot parse /{my-path}/.git/logs/refs/remotes/upstream/package.json as JSON: Unexpected number in JSON at position 1
at Object.<anonymous> (/{my-path}/node_modules/jest-haste-map/build/worker.js:56:15)
at Generator.next (<anonymous>)
at step (/{my-path}/node_modules/jest-haste-map/build/worker.js:192:30)
at /{my-path}/node_modules/jest-haste-map/build/worker.js:211:14
at new Promise (<anonymous>)
at Object.<anonymous> (/{my-path}/node_modules/jest-haste-map/build/worker.js:189:12)
at Object.worker (/{my-path}/node_modules/jest-haste-map/build/worker.js:95:17)
at execMethod (/Users/andresmazzo/Code/Sites/bonda-pwa/node_modules/jest-worker/build/child.js:90:29)
at process.on (/{my-path}/node_modules/jest-worker/build/child.js:40:7)
at process.emit (events.js:180:13)
After a looooong research at work i fixed it removing _upstream_ (git remote rm upstream) 🎉.
Really weird, but it checks that jest was running under .git.
Most helpful comment
Honestly that never happened to me. Do you have a repro?