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

Running jest test on Nodebox #38

Open
s4847043 opened this issue May 22, 2023 · 4 comments
Open

Running jest test on Nodebox #38

s4847043 opened this issue May 22, 2023 · 4 comments

Comments

@s4847043
Copy link

s4847043 commented May 22, 2023

I am trying to run jest test but it is not even starting. I am trying the reason behind at least a log why it did not run.

here is my code:

import { Box } from "@chakra-ui/react";
import { useContext, useEffect, useRef } from "react";
import { FileSystemContext } from "@/contexts/FileSystem";

const TestRunner = () => {
  const { visibleFiles } = useContext(FileSystemContext);
  const iFrame = useRef<HTMLIFrameElement>(null);

  const startEmulator = async () => {
    if (iFrame.current) {
      const { Nodebox } = await import("@codesandbox/nodebox");
      const emulator = new Nodebox({
        iframe: iFrame.current,
      });
      await emulator.connect();
      await emulator.fs.init({
        "package.json": JSON.stringify({
          name: "my-app",
          scripts: {
            test: "jest",
          },
          dependencies: {
            // jest: "29.5.0",
            "@testing-library/jest-dom": "5.16.5",
          },
        }),
        "test.test.js": `
          import { readFileSync } from "fs";
          import '@testing-library/jest-dom/extend-expect';

          describe('index.html', () => {
            let content;
            beforeEach(() => {
              content = readFileSync("./index.html",'utf8');
              document.documentElement.innerHTML = content;
            });
          
            it('contains h1 tag', () => {
              expect(document.querySelector("h1")).not.toBeNull();
            });

            it('h1 tag has text Murad', () => {
              expect(document.querySelector("h1").innerHTML).toEqual("Murad");
            });
            
            it('has to have a style', () => {

              expect(document.querySelector('h1')).toHaveStyle({
                backgroundColor: 'red',
                fontSize: '16px',
              });
            });

          });`,
        "index.html": `<html>
        <head>
          <title>first</title>
          <style>
            h1 {
              background-color: red;
              font-size: 16px;
            }
          </style>
        </head>
        <body>
          <h1>Murad</h1>
        </body>
        </html>`,
      });
      const shell = emulator.shell.create();
      shell.on("progress", (status) => console.log(status));
      shell.stderr.on("data", (data) => {
        console.log("Error:", data);
      });
      shell.stdout.on("data", (data) => {
        console.log("Output:", data);
      });
      shell.on("exit", (status) => console.log(status));
      const serverCommand = await shell.runCommand("npm", ["run", "test"]);
      console.log(serverCommand);
    }
  };

  useEffect(() => {
    startEmulator();
  }, []);

  return (
    <Box>
      <iframe ref={iFrame}></iframe>
    </Box>
  );
};

export default TestRunner;

and log:
image
basicaly it downloads the packages and quits

@s4847043
Copy link
Author

s4847043 commented Jun 8, 2023

any comments?

@Marius-Adam
Copy link

Did you manage to fix it? Currently facing similar issue

@qargali
Copy link

qargali commented Jul 6, 2023

Unfortunately no. I used webcontainer by stackblitz. repo: https://github.com/stackblitz/webcontainer-core

@DeMoorJasper
Copy link
Collaborator

Please discuss using webcontainers for jest in the webcontainers repo, this isn't really the place to do that

@Sandpack Sandpack deleted a comment from Marius-Adam Jul 7, 2023
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

4 participants