Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save a2sc/0892269fcd4a3181517e339df63b6a8f to your computer and use it in GitHub Desktop.
Save a2sc/0892269fcd4a3181517e339df63b6a8f to your computer and use it in GitHub Desktop.
Fix errors with require in es6 in cypress.config.js (Using: Laracasts/Cypress 3 + Laravel 11 + Inertia and Jetstream + Vue + Vite)

Lines to modify in the cypress.config.js file

import { defineConfig } from 'cypress'
import index from './tests/cypress/plugins/index.js'

export default defineConfig({

    //...
    
    e2e: {
        setupNodeEvents(on, config) {
            return index(on, config)
        },
        
        // ...
        
    },
})

Change ./tests/cypress/plugins/index.js file

import { activateCypressEnvFile, activateLocalEnvFile} from './swap-env.js'

export default (on, config) => {
    on('task', {activateCypressEnvFile, activateLocalEnvFile});
};

Change ./tests/cypress/plugins/swap-env.js file

import { existsSync, renameSync } from 'fs';

export function activateCypressEnvFile() {
    if (existsSync('.env.cypress')) {
        renameSync('.env', '.env.backup');
        renameSync('.env.cypress', '.env');
    }

    return null;
}
export function activateLocalEnvFile() {
    if (existsSync('.env.backup')) {
        renameSync('.env', '.env.cypress');
        renameSync('.env.backup', '.env');
    }

    return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment