Repository: pyodide/pytest-pyodide (language: python)

Issue:
Fix Node tests
I added CI which runs tests for firefox and chrome in https://github.com/pyodide/pytest-pyodide/pull/1 however there are still failures for tests running in Node for some reason which need fixing. 
 

<details>

```
ERROR at setup of test_selenium3[node] ____________________
pyodide_test_runner/fixture.py:181: in selenium_module_scope
    with selenium_common(
/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/contextlib.py:135: in __enter__
    return next(self.gen)
pyodide_test_runner/fixture.py:86: in selenium_common
    runner = cls(
pyodide_test_runner/browser.py:121: in __init__
    self.driver = self.get_driver()
pyodide_test_runner/browser.py:496: in get_driver
    self.init_node()
pyodide_test_runner/browser.py:490: in init_node
    self.p.expect_exact("READY!!")
/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/pexpect/spawnbase.py:421: in expect_exact
    return exp.expect_loop(timeout)
/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/pexpect/expect.py:181: in expect_loop
    return self.timeout(e)
/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/pexpect/expect.py:144: in timeout
    raise exc
E   pexpect.exceptions.TIMEOUT: Timeout exceeded.
E   <pexpect.pty_spawn.spawn object at 0x7f60ab6923e0>
E   command: /bin/bash
E   args: ['/bin/bash']
E   buffer (last 100 chars): b'odide$ '
E   before (last 100 chars): b"test_runner/node_test_driver.js'\r\n  ]\r\n}\r\nrunner@fv-az133-654:~/work/pytest-pyodide/pytest-pyodide$ "
E   after: <class 'pexpect.exceptions.TIMEOUT'>
E   match: None
E   match_index: None
E   exitstatus: None
E   flag_eof: False
E   pid: 2477
E   child_fd: 18
E   closed: False
E   timeout: 60
E   delimiter: <class 'pexpect.exceptions.EOF'>
E   logfile: None
E   logfile_read: None
E   logfile_send: None
E   maxread: 2000
E   ignorecase: False
E   searchwindowsize: None
E   delaybeforesend: None
E   delayafterclose: 0.1
E   delayafterterminate: 0.1
E   searcher: searcher_string:
E       0: b'READY!!'
```

</details>

We should probably also run node tests for several node versions.


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index b2b7801e..5a87faa5 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -1,16 +1,12 @@
 name: main
 
-on:
-  push:
-    branches: [ "main" ]
-  pull_request:
-    branches: [ "main" ]
+on: [push, pull_request]
 
 permissions:
   contents: read
 
 concurrency:
-  group: main-${{ github.head_ref }}
+  group: main-${{ github.head_ref || github.run_id }}
   cancel-in-progress: true
 
 jobs:
@@ -47,6 +43,7 @@ jobs:
           # We then uninstall it otherwise tests fails due to pytest hook being
           # registered twice.
           pip uninstall -y pytest-pyodide
+          npm install node-fetch@2
       - name: Download Pyodide
         shell: bash -l {0}
         run: |
@@ -56,8 +53,8 @@ jobs:
       - name: Run tests
         shell: bash -l {0}
         run: |
-          # TODO: fix node tests
-          pytest --cov=pytest_pyodide -v --dist-dir=./dist/ -k "not node"
+          pytest --cov=pytest_pyodide -v --dist-dir=./dist/
       - uses: codecov/codecov-action@v3
+        if: ${{ github.event.repo.name == 'pyodide/pytest-pyodide' }}
         with:
           fail_ci_if_error: true
diff --git a/.gitignore b/.gitignore
index 3d77febd..a9743e64 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,6 @@
 __pycache__
 *.egg-info
 dist/
+
+# pyenv
+.python-version


Test patch:
diff --git a/pytest_pyodide/browser.py b/pytest_pyodide/browser.py
index 6a380f9a..f064be1c 100644
--- a/pytest_pyodide/browser.py
+++ b/pytest_pyodide/browser.py
@@ -488,7 +488,7 @@ def init_node(self):
 
         try:
             self.p.expect_exact("READY!!")
-        except pexpect.exceptions.EOF:
+        except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT):
             raise JavascriptException("", self.p.before.decode())
 
     def get_driver(self):
diff --git a/pytest_pyodide/node_test_driver.js b/pytest_pyodide/node_test_driver.js
index 10b89910..6fb1c6ec 100644
--- a/pytest_pyodide/node_test_driver.js
+++ b/pytest_pyodide/node_test_driver.js
@@ -2,7 +2,11 @@ const vm = require("vm");
 const readline = require("readline");
 const path = require("path");
 const util = require("util");
-const node_fetch = require("node-fetch");
+
+let nodeFetch = globalThis.fetch;
+if (!nodeFetch) {
+  nodeFetch = require("node-fetch");
+}
 
 let baseUrl = process.argv[2];
 let distDir = process.argv[3];
@@ -11,8 +15,8 @@ let { loadPyodide } = require(`${distDir}/pyodide`);
 process.chdir(distDir);
 
 // node requires full paths.
-function fetch(path) {
-  return node_fetch(new URL(path, baseUrl).toString());
+function _fetch(path) {
+  return nodeFetch(new URL(path, baseUrl).toString());
 }
 
 const context = {
@@ -20,8 +24,8 @@ const context = {
   path,
   process,
   require,
-  fetch,
   setTimeout,
+  fetch: _fetch,
   TextDecoder: util.TextDecoder,
   TextEncoder: util.TextEncoder,
   URL,