question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Script in index.ts runs too late

See original GitHub issue

Context:

System:

  • OS: macOS 12.6
  • Memory: 390.20 MB / 16.00 GB

Binaries:

  • Node: 18.9.0 - ~/.nvm/versions/node/v18.9.0/bin/node
  • npm: 8.19.1 - ~/.nvm/versions/node/v18.9.0/bin/npm

Languages:

  • Bash: 3.2.57 - /bin/bash

Code Snippet

<!-- /playwright/index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Testing Page</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/playwright/index.ts"></script>
  </body>
</html>
/** /playwright/index.ts **/
// Import styles, initialize component theme here.
(self as Mutable<typeof self>)._clientConfig = {
  debugEnabled: true,
}

export {};
/** The tested component module */

/**
 * This could be for example Redux store init. It's common to init the store with extra middleware for debugging.
 * This line of code runs before the script from /playwright/index.ts, so it fails on _clientConfig to be undefined.
 */
const store = self._clientConfig.debugEnabled
    ? () => ({
      /** todo add debug middleware... */
      value: 'value',
    })
    : () => ({
      value: 'value',
    });

export const Component = () => {
  return (
    <>
      <div>{store().value}</div>
    </>
  );
}

Runtime output The error in the browser console during the test: image

Output bundle Line 568 is the code from the tested component reading the self._clientConfig. Line 581 is the definition of self._clientConfig: image

Describe the bug

The content of index.ts is merged with the components js bundle, but instead of at the beginning of the file, it appears at the end it appears after the tested components code. This behavior means we cannot set domain-specific globals for the browser and the tested component fails on self._clientConfig to be undefined if read by zero indent code.

Based on the docs https://playwright.dev/docs/test-components#playwrightindexts, I guess it should run before the component definition code.

Steps to reproduce

  1. Clone repo https://github.com/RichardJuchelka/Playwright-script_in_index_ts_runs_too_late
  2. npm run install-project
  3. npm run playwright-debug
  4. Open the browser devtools, and see the error in the console tab.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pavelfeldmancommented, Sep 29, 2022

So the issue is that we generate a following snippet:

//  - Inlined index.ts -
...
//  - list of imports
import Component from './Component.tsx'

And esbuild is moving the imports up, because they need to be leading. As a result, component’s inline code runs before the index.ts.

0reactions
chekrdcommented, Oct 7, 2022

Yes, that’s a workaround. Another one is to move the config setup code into its own module and reference it before index.ts script:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Testing Page</title>
    <script type="module" src="/playwright/setupClientConfig.ts"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/playwright/index.ts"></script>
  </body>
</html>

Read more comments on GitHub >

github_iconTop Results From Across the Web

ts-node fails when tsc runs ok · Issue #391 - GitHub
I have an app with ts-node and ts-babel-node configured (using the register require). When I try to start the app, I get the...
Read more >
Debug Failure. False expression: Non-string value passed to ...
Running npm install -g ts-node@latest fixed it for me. I needed to update my ... I lowered my typescript to 4.6.4, and the...
Read more >
Documentation - Migrating from JavaScript - TypeScript
During our JS to TS migration, we'll need to separate our input files to prevent TypeScript from overwriting them. If your output files...
Read more >
ts-loader - npm
The simple solution is to disable it by using the transpileOnly: true option, but doing so leaves you without type checking and will...
Read more >
TypeScript — a beginner's primer. This blog ... - Rick Glascock
I've outlined the basics of how to use TypeScript with out spending much time on the core idea — types. TS offers a...
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