Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS swc conflicts with babel custom configuration #285

Open
alamenai opened this issue Aug 9, 2023 · 0 comments
Open

NextJS swc conflicts with babel custom configuration #285

alamenai opened this issue Aug 9, 2023 · 0 comments

Comments

@alamenai
Copy link

alamenai commented Aug 9, 2023

I started using Cypress for NextJS, I followed their documentation for integrating babel-plugin-istanbul for transpiling during the code coverage. However, I faced this issue:

  • If I use baberc for configuring the plugin the test works well but the build fails because NextJS tells you that you disabled the SWC configuration ( default config instead of babel )

  • if I remove babelrc the build runs succeefully but the tests report the missing of babelrc configuration.

I found the same issue here but without an answer.

I configured my .babelrc for Cypress and Istanbul:

.babelrc

{
  "presets": ["next/babel"],
  "env": {
    "test": {
      "plugins": ["transform-class-properties"]
    }
  }
}

however, when I run npm run build, I got this error due to the custom configuration:


enter image description here

.cypress.config.ts

import { defineConfig } from "cypress";

export default defineConfig({
component: {
  devServer: {
    framework: "next",
    bundler: "webpack",
    webpackConfig: {
      mode: "development",
      devtool: false,
      module: {
        rules: [
          // application and Cypress files are bundled like React components
          // and instrumented using the babel-plugin-istanbul
          // (we will filter the code coverage for non-application files later)
          {
            test: /\.tsx$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader",
              options: {
                presets: ["@babel/preset-env", "@babel/preset-react"],
                plugins: [
                  // we could optionally insert this plugin
                  // only if the code coverage flag is on
                  "istanbul",
                ],
              },
            },
          },
        ],
      },
    },
  },
  setupNodeEvents(on, config) {
    require("@cypress/code-coverage/task")(on, config);

    return config;
  },
},
});

.nycrc


{
"report-dir": "cypress-coverage",
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"extension": [".ts", ".tsx", ".css"],
"exclude": [
  "cypress/**",
  "**/__fixtures__/**",
  "**/__mocks__/**",
  "**/__tests__/**",
  "cypress-coverage/**",
  "instrumented"
],
"reporter": ["text-summary", "json", "html"],
"instrumenter": "nyc",
"sourceMap": false
}

next.config.ts


/** @type {import('next').NextConfig} */
const nextConfig = {
  async rewrites() {
    if (process.env.NODE_ENV === "development") {
      return [
        {
          source: "/components",
          destination: "/page",
        },
      ];
    }

    return [];
  },
};

module.exports = nextConfig;

image

How to configure the plugin in SWC?

Resources:

https://docs.cypress.io/guides/tooling/code-coverage#Using-code-transpilation-pipeline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant