Not a big problem per se but I'm curious as to what's causing this behavior. I'm writing some very basic code to learn how to do some testing. I'm using jestjs for testing a node/express application, and am presently testing the development version of my project locally. All versions are up to date (most current available).
In the configuration for jest I have the following setup:
...
"test": "./node_modules/.bin/env-cmd -f ./config/test.env jest --watch"
},
"jest": {
"testEnvironment": "node",
"verbose": true
}
And my environment configuration (as referenced above by the env-cmd:
PORT=3000
SENDGRID_API_KEY=<API KEY>
JWT_SECRET=<JWT SECRET>
MONGODB_URL=mongodb://127.0.0.1:27017/task-manager-api-test
The --watch flag is supposed to work sort of like nodemon - whenever I save my test file it re-runs the tests. The problem seems to be that whenever I save the file some of the tests fail (it's fairly inconsistent as to which tests fail) - but if I manually re-run the tests (--watch gives me a CLI that allows me to re-run tests with a keypress) the tests pass.
I'm using the following in my test file to make sure that the DB instance has no data in it before running the tests:
// User to seed DB
const testUserUID = new mongoose.Types.ObjectId()
const testUser = {
_id: testUserUID,
name: 'firstName lastName',
email: 'automatedTest#test.com',
password: 'test1234',
tokens: [{
token: jwt.sign({ _id: testUserUID }, process.env.JWT_SECRET)
}]
}
// Setup
beforeEach(async () => {
await User.deleteMany()
await new User(testUser).save()
})
An Example of one of my tests:
test('Should signup a user', async () => {
await request(app)
.post('/users')
.send({
name: 'hardcodeFirst hardcodeLast',
email: 'hardcodeTest#test.com',
password: 'test1234'
})
.expect(201)
})
One of the more common errors I am getting is a MongoError:
MongoError: E11000 duplicate key error collection: task-manager-api-test.users index: email_1 dup key: { : "automatedtest#test.com" }
The other errors that are being thrown are related to the tests failing - so I'm getting values that the test does not expect.
I've tried googling some stuff related to testing async with jest but I haven't found anything that isn't shown in the documentation about how to use promises or async/await with jest. I've verified that my environment variables aren't pointing at my remote DB instance. I've run the tests in my normal (non-vscode) terminal. I've also verified that the tests always pass when using the --watch CLI (pressing Enter or a repeatedly) - the tests are only failing when I save the test file and it automatically re-runs due to the --watch flag.
Talking to one of my developer buddies it was suggested that I've possibly somehow created some sort of race condition. That would be a new situation for me if that's the case!
Thanks in advance for taking a look/any help offered!
EDIT: Included .env for my test environment
--watch flag works only for github repos. u should add watchAll
"test": "env-cmd -f ./config/test.env jest --watch"
rest of your code looks fine.
I want to toggle only running one test, so I don't have to wait for my other tests to see the result of one test.
Currently, I comment out my other tests, but this is really annoying.
Is there a way to toggle only running one test in Cypress?
to run only one file
cypress run --spec path/to/file.spec.js
or using glob patterns:
cypress run --spec 'path/to/files/*.spec.js'
Note: you need to wrap your glob patterns in single quotes to avoid shell expansion!
to run only one test in a file
You can use a .only as described in the Cypress docs
it.only('only run this one', () => {
// similarly use it.skip(...) to skip a test
})
it('not this one', () => {
})
Also, you can do the same with describe and context blocks
edit:
there's also a nice VSCode extension to make adding/removing .only's easier with keyboard shortcuts. It's called Test Utils (install with ext install chrisbreiding.test-utils). It works with js, coffee, and typescript:
There are multiple ways of achieving this.
You can add .onlyto it or describe see #bkucera answer
You can do it from the terminal as explained in the doc here
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
You can mute not needed test suites and particular cases by prepending x to testrunner methods call (describe, it, etc.)
So it would look like:
// this whole testsuite will be muted
xdescribe('Visit google', () => {
it('should visit google', () => { cy.visit('https://google.com/'); });
});
// this testsuite will run
describe('Visit youtube', () => {
it('should visit youtube', () => { cy.visit('https://youtube.com/'); });
// this testcase will be muted
xit('is not necessary', () => { ... });
});
You can run the test like this.
cypress run --spec **/file.js
The best way to do such kind runs are by using the .only keyword that cypress provide.
To run all the test cases in one describe function from many describe functions add the .only in the required describe.
describe("1st describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe.only("2nd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe("3rd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
So here only the 2nd describe will run.
Similarly if you want to run some test cases in 1 describe add the .only in front of all the test cases that you want to run.
describe("describe statement", () => {
it("Should check xx", async function(){
});
it.only("Should check yy", async function(){
});
it.only("Should check zz", async function(){
});
});
So here the it for yy and zz will run
This is similar to the fit and fdescribe in karma and jasmine that you might be familiar with.
You can skip the test in cypress with it.skip or xit
There is one way I have found to skip tests which I don't need to run (in the current test), and that is to use: this.skip();
it('test page', function () {
// skip this test for now
this.skip();
cy.visit('http://example.com/')
cy.contains('test page').click()
cy.url()
.should('include', '/test-page/')
})
1. it is important to use regular function as second argument of it, this will not be available in arrow function
2. Whole of the test will be skipped no matter where we write this.skip()
My test files have a structure like this path/something.test.jsx and commands npx cypress run --spec path/something.test.jsx gives the following exception in the terminal:
Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...
Surprisingly enough the following works and run the test exactly for one file (providing you have jest installed):
jest path/something.test.jsx
A very easy solution is to prefix your tests in with numbers, as testing frameworks will typically will run tests in alpha/numeric order by default - so if I have to check one spec file - I will copy the contents into a file 0-[file-name].spec and re-run the test command. Once the test completes - you terminate the test run - as you will have the results you were looking for. This answer is targeted at projects where your testing framework is abstracted and as a developer, you do not have all available options for your testing framework. Not the best answer, but it works and is intuitive and super easy to do. I have found this to be a way to avoid adding a bunch of conditional skips() or only() calls that will not make it to production, will have to be removed and you can easily add the file pattern to .gitignore file so these local files do not get checked in.
The best-known solution for that already exists and requires adding just one simple argument in the console.
https://github.com/cypress-io/cypress/tree/develop/npm/grep
Simply run:
npx cypress run --env grep="TestName" --spec "filename"
Cypress .only() function is used only for development.
put .only for the test you want to execute and then run the spec as npx cypress run --spec path/to/your-file.spec.js
To run a specific file through Terminal:
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
You can use this
cypress run -- --spec 'path/to/files/*.spec.js'
or
npm run --spec 'path/to/files/*.spec.js'
It worked for me.
Many thanks
use the #focus keyword in the test scripts when execute using cypress open
I have a function that must find a file(.zip) and return it, that i can use it as argument for another function. Also I have test in karma/jasmine where i lunch my search function and when I do this its throws an error that 'fs.readFile is not a function'
test code:
const fs = require('fs');
const JSZip = require("jszip");
const searchfile = () => {
fs.readFile('./data/2-11253540.zip', function (err, data) {
if (err) throw err;
JSZip.loadAsync(data).then(function (zip) {
console.log('Process Zip: ', zip);
});
});
};
describe('Process', () => {
const process = require('./process');
searchfile();
it('001', () => expect(process()).toEqual(null));
});
it's looks not very similar to what I described above, but it was a test version to check if it work or not. In my karma config I have browserify to handle require.
So, searchfile function search a file and process function will use this file. When I run this test I have error that fs.readFile is not a function.
However if I put a code of searchfile in process function and run it directly it work fine.
Why it's not work ?
Look at the last couple of lines of green text in the screenshot (why are you posting PICTURES of text?!).
You are running the tests in Chrome.
The fs module is a Node.js feature.
The fs module is not available in Chrome (and it would be silly if a web page could read arbitrary files from the visitor’s file system).
I am trying to run multiple Karma test files in parallel from inside a Node script and get to know which tests are passing or failing. Right now what I have is this:
const exec = require("child_process").exec;
exec("karma start " + filename, (error, stdout, stderr) => {
// handle errors and test results...
});
The code above works well, and I can get the information on tests passed or failed from stdout. However, it requires having installed Karma and all of the associated dependencies (reporters, browser launchers, etc.) globally. I am looking for a solution that doesn't require me to install all dependencies globally.
My first thought was this:
const karma = require("karma");
const server = new karma.Server(config, () => {
// some logic
});
However, when trying this other approach, I have been unable to gather the test results programmatically.
When using new karma.Server(), is there any way in which I could know which tests have passed or failed (and, ideally, a stack trace of the error)? Alternatively, is there any other way in which I can execute my tests and get the desired information programmatically without the need to install dependencies globally?
Actually, changing the exec line to this seems to do the trick:
exec("node node_modules/karma/bin/karma start " + filename, (error, stdout, stderr) => {
It turns out I'd only need to run the locally installed version of Karma instead of the global one. :-)
I am using Jasmine and Karma for writing unit tests and code coverage. I have created the tasks using Gulp and running them through task runner explorer in VS 2015 update 3.
var gulp = require("gulp");
var Server = require('karma').Server;
var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
gulp.task('unit-tests', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js'
}, done).start();
});
gulp.task('code-coverage', function () {
return gulp.src('_reports/coverage-javascript.json')
.pipe(remapIstanbul({
reports: {
'json': '_reports/coverage-typescript.json',
'html': '_reports/html-report'
}
}));
});
I want to read the generated html results file, i.e. from _reports/html-report/index.html file during Gated Builds or Nightly builds. I want to use this code coverage to perform certain actions like stopping the build if code coverage is below 80% or when a test failed.
How can I do that?
I have implemented one solution which fails MSBuild whenever any Unit Test failed. Basically I wrote a Custom Target in my project.csproj file which runs after 'CompileTypeScript' target.
<PropertyGroup>
<CompileDependsOn>
$(CompileDependsOn);
GulpBuild;
</CompileDependsOn>
</PropertyGroup>
<Target Name="GulpBuild" DependsOnTargets="CompileTypeScript">
<Exec Command="./node_modules/.bin/gulp task-name" />
</Target>
This task will run after Visual studio compiles TS to JS. In build server 'Path' variable is not set for 'gulp', that's why passing the command through node_modules .bin folder.