Skip to content

[Feature]: Support importing SCSS in ESM #31689

Closed

Description

@leotg130

🚀 Feature Request

There's been a few related issues already raised:

As well as at least one PR:

From one of those issues:

make sure no client files are imported within playwright itself

This would still be our official recommendation. We don't think that e2e tests should import front-end code in Node. Next time component rightfully uses window inline and tests will explode. Mitigating it requires webpack/jsdom-ing the Node which we won't do.

Why shouldn't tests import front-end code? I think there's two different use cases:

  • Automating an external site, here you'd have no access to the front-end code.
  • Testing your own site, here you have access to the front-end code and it makes things quite a bit easier not having to duplicate functionality between the actual front-end code and test code. As an example I can use react-router's route object to get a list of all possible routes, which I can then use in tests.

On the other hand, why drop the style files (css/less/scss) only for non-ESM users? Why not ESM users as well?

I understand that you don't want to "jsdom the Node", but if some use cases (e.g. ignoring style files) can be supported without much effort, then why not? It won't support all possible scenarios, but in practice this style files issue seems to be coming up somewhat frequently.

Btw, is there some reasonable workaround? Or simply duplicate relevant code (or manually manage) all that is needed within the e2e tests?

Example

No response

Motivation

Allowing users to reduce duplication by being able to re-use their own front-end code.

Activity

  1. pavelfeldman commented on Sep 4, 2025

    @pavelfeldman
    Member

    Why was this issue closed?

    Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

    If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

  2. karlhorky commented on Sep 9, 2025

    @karlhorky
    Contributor

    Workaround: Node.js ESM Loaders via Customization Hooks

    Our workaround is to register Node.js ESM loaders (via Customization Hooks APIs) in playwright.config.ts for custom file types imported in our tests - @mdx-js/node-loader for MDX files and @nodejs-loaders/media for image files:

    +import { register } from 'node:module';
     import type { PlaywrightTestConfig } from '@playwright/test';
    
    +register('@nodejs-loaders/media', import.meta.url);
    +register('@mdx-js/node-loader', import.meta.url);
    +
     const config: PlaywrightTestConfig = {
       // ...
     };
     
     export default config;

    Code for copying:

    import { register } from 'node:module';
    
    // Node.js ESM loaders for image files and MDX imported by test files
    // - https://github.com/microsoft/playwright/issues/26822#issuecomment-2692835230
    register('@nodejs-loaders/media', import.meta.url);
    register('@mdx-js/node-loader', import.meta.url);

    Seems to work so far!

    So to import SCSS or other formats in your tests, try to find a Node.js ESM loader (NOT the same as a webpack loader) for your file format, and try using register() from node:module in playwright.config.ts

    Also added to playwright-tricks: https://github.com/karlhorky/playwright-tricks#import-custom-file-types-in-tests

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions