Javascript Can';找不到的来源:`uncaught syntaxerror`(仅出现在cypress中)

Javascript Can';找不到的来源:`uncaught syntaxerror`(仅出现在cypress中),javascript,mocha.js,chai,cypress,Javascript,Mocha.js,Chai,Cypress,寻找有关如何精确定位无效/意外令牌的实际来源的建议 我正在用cypress运行测试,大多数时候(尽管不一致),我从所有测试中都会遇到这个错误 Uncaught SyntaxError: Invalid or unexpected token This error originated from your application code, not from Cypress. When Cypress detects uncaught errors originating from your

寻找有关如何精确定位无效/意外令牌的实际来源的建议

我正在用cypress运行测试,大多数时候(尽管不一致),我从所有测试中都会遇到这个错误

Uncaught SyntaxError: Invalid or unexpected token

This error originated from your application code, not from Cypress.

When Cypress detects uncaught errors originating from your application it will automatically fail the current test.

This behavior is configurable, and you can choose to turn this off by listening to the 'uncaught:exception' event.

https://on.cypress.io/uncaught-exception-from-application
所以,让我们澄清一下;我明白这是我的应用程序代码的问题,而不是我的测试代码的问题。我的问题是,我还没有看到任何东西指向语法错误的实际位置。此外,我在chrome72中运行应用程序(不是通过cypress),我没有问题。只有当我通过cypress运行应用程序时才出现问题(也使用chrome 72,因为我在运行cypress规范时使用的是浏览器chrome)

我已经在测试中添加了
fail
uncaught:exception
处理程序来捕获输出,尽管我仍然找不到任何东西来指示错误的实际来源

通过中断
uncaught:exception
处理程序,传递了两个参数, 1) 误差(同上);2) 摩卡咖啡(我认为)可以运行:

Hook {title: ""before all" hook", fn: ƒ, body: "function () {↵    var _this = this;↵↵    debugger;…   cy.visit("/#/account-management");↵    });↵  }", async: 0, sync: true, …}

    $events: {error: Array(1)}
    async: 0
    body: "function () {↵    var _this = this;↵↵    debugger;↵    cy.on('fail', event_handler_functions_1.failHandler.bind(this));↵    cy.on('uncaught:exception', function (e, r) {↵      console.log(e, r);↵      debugger;↵    });↵    cy.fixture(Cypress.env("environmentSettings")).then(function (fixture) {↵      _this.environmentData = environmentData = fixture;↵      cy.launchApp(environmentData.baseUrl, environmentData.username, environmentData.password↵      /*, 300000*/↵      );↵      cy.visit("/#/account-management");↵    });↵  }"
    callback: ƒ done(err)
    ctx: Context {currentTest: Test, _runnable: Hook, test: Hook, environmentData: {…}}
    fn: ƒ ()
    hookId: "h1"
    hookName: "before all"
    id: "r3"
    parent: Suite {title: "Account Management", ctx: Context, suites: Array(0), tests: Array(3), pending: false, …}
    pending: false
    sync: true
    timedOut: false
    timer: null
    title: ""before all" hook"
    type: "hook"
    _currentRetry: 0
    _enableTimeouts: false
    _retries: -1
    _slow: 75
    _timeout: 4000
    _trace: Error: done() called multiple times at Hook.Runnable (https://localhost:44399/__cypress/runner/cypress_runner.js:30161:17) at new Hook (https://localhost:44399/__cypress/runner/cypress_runner.js:26593:12) at Suite.beforeAll (https://localhost:44399/__cypress/runner/cypress_runner.js:31573:14) at before (https://localhost:44399/__cypress/runner/cypress_runner.js:26770:17) at context.describe.context.context (https://localhost:44399/__cypress/runner/cypress_runner.js:26666:10)
    __proto__: Runnable


在我的测试中,我已经通过了
before()
,在chrome调试器中启用了“暂停异常”。在我完成
before()
中的所有步骤并必须“恢复脚本执行”之前,不会出现任何错误。 注意,我的测试中没有一个
beforeAll()
钩子,只有一个
before()

我最近没有做过使用不寻常语法的更改(据我所知),而且我已经有几个星期没有在本地环境中运行测试套件了,所以有很多更改-太多了,我觉得逐个筛选是值得的


下面是来自该错误实例的测试供参考,尽管它们都有相同的问题

import { failHandler } from "..\\..\\support\\event-handler-functions"
describe('Account Management', function () {
    var environmentData: CypressTypings.EnvrionmentSettings;

    before(function () {
        debugger;
        cy.on('fail', failHandler.bind(this))
        cy.on('uncaught:exception', (e, r) => {console.log(e, r); debugger;})
        cy.fixture(Cypress.env("environmentSettings")).then((fixture) => {
            (<any>this).environmentData = environmentData = fixture

            cy.launchApp(environmentData.baseUrl, environmentData.username, environmentData.password/*, 300000*/);
            cy.visit("/#/account-management");
        });
    })

    beforeEach(() => {
        Cypress.Cookies.preserveOnce(environmentData.cookieName)
    })

    it('Loads Governments', function () {
        cy.get("[data-cy=government-panel]", { timeout: 20000 }).should("have.length.gte", 1);
    })

    it('Users Page Loads', function () {
        cy.get("[data-cy=government-panel]").first().find(".fa-users").click();
        cy.get("tbody", { timeout: 20000 }).find("tr").should("have.have.length.greaterThan", 0);
        cy.get("[data-cy=return-to-organization-button]").click();
        cy.get("[data-cy=government-panel]").should("exist");
    })
    it('Service Area Page Loads', function () {
        cy.get("[data-cy=government-panel]").first().find(".fa-globe").click();
        cy.get("tbody", { timeout: 20000 }).find("tr").should("have.have.length.greaterThan", 0);
        cy.get("[data-cy=return-to-organization-button]").click();
        cy.get("[data-cy=government-panel]").should("exist");
    })
})
从“.\\..\\support\\event handler functions”导入{failHandler}
描述('账户管理',功能(){
var environmentData:CypressTypings.environmentsettings;
前(函数(){
调试器;
cy.on('fail',failHandler.bind(this))
cy.on('uncaught:exception',(e,r)=>{console.log(e,r);debugger;})
cy.fixture(Cypress.env(“环境设置”))。然后((fixture)=>{
(此).environmentData=environmentData=fixture
cy.launchApp(environmentData.baseUrl,environmentData.username,environmentData.password/*,300000*/);
cy.visit(“/#/账户管理”);
});
})
在每个之前(()=>{
Cypress.Cookies.preserveOnce(environmentData.cookieName)
})
它('加载政府',函数(){
get(“[data cy=government panel]”,{timeout:20000}).should(“have.length.gte”,1);
})
它('用户页面加载',函数(){
cy.get(“[data cy=government panel]”)。首先()。查找(“.fa用户”)。单击();
cy.get(“tbody”,{timeout:20000}).find(“tr”).should(“have.have.length.greaterThan”,0);
cy.get(“[data cy=返回组织按钮]”)。单击();
cy.get(“[data cy=government panel]”)应该(“存在”);
})
it('服务区页面加载',功能(){
cy.get(“[data cy=government panel]”)。首先()。查找(“.fa globe”)。单击();
cy.get(“tbody”,{timeout:20000}).find(“tr”).should(“have.have.length.greaterThan”,0);
cy.get(“[data cy=返回组织按钮]”)。单击();
cy.get(“[data cy=government panel]”)应该(“存在”);
})
})

还值得注意的是:
launchApp()
步骤实际上发生了-应用程序已登录,然后似乎在加载应用程序时出现了语法错误,并且
visit()
步骤从未实际执行过。

我减少了npm包的数量,怀疑添加的包可能引入了SyntaxError。 我删除了大约1/4的npm包(无论如何早就应该删除了),并清理了我们的依赖关系。在这一切之后,
Uncaught SyntaxError
就消失了


我怀疑是以下软件包之一引入了错误,尽管我不能肯定地说,因为我从未真正查明错误的来源

我结束删除的软件包(其中一些可能是我们最近更新的):


如果将来有人遇到这种情况,我的问题是试图加载一个不存在的JS文件(从我的索引页)

Cypress似乎隐藏了错误,因此日志记录有帮助

Cypress.on('uncaught:exception', (err, runnable) => {
  console.log(err);
  return false;
})

同样在
runnable.\u trace
下,您可以看到有一个错误:
done()多次调用
-我认为这与此无关,但即使如此,我也无法从中找到任何东西..Cypress版本是3.1.5绝对是一个很好的建议。正如我在问题中所说,我曾尝试添加此内容,但没有从中找到任何其他信息。也就是说,绝对值得一试,我希望这有助于看到这个答案的人解决未来的问题
Cypress.on('uncaught:exception', (err, runnable) => {
  console.log(err);
  return false;
})