[BUG] Unable to resolve TsConfig Aliases in Playwright Component Testing for React

See original GitHub issue

Currently we are unable to resolve our tsconfig.json Alias paths that we set.

The error we receive is:

Screenshot 2022-12-13 at 2 57 54 PM

Context:

  • Playwright Version: v.1.28.1
  • Operating System: Mac Ventura 13.0.1
  • Node.js version: 16.17.1
  • Browser: All

Extra

We are using both e2e Playwright tests, and component Testing. Here are the playwright packages being used:

    "@playwright/experimental-ct-react": "^1.28.1",
    "@playwright/test": "^1.28.1",
    "playwright": "^1.28.1",

Code Snippet Component to be tested (it’s a basic sample one)

// src/renderer/components/sample.tsx
import React from "react";

const MyComponent: React.FC = () => {
    return <h1>Hello World</h1>;
};

export default MyComponent;

This works:

import MyComponent from "../../../src/renderer/components/sample";
test("should work", async ({ mount }) => {
    const component = await mount(<MyComponent />);
    await expect(component).toContainText("Greetings");
});

This Does Not work:

import MyComponent from "~Components/sample";

test("should work", async ({ mount }) => {
    const component = await mount(<MyComponent />);
    await expect(component).toContainText("Greetings");
});

Here is the tsconfig.json paths definition

    "paths": {                                    
      "~Components/*": ["src/renderer/components/*"],
    },

Describe the bug

As Stated above, it seems to be an issue with resolving tsconfig.json specifically in the Experimental Testing Components library. Our aliases are resolving fine in our e2e testing suite using playwright.

We have 2 playwright configs:

  • 1 for e2e
  • 1 for react component testing. The generated one through yarn create playwright --ct is what we use, besides changes to the testDir
const config: PlaywrightTestConfig = {
    testDir: "./__tests__/renderer",
   // ...
}

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bdlb77commented, Dec 14, 2022

^^ Resolved : I’ve been hasty 😃

@sand4rt Thank you for your assistance! your response here was exactly what I needed: https://github.com/microsoft/playwright/issues/17846#issuecomment-1269618105



const config: PlaywrightTestConfig = {
  use: {
    ctViteConfig: {
       resolve: {
        alias: {
          "~Components": resolve(__dirname, './src/renderer/components'),
        },
      },
    },
  },
};
2reactions
dgozmancommented, Dec 14, 2022

@bdlb77 This is because component testing involves actually building components for the web page, which is done by Vite. Thus, you have to separately configure how your web page is built, including aliases. For the tests themselves, which run in Node.js, Playwright follows tsconfig.json path mapping for this. You can read more about component testing internals here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot find module that is defined in tsconfig `paths`
I put it both in webpack resolve alias and the tsconfig paths and it worked that way :) module.exports = { //... resolve:...
Read more >
Path aliases with TypeScript in Node.js - Level Up Coding
First of all, we have to declare the path aliases in our tsconfig.json file: ... That's because JS can't resolve the modules for...
Read more >
cannot find module 'vuex' or its corresponding type ...
I am using Webstorm 2022.3, and reopening the IDE does the trick. This might be an IDE issue that fails to reload node_module...
Read more >
Configuring Jest
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have...
Read more >
TypeScript errors and how to fix them
error TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier. Broken Code ❌. index.d ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found