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

Fix ENOENT error for Jest test in function-no-unknown #5904

Closed
100terres opened this issue Feb 12, 2022 · 12 comments · Fixed by #5916
Closed

Fix ENOENT error for Jest test in function-no-unknown #5904

100terres opened this issue Feb 12, 2022 · 12 comments · Fixed by #5916
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule

Comments

@100terres
Copy link

100terres commented Feb 12, 2022

What steps are needed to reproduce the bug?

I've documented the issue on a library used by stylelint

When running this the function-no-unknown rule throws this error

ENOENT: no such file or directory, open '/index.json'

Here is where it fails https://github.com/react-forked/dnd/blob/e18cfa53c3f6bb8f085137c934a4a0d9b8332671/test/unit/view/style-marshal/get-styles.spec.ts

Here is the error in CircleCI
https://app.circleci.com/pipelines/github/react-forked/dnd/229/workflows/9d30597b-ac76-4548-b2cb-3416ab0a03fe/jobs/1331

What Stylelint configuration is needed to reproduce the bug?

{
  "extends": ["stylelint-config-recommended"]
}

How did you run Stylelint?

Node stylelint.lint(...)

Which version of Stylelint are you using?

14.5.0

What did you expect to happen?

To not throw an error

What actually happened?

It throws this error

ENOENT: no such file or directory, open '/index.json'

Does the bug relate to non-standard syntax?

Nop

Proposal to fix the bug

As suggested here I've made it work by using path.resolve here in this lib and directly using the return of the require("css-functions-list") here without using the new URL.

@jeddy3 jeddy3 changed the title The new function-no-unknown rule can break Fix ENOENT error for Jest test in function-no-unknown Feb 12, 2022
@jeddy3 jeddy3 added the status: needs investigation triage needs further investigation label Feb 12, 2022
@100terres
Copy link
Author

100terres commented Feb 12, 2022

I've look around a bit and I found that it might also be related to TypeScript (I forgot to mention that I was using it).

Issue following jest ESM support

@ybiquitous
Copy link
Member

ybiquitous commented Feb 13, 2022

The require("css-functions-list") way has a problem on Windows. See below:

@ybiquitous
Copy link
Member

ybiquitous commented Feb 13, 2022

@100terres I've opened a pull request #5906 that may fix your problem. Can you try it, please?

-    "stylelint": "^14.5.0",
+    "stylelint": "github:stylelint/stylelint#issue-5904",

@ybiquitous
Copy link
Member

Sorry, PR #5906 doesn't work. 😢
See #5906 (comment)

@ybiquitous
Copy link
Member

@100terres I've opened PR #5916 that no longer uses URL thanks to css-functions-list@3.0.1.
Can you try whether to resolve ENOENT, please?

-    "stylelint": "^14.5.0",
+    "stylelint": "github:stylelint/stylelint#bump-css-functions-list",

@jeddy3 jeddy3 added status: wip is being worked on by someone type: bug a problem with a feature or rule and removed status: needs investigation triage needs further investigation labels Feb 14, 2022
@100terres
Copy link
Author

100terres commented Feb 14, 2022

Hi @ybiquitous thank you for your time! 🙂

I've just tested it here and know it's giving me this error. I didn't had the time to investigate if I need to change something in my config (with jest or TypeScript)

    TypeError: The URL must be of scheme fileTypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file

      at Object.<anonymous> (node_modules/css-functions-list/index.js:6:18)
      at Object.<anonymous> (node_modules/stylelint/lib/rules/function-no-unknown/index.js:5:27)

Here is the result of the CI https://app.circleci.com/pipelines/github/react-forked/dnd/232/workflows/bf31edb7-16e2-4c43-9835-0271ab13ef75/jobs/1349

And now event if I disable the function-no-unknown rule I still get the error described above, but only for the first test that run. Circle CI.

TypeError: The URL must be of scheme fileTypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file

      at Object.<anonymous> (node_modules/css-functions-list/index.js:6:18)
      at Object.<anonymous> (node_modules/stylelint/lib/rules/function-no-unknown/index.js:5:27)

@ybiquitous
Copy link
Member

@100terres Thanks for the detailed reproduction. I'm not sure why ERR_INVALID_URL_SCHEME is raised... 🤷🏼

@ybiquitous
Copy link
Member

ybiquitous commented Feb 15, 2022

@100terres The problem seems due to jsdom. When changing testEnvironment to 'node', it works:

diff --git a/jest.config.js b/jest.config.js
index 08a90c82..52634cf4 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -12,4 +12,5 @@ module.exports = {
     'jest-watch-typeahead/testname',
   ],
   verbose: true,
+  testEnvironment: 'node',
 };
$ npx jest test/unit/view/style-marshal/get-styles.spec.ts
 PASS  test/unit/view/style-marshal/get-styles.spec.ts
  ✓ should generate valid always styles (117 ms)
  ✓ should generate valid resting styles (5 ms)
  ✓ should generate valid dragging styles (6 ms)
  ✓ should generate valid dropAnimating styles (3 ms)
  ✓ should generate valid userCancel styles (1 ms)

Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        0.946 s, estimated 2 s
Ran all test suites matching /test\/unit\/view\/style-marshal\/get-styles.spec.ts/i.

EDIT: Tested on hello-pangea/dnd@b16f775

@ybiquitous
Copy link
Member

css-functions-list@3.0.1 includes the following code on CJS:

node_modules/css-functions-list/cjs/index.js:

'use strict';

var url = require('url');

/**
 * Path to CSS functions list JSON file.
 */
const location = url.fileURLToPath(new url.URL('index.json', (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.js', document.baseURI).href))));

module.exports = location;
//# sourceMappingURL=index.js.map

I try embedding the debug code into the above file:

 var url = require('url');
+console.log('document:', document)
+console.log('document.currentScript:', document.currentScript)
+console.log('document.baseURI:', document.baseURI)
 /**
  * Path to CSS functions list JSON file.
  */

Then, run the failed test:

$ npx jest test/unit/view/style-marshal/get-styles.spec.ts
  console.log
    document: Document {
      location: [Getter/Setter],
      [Symbol(DOM SymbolTree)]: SymbolTreeNode {
        parent: null,
        previousSibling: null,
        nextSibling: null,
        firstChild: null,
        lastChild: null,
        childrenVersion: 0,
        childIndexCachedUpTo: null,
        cachedIndex: -1,
        cachedIndexVersion: NaN
      }
    }

      at Object.<anonymous> (node_modules/css-functions-list/cjs/index.js:4:9)

  console.log
    document.currentScript: null

      at Object.<anonymous> (node_modules/css-functions-list/index.js:3:1)

  console.log
    document.baseURI: http://localhost/

      at Object.<anonymous> (node_modules/css-functions-list/index.js:4:1)

 FAIL  test/unit/view/style-marshal/get-styles.spec.ts
...

Unexpectedly, document.baseURI returns http://localhost, so new URL('index.js', document.baseURI) doesn't point to a local file path in the node_modules/css-functions-list directory.

@100terres
Copy link
Author

100terres commented Feb 15, 2022

Hi @ybiquitous thank you a lot for taking the time to investigate this futher! 🙂

I've just tested it and has you mention the test works in the node environment.

It feels like that inside a jest test that is using stylelint, we should be running this test in a node environment and not in a jsdom environment (which mimic a browser environment).

@100terres
Copy link
Author

100terres commented Feb 15, 2022

Also jsdom does not seem to support esm yet jsdom/jsdom#2475

@ybiquitous
Copy link
Member

@100terres Thanks for sharing the knowledge. 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule
3 participants