Cypress: Tests are not run when express is required in the test file

Created on 5 Apr 2017  路  6Comments  路  Source: cypress-io/cypress

It seems Cypress stops executing tests as soon as the express package is required within a test file.

Sample test file:

// ./cypress/integration/test_spec.js
var express = require("express");

describe("test", function() {
  it("case", () => {
    throw new Error("should fail");
  });
});

Running this test through cypress run gives this:

If the require call is commented, the test fails as expected.

My environment:

$ node --version
v6.0.0
$ cypress --version
Cypress CLI: 0.13.1

Express package version installed in node_modules: 4.14.1.

Most helpful comment

In case it's helpful to anyone, I've found having this in my package.json works:

"scripts": {
    "test-cypress": "node app.js & pid=$!; cypress run; kill -9 $pid"
}

(using cypress 2.0.4).

All 6 comments

Cypress bundles and serves your test files to the browser via browserify.

You cannot require modules which rely on core node libs and concepts that don't exist in the browser.

If you were to run these tests in a headed environment you would likely see the error in your console.

Cypress is meant to be used on an existing webserver - boot your server yourself prior to running Cypress tests.

Cypress is meant to be used on an existing webserver - boot your server yourself prior to running Cypress tests.

That was actually part of my plan, hence why requiring (smthg requiring) express to start the server right from the test env. Right, I'll probably rely on some exec system call instead (edit: nope my bad, it won't work too).

Though I'm surprised this issue is closed. I feel like tests being silently skipped with no errors shown to the user is very confusing (I spent smthg like 1.5h debugging and tracking this).

We don't recommend starting / shutting down a webserver or any other long lived process from inside Cypress.

Here is a longer explanation as to why: https://gitter.im/cypress-io/cypress?at=58b371831465c46a56ba9609

Regarding no errors shown - I didn't mean to be dismissive. My guess is that if you were running these tests in a headed browser that there would be an error in your console. As to why its not being "caught" we'd have to do some investigating whether its failing on bundling or whether there is an uncaught exception happening prior to test parsing.

My guess is the latter and in that case there's already an open issue for that here: https://github.com/cypress-io/cypress/issues/476

I'm sorry it took a long time to debug. Likely the error was just sitting there in your Dev Tools console :/

We don't recommend starting / shutting down a webserver or any other long lived process from inside Cypress.

Ok, that actually makes sense (I'm the original author of casperjs, we had the very same constraint). Now do you have documentation/recipes on how to deal with servers setup and teardown before running Cypress?

Is there a JavaScript API for running a cypress test suite, so I could write a high-level runner in node? Or should I just go with bash scripts?

We have not been opinionated about the server setup / teardown other than what we have in our public projects for CI scripts.

The reason is that the vast majority of our users only need to programmatically boot webservers in CI. Our users typically iterate in the headed browser all day so there's not really ever a "start" and an "end" event. This is part of what separates Cypress from other browser automation tools. We're like a long running experience which you use to drive, develop, and debug your application code all day.

Running headlessly from start to stop is really only useful in CI, its just not practical to run locally.

Since cypress can be spawned programmatically you could just write a simple wrapper using child_process in node.

We are actually in the middle of rewriting our CLI tool to be a requireable node_module which will have a nice API to do these things for you.

Here's the open issue for that with some example API code.

In case it's helpful to anyone, I've found having this in my package.json works:

"scripts": {
    "test-cypress": "node app.js & pid=$!; cypress run; kill -9 $pid"
}

(using cypress 2.0.4).

Was this page helpful?
0 / 5 - 0 ratings