Cypress 和 Typescript - 你的配置文件无效:config.ts

red*_* 87 8 javascript node.js typescript cypress

我正在尝试在 Cypress 中设置自定义获取,并且每当我将 Typescript 文件导入到我的cypress.config.ts. 这是我的代码:

赛普拉斯.config.ts

import 'dotenv-defaults/config';
import { defineConfig } from 'cypress';
import { MongoMemoryServer } from 'mongodb-memory-server';
import db from './src/server/db/datasource';

const createDb = async () => {
  const server = await MongoMemoryServer.create();
  try {
    db.setOptions({ url: server.getUri() });
    await db.initialize();
  } catch (err) {
    throw err;
  }
  return db;
};

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('before:browser:launch', (browser, launchOptions) => {
        if (browser.name === 'chrome') {
          launchOptions.args.push('--proxy-bypass-list=<-loopback>');
          launchOptions.args.push('--disable-dev-shm-usage');
          launchOptions.args.push('--disable-gpu');
          launchOptions.args.push('--no-sandbox');

          return launchOptions;
        }
      });

      on('task', {
        log(message) {
          console.log(message);
          return null;
        },
        /*  createDb() {
          return createDb();
        }, */
      });

      return config;
    },
    baseUrl: `http://localhost:${process.env.PORT || 3000}`,
    video: false,
    screenshotOnRunFailure: false,
    modifyObstructiveCode: false, // Cypress utterly ruins Remix Context if this isn't disabled
    chromeWebSecurity: false,
    experimentalSessionAndOrigin: true,
    experimentalSourceRewriting: true,
  },
  env: process.env,
});
Run Code Online (Sandbox Code Playgroud)

上述文件中的第四行import db from './src/server/db/datasource';破坏了配置并导致此错误:

您的配置文件无效:/home/project/cypress.config.ts TypeError [ERR_UNKNOWN_FILE_EXTENSION]:/home/project/cypress.config.ts 的未知文件扩展名“.ts”

我在 Google 上搜索了该错误,它似乎是 Cypress 的已知错误,我已尝试了所有建议,包括将 Cypress 升级到最新版本 ( ^10.11.0),但没有任何帮助。

我尝试过使用 tsconfig.json 进行规划,但未能有所作为。这是该文件:

{
  "include": ["./*", "src", "test", "types", "scripts"],
  "exclude": ["cypress" /* "./cypress.config.ts" */],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "esnext",
    "jsx": "react-jsx",
    "moduleResolution": "node",
    "target": "es2019",
    "baseUrl": ".",
    "rootDir": ".",
    "isolatedModules": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "strict": true,
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "noEmit": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictPropertyInitialization": false,
    "paths": {
      "~/*": ["./src/*"],
      "@test": ["./test"],
      "@test/*": ["./test/*"]
    }
  },
  "ts-node": {
    "compilerOptions": {
      "module": "commonjs"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我运行来启动 cypress 的命令只是npx cypress open. 希望得到一些帮助,因为目前尚不清楚是什么原因导致了这个问题!

小智 3

用下面的方法修复了

\n
    \n
  1. 删除node_module文件夹
  2. \n
  3. 删除package-lock.json\xc2\xb7
  4. \n
  5. 运行 npm cache clean --force 命令两次
  6. \n
  7. 运行 npm i\xc2\xb7
  8. \n
\n