运行Cypress Cucumber测试时遭遇文件解析错误:Unexpected token (29:50) 求助
Hey there, let's break down this issue and fix it up! That "Unexpected token" error might look like it's a problem with your Login.feature file, but the real culprit is a syntax mistake in your step definition JavaScript code—it's causing the Cucumber preprocessor to choke and misreport the error location.
What's the Root Cause?
Looking at your step definition code, the Given('I open the heroku login page', () => {...}) function isn't properly closed. You added a before() hook inside the Given step, but forgot to add }) to close the Given function's body before moving on to the When step. This breaks the entire JS file's structure, leading the preprocessor to throw a syntax error that gets mislabeled as a Feature file parsing issue.
Step-by-Step Fixes
1. Fix the Syntax Error in Step Definitions
Here's the corrected version of your step definition code, with the missing closure and a few other improvements:
const login_url = '/login' import {Given, When, And, Then} from "cypress-cucumber-preprocessor/steps" Given('I open the heroku login page', () => { // Remove the unnecessary before() hook—Given steps are for preconditions directly cy.visit(login_url) cy.url() .should('eq', Cypress.config().baseUrl + login_url) }) // Added this closing }) to fix the syntax issue When('I should see the Login Page title', () => { cy.get('h2') .should('contain', 'Login Page') }) When('I type in', (datatable) => { datatable.hashes().forEach(element=>{ cy.get('#username').type(element.username) cy.get('#password').type(element.password) }) }) And('I click login button', () => { cy.get('.fa').contains('Login').click() // Commented out immediate Logout—this would break your "Secure Area" verification // cy.get('.icon-2x').contains('Logout').click() }) Then('{string} should be shown', (content) => { cy.contains(content, {timeout:10000}).should('be.visible') })
Key changes here:
- Added the missing
})to close the Given function - Removed the
before()hook from the Given step: In Cucumber step definitions, you don't need Cypress'sbefore()—the Given step itself is meant to handle preconditions. If you need code to run before all scenarios, use Cucumber'sBeforehook (import it from the preprocessor package). - Commented out the immediate Logout click: Your scenario is supposed to verify that "Secure Area" appears after login, but clicking Logout right away would make that check fail. Save Logout for a separate scenario if needed.
2. (Optional) Improve Feature File Readability
Your Login.feature is syntactically correct, but you can make it more readable by updating the step text to match a more natural phrasing. For example:
Feature: Login Scenario: Heroku Login Given I open the heroku login page When I should see the Login Page title When I type in the following credentials | username | password | | tomsmith | SuperSecretPassword! | And I click login button Then "Secure Area" should be shown
Just remember to update the corresponding step definition string to match: When('I type in the following credentials', (datatable) => {...})
3. Verify Plugin Configuration
Your plugin/index.js setup is correct—no changes needed there!
Final Check
Once you've applied these fixes, restart your Cypress runner and re-run the test. The "Unexpected token" error should be gone, and your test should execute as expected.
内容的提问来源于stack exchange,提问作者ModaKolla

