I am trying to run my Jest unit tests in Team City but I always end up getting the prompt as shown below.
No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
I tried running yarn test a to run all the tests. But once the tests have completed execution, I'm still getting the same prompt. I tried yarn test a q but that doesn't work. I also tried yarn test a --forceExit and yarn test a --bail but nothing happens, I still get the prompt. How can I run all my Jest tests without getting this prompt as there will be no interaction when running through Team City? Any help would be much appreciated.
--ci
When this option is provided, Jest will assume it is running in a CI environment. This changes the behavior when a new snapshot is encountered. Instead of the regular behavior of storing a new snapshot automatically, it will fail the test and require Jest to be run with --updateSnapshot. link
Also, you can change package.json to:
"test": "CI=true react-scripts test --env=jsdom",
which works great.
Your other option is to set CI in the command like any variable:
CI=true yarn test
In TeamCity, edit the setting for your configuration, then select Parameters on the side.
Click Add a new Parameter, and in the dialog popup that appears, under Kind: select Environment variable (env.).
Set the name to env.CI and set the value to true. Click Save.
Next time you run your build, your build should auto-run the tests and move on.
For bonus points (and if you are the administrator) go to Administration then under Projects edit the <Root project>. Click Parameters on the side and set the env.CI parameter to true so you don't have to set this for future projects.
yarn test --coverage
Will run only once (with coverage) and returns 0 on success and 1 on failure.
The following command worked for me.
CI=true yarn test
Related
I seem to get different results in the summary chart when I run
npm test -- --coverage
versus when I run
npm test -- --coverage --watch
The watched version only shows 1 test total (suites related to changed files). However it also shows all the files in the summary chart (with worse stats)
I'm wondering if the chart is cached or something? I'm not sure why there is a difference.
After you run npm test -- --coverage --watch press a to run all tests.
I am trying to run my Jest tests on Appveyor. I am using the asp.net core template for a react app (which uses create-react-app). When I run the tests locally (with npm run test) the tests run fine, I see PASS src\App.test.js with a run-down of the tests that have passed.
In Appveyor, however, an error is thrown from the console, causing my build to fail. The error message is PASS src\App.test.js. So it would seem an error is being emitted from the run somehow, despite the fact it seems to actually be passing. I checked and $LASTEXITCODE is -1
App.Test.js
it('Runs tests', () => {
expect(true).toBeTruthy();
});
Appveyor.yml
test_script:
- ps: .\build\appveyor\js-tests.ps1
.\build\appveyor\js-tests.ps1
($env:CI = $true) -and (npm run test)
I'm setting the CI environment as this prevents the tests from using "watch". the js-tests script does some other stuff with sorting file locations and a bit of logging etc, but I don't think that is relevant giving the message comes back as a passed test.
Npm sometimes writes normal output to stdErr, which custom PowerShell host on AppVeyor treats as error. Solution is to run those tests in CMD. So wrap them int .cmd file and run with - cmd: prefix.
An alternative to #ilyaf 's answer was to use jest-silent-reporter, which does not give an output when tests pass. This works for the same reason ilyaf's answer does, because npm writes output to stderr, not writing anything solves it.
to install:
npm install jest-silent-reporter --save-dev
in your package.json
"scripts": {
"test": "react-scripts test --env=jsdom --reporters jest-silent-reporter",
},
note i define the reporter here.
I'd also recommend adding a log after calling npm test so your logs have some confirmation that the thing has run.
Jenkins/jest and CI ,
I have created a react APP using create-react-app and I use JEST for testing and I did some new changes in a file created app.test.js and committed to git-hub- hooked it with jenkins -when I run npm test in the local machine the tests are run fine and they all pass ..
BUT when i run the jenkins pipeline script it says the following :
No tests found related to files changed since last commit.
Press a to run all tests, or run Jest with --watchAll.
Watch Usage
› Press a to run all tests.
› Press p to filter by a filename regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
I have tried changing app.test.js -created a new file commit changes and then created a new pipeline in jenkins and tried again ( I have also tried these discussed here :https://github.com/facebookincubator/create-react-app/issues/930) but I still get the above error : my pipeline script is shown below:
node{
stage "CI"
git 'https://github.com/NaveenDK/mentalshortcuts.git'
bat "npm install"
stage " Unit testing"
bat "npm test"
}
def notify(status){
emailext (
to: "dd#dd.com",
subject: "${status}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>${status}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at <a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p>""",
)
}
node {
notify("Deploy to staging?")
}
input 'Deploy to Staging?'
node {
bat "npm run-script build"
}
Any help any links anything at all would be great!
The tests did not run on jenkins as Jenkins was running on Windows, and Windows doesn't support glob, which is src/**/*.js.
this problem was not encountered when the project was build on Jenkins that was installed on a Linux OS!
I also faced the same issue. As mentioned above by Naveen, Windows doesn't support glob, which is src/**/*.js.
That's why in Jenkins configuration, change the label to Linux. Check here to see a preview.
Jenkins configuration
And then in the build section, add the following line as shell commands:
npm install
npm run build
CI=true npm test
Always run npm install first. It will install all the packages.
Then run npm run build to build the app
In the end, make sure to write CI=true npm test. It will run test cases only once. If you write npm test instead, then it will run the test and go into watch mode forever.
I hope this helps.
I encounter this problem on my local machine like this,
"test": "jest --watchAll"
I created a boilerplate React project, packages.json has the usual suspects:
prestart
start
list
test
etc.
I am using Selenium for my E2E framework. I have the following test:
it('should launch a browser', () => {
const By = webDriver.By;
let driver = new webDriver.Builder()
.forBrowser('chrome')
.build();
// verify Continue button exist on page
driver.navigate().to('http://localhost:3000').then(() => driver.findElement(By.id('submitButton')).getAttribute('value'))
.then(buttonValue => expect(buttonValue).toEqual('Continue'));
});
If I do npm start, my site launches and my E2E launches an additional Chrome browser and navigate to my running site: localhost:3000. The test succeeds.
My question is, how do I run my E2E separately, without the need to my site side by side using npm start.
I am newbie to React and Selenium, in case I am missing a lot of information on this post, I apologize in advance.
Well, since you didn't find the time to update the question information with the NPM "scripts" object, then I'll try to give it a shot in the dark.
First of all, due to your wording, I can interpret your question two ways:
a.) you want to run your E2E tests separately, w/o your server running (which is started via npm start);
b.) you want to run your E2E tests via npm start, without triggering your server from starting;
a.) If you want to run your scripts separately, seeing as you are using Mocha, then you can trigger them via: ./node_modules/.bin/mocha <pathToTests>/<testFile>.
Now, since you stated in your question that you're using npm test script, then that should be the best switch to bind your E2E tests execution to:
package.json (Scripts object):
"scripts": {
"test": "mocha --reporter spec <pathToTests>/<testFile>",
"start": "node <yourServerName>.js"
},
Please note that mocha <pathToTests>/<testFile> is equivalent to ./node_modules/.bin/mocha <pathToTests>/<testFile>, because NPM looks for binaries inside node_modules/.bin and when Mocha was installed, it installed it into this directory.
Note: Many packages have a bin, or .bin section, declaring scripts that can be called from NPM similar to Mocha. If you want to find out what other binaries you can run that way, just issue a ls node_modules/.bin.
b.) In this care, I think your issue might be due to NPM defaulting some script values based on package contents. Specifically, if you have a server.js file in the root of your package, then npm will default the start command to server.js.
So if you're starting your E2E tests via npm start, having this ("start": "mocha <pathToTests>/<testFile>") in your package.json and there is a server.js file in the root of your package, then npm will default the start command to node server.js.
In which case, you could either move your server script to another place in the project, or change the switch you're using to trigger the E2E tests (see section b.)).
Hope this solves your problem and if not, looking forward for that package.json "scripts" object so we can really see what's up. :)
Cheers!
Update: my use case is mainly to run tests at CI, but overriding default CRA Jest parameters is something I'm generally wondering about.
I'm running tests using the Jest, config that came with Create React App. It always launches into the interactive mode:
› Press a to run all tests.
› Press o to only run tests related to changed files.
› Press p to filter by a filename regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
But I don't want it to wait for my input. I want it to run once and then terminate. I tried using the --bail or --no-watchman switches but it still launches in interactive mode.
If I globally install jest, and run it in the root of my project, it executes once and finish (just as I want). But when I run npm test which runs react-scripts test, it goes into the watch mode even when I'm not passing --watch.
Update: I've also filed an issue on CRA.
You should use Jests --watchAll=false flag.
eg:
npm test -- --watchAll=false
Note: this is for react-scripts > 3.00
For older versions:
react-scripts >= 2.1.4 < 3.00
For non-ci, eg running tests locally, you can pass a --no-watch flag:
npm test --no-watch
react-scripts <= 2.1.3
CRA looks for a CI environment variable, if its present it doesn't run in watch mode.
CI=true npm test should do what you are looking for
See the User Guide -> Running Tests -> On your own environment
In your package.json scripts:
"test": "react-scripts test --watchAll=false"
Or npm test -- --watchAll=false
Or yarn test --watchAll=false
Note: the flag used to be called --no-watch in react-scripts < 3.0: https://github.com/facebook/create-react-app/blob/3.x/CHANGELOG.md#remove---no-watch-flag
non-interactive solution:
npm test a --watchAll=false
or
yarn test a --watchAll=false