使用cypress-firebase运行cypress测试时出现Webpack错误

5m1hhzi4  于 2023-01-14  发布在  Webpack
关注(0)|答案(1)|浏览(204)

我正在为我的nextjs web应用编写cypress e2 e测试,它在后端使用firebase,我已经按照文档中的指南使用cypress-firebase包(https://www.npmjs.com/package/cypress-firebase)设置了它,但是我得到了一个与webpack相关的错误:

Webpack Compilation Error
./node_modules/cypress-firebase/lib-esm/plugin.js 18:27
Module parse failed: Unexpected token (18:27)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| export default function pluginWithTasks(cypressOnFunc, cypressConfig, adminInstance, overrideConfig) {
|     // Only initialize admin instance if it hasn't already been initialized
>     if (adminInstance.apps?.length === 0) {
|         initializeFirebase(adminInstance, overrideConfig);
|     }

下面是我的cypress/support/commands.js文件的内容(我使用的是firebase版本9.15.0,因此从firebase/compat/* 导入):

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';
import 'firebase/compat/firestore';
import { attachCustomCommands } from 'cypress-firebase';

const fbConfig = {
  apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
  projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
  storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
  messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
  appId: process.env.NEXT_PUBLIC_APP_ID
};

firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase });

下面是我的cypress.config.js文件的内容:

const { defineConfig } = require('cypress');
const cypressFirebasePlugin = require('cypress-firebase').plugin;
const admin = require('firebase-admin');

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:3000',
    chromeWebSecurity: false,
    setupNodeEvents(on, config) {
      return cypressFirebasePlugin(on, config, admin, { projectId: process.env.NEXT_PUBLIC_PROJECT_ID });
    },
  },
});

我的.env文件中有一个CYPRESS_TEST_UID,并且下载了firebase-admin所需的serviceAccount.json文件。我不确定是什么导致了这个错误;任何帮助都将不胜感激。

gab6jxml

gab6jxml1#

我在3.0.1中也遇到了同样的问题;我不确定这是否是一个插件错误。

相关问题