找不到来源:`uncaught syntaxerror`(只发生在赛普拉斯)

Can't find source of: `uncaught syntaxerror` (only occurs in cypress)

寻找有关如何查明 invalid/unexpected 令牌实际来源的建议。

我正在 运行使用 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

所以让我们说清楚;我明白这是我的应用程序代码的问题,而不是我的测试代码。我的问题是,我还没有看到任何指向 语法错误 实际位置 的内容。此外,我 运行 chrome 72 中的应用程序(不是通过 cypress)并且我没有问题。当我通过 cypress 运行 应用程序时似乎只有一个问题( 也使用 chrome 72 因为我使用 --browser chrome 当我 运行 柏树规格)。

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

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

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 调试器中启用了 "Pause on Exceptions"。在我逐步完成 before() 中的所有内容并且必须 "Resume script execution" 之前,没有任何错误。 请注意,我的测试中没有 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");
    })
})

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

我精简了我们的 npm 包,怀疑添加的包可能引入了 SyntaxError。 我删除了大约 1/4 的 npm 包(反正早就该删除了),并清理了我们的依赖项。毕竟,Uncaught SyntaxError 消失了。


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

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

@types/plotly.js
aurelia-animator-css
@types/jest
@types/node
@types/pikaday
ajv
d3
jest
jest-cli
jquery-sparkline
nps-utils
opn
protractor
ts-jest
ts-node
uglify-js
vinyl-fs
wait-on

如果以后有人遇到这个问题,我的就会尝试加载一个不存在的 JS 文件(从我的索引页面)。

Cypress 似乎隐藏了错误,因此记录它很有帮助;

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