Files
cypress/packages/server/test/unit/errors_spec.coffee
Jennifer Shehane 2ba53f6837 Better handle reserved key CYPRESS_ENV being set by users to va… (#6437)
* Add warning when setting CYPRESS_ENV to non-production value

* Add warning and update error when setting CYPRESS_ENV in config to non-production value

* Update config test/to throw

* we want warning, not throw

* Rename env var to CYPRESS_INTERNAL_ENV + fix warning to actually warn when staging

* update cli snapshot to include new 'info' command

* yarn.lock

* removed the warning from config, is overboard on our own tests 😓

* cleanup from review

Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
2020-03-06 16:53:48 +06:30

138 lines
4.2 KiB
CoffeeScript

require("../spec_helper")
style = require("ansi-styles")
chalk = require("chalk")
errors = require("#{root}lib/errors")
logger = require("#{root}lib/logger")
snapshot = require("snap-shot-it")
describe "lib/errors", ->
beforeEach ->
sinon.spy(console, "log")
context ".log", ->
it "uses red by default", ->
err = errors.get("NOT_LOGGED_IN")
ret = errors.log(err)
expect(ret).to.be.undefined
red = style.red
expect(console.log).to.be.calledWithMatch(red.open)
expect(console.log).to.be.calledWithMatch(red.close)
it "can change the color", ->
err = errors.get("NOT_LOGGED_IN")
ret = errors.log(err, "yellow")
expect(ret).to.be.undefined
yellow = style.yellow
expect(console.log).to.be.calledWithMatch(yellow.open)
expect(console.log).to.be.calledWithMatch(yellow.close)
it "logs err.message", ->
err = errors.get("NO_PROJECT_ID", "foo/bar/baz")
ret = errors.log(err)
expect(ret).to.be.undefined
expect(console.log).to.be.calledWithMatch("foo/bar/baz")
it "logs err.details", ->
err = errors.get("PLUGINS_FUNCTION_ERROR", "foo/bar/baz", "details huh")
ret = errors.log(err)
expect(ret).to.be.undefined
expect(console.log).to.be.calledWithMatch("foo/bar/baz")
expect(console.log).to.be.calledWithMatch("\n", "details huh")
it "logs err.stack in development", ->
process.env.CYPRESS_INTERNAL_ENV = "development"
err = new Error("foo")
ret = errors.log(err)
expect(ret).to.eq(err)
expect(console.log).to.be.calledWith(chalk.red(err.stack))
context ".logException", ->
it "calls logger.createException with unknown error", ->
sinon.stub(logger, "createException").resolves()
sinon.stub(process.env, "CYPRESS_INTERNAL_ENV").value("production")
err = new Error("foo")
errors.logException(err)
.then ->
expect(console.log).to.be.calledWith(chalk.red(err.stack))
expect(logger.createException).to.be.calledWith(err)
it "does not call logger.createException when known error", ->
sinon.stub(logger, "createException").resolves()
sinon.stub(process.env, "CYPRESS_INTERNAL_ENV").value("production")
err = errors.get("NOT_LOGGED_IN")
errors.logException(err)
.then ->
expect(console.log).not.to.be.calledWith(err.stack)
expect(logger.createException).not.to.be.called
it "does not call logger.createException when not in production env", ->
sinon.stub(logger, "createException").resolves()
sinon.stub(process.env, "CYPRESS_INTERNAL_ENV").value("development")
err = new Error("foo")
errors.logException(err)
.then ->
expect(console.log).not.to.be.calledWith(err.stack)
expect(logger.createException).not.to.be.called
it "swallows creating exception errors", ->
sinon.stub(logger, "createException").rejects(new Error("foo"))
sinon.stub(process.env, "CYPRESS_INTERNAL_ENV").value("production")
err = errors.get("NOT_LOGGED_IN")
errors.logException(err)
.then (ret) ->
expect(ret).to.be.undefined
context ".clone", ->
it "converts err.message from ansi to html with span classes when html true", ->
err = new Error("foo" + chalk.blue("bar") + chalk.yellow("baz"))
obj = errors.clone(err, {html: true})
expect(obj.message).to.eq('foo<span class="ansi-blue-fg">bar</span><span class="ansi-yellow-fg">baz</span>')
it "does not convert err.message from ansi to html when no html option", ->
err = new Error("foo" + chalk.blue("bar") + chalk.yellow("baz"))
obj = errors.clone(err)
expect(obj.message).to.eq('foo\u001b[34mbar\u001b[39m\u001b[33mbaz\u001b[39m')
context ".displayFlags", ->
it "returns string formatted from selected keys", ->
options = {
tags: "nightly,staging",
name: "my tests",
unused: "some other value"
}
# we are only interested in showig tags and name values
# and prepending them with custom prefixes
mapping = {
tags: "--tag",
name: "--name"
}
text = errors.displayFlags(options, mapping)
snapshot("tags and name only", text)