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

In-browser package import for writing unit test #34

Open
doodlewind opened this issue Feb 16, 2023 · 3 comments
Open

In-browser package import for writing unit test #34

doodlewind opened this issue Feb 16, 2023 · 3 comments
Labels
question Further information is requested

Comments

@doodlewind
Copy link

Hi, I'm interested in XBell since I'm working on an editor project (BlockSuite), which requires comprehensive test suite that works across different environments. For this snippet in the documentation:

import { test, expect } from 'xbell';

test('unit testing', async ({ page }) => {
  const result = await page.evalute(async () => {
    const { add } = await import('./add');
    return add(1, 1);
  });

  expect(result).toBe(2);
});

I'm wondering if it's possible to import third-party packages inside the test case? This allows us to write unit tests in this way:

test('unit testing', async ({ page }) => {
  const result = await page.evalute(async () => {
    const { add } = await import('my-package');
    return add(1, 1);
  });

  expect(result).toBe(2);
});

I'm not sure if it's possible to import ESM package inside page.evaluate, this is a blocker in my scenario.

@Testerding
Copy link

Testerding commented Feb 16, 2023 via email

@xlianghang
Copy link
Contributor

Sorry, I just saw this issue.

I'm wondering if it's possible to import third-party packages inside the test case? This allows us to write unit tests in this way:

Yes, XBell allows importing third-party packages.

Thank you very much for your interest in XBell, but XBell may be a little unstable recently.
Because I am refactoring part of its page build to solve the page refresh problem caused by vite.

@xlianghang
Copy link
Contributor

xlianghang commented Feb 23, 2023

test('unit testing', async ({ page }) => {
  const result = await page.evalute(async () => {
    const { add } = await import('my-package');
    return add(1, 1);
  });

  expect(result).toBe(2);
});

I would probably stop supporting this because of the ambiguity, for example, javascript in the page already includes a react, and then the developer also imports a react in a test case. The developer might mistakenly think that react is only imported once, but actually import it twice.

You can see if the way below can support your scenario:

test.browser('test the code in browser', async ({ expect, page }) => {
  const { add } = await import('my-package');
  const result = add(1, 1);
  expect(result).toBe(2);

  window.document.body.innerHTML = result;
  await expect(page).toMatchScreenshot({
    name: 'default-screenshot',
  });
});

@xlianghang xlianghang added the question Further information is requested label Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants