jest-community / jest-editor-support

A module for handling editor to jest integration
MIT License
28 stars 21 forks source link

Parsing fails upon encountering optional chaining #51

Open JonathanWilbur opened 4 years ago

JonathanWilbur commented 4 years ago

Upon encountering an optional chaining operator, the parsing fails. I am using this in the Jest VS Code Extension, but I have also tried the other popular Jest VS Code Extension, both of which fail and both of which use this library. Here is an example that you can try yourself:

This parses correctly. In VS Code, I can see the "Run | Debug" floating over the tests.

describe("Normalization", () => {
    test("adds a module to every assignment", () => {
        expect(assignment.module.name).toBe("AuthenticationFramework");
    });
});

When you add the question mark (optional chaining operator), it no longer parses correctly.

This does not parse correctly. I cannot see the "Run | Debug" floating over the tests anymore.

describe("Normalization", () => {
    test("adds a module to every assignment", () => {
        expect(assignment.module?.name).toBe("AuthenticationFramework");
    });
});
JonathanWilbur commented 4 years ago

I see now that this is because this library does not execute parse from the @babel/parser library with any plugins, so only the bare minimum "vanilla Babel" would be supported. I believe this should be easy to fix by requireing the local babel.config.js file (or making it configurable, at least) so that the required Babel plugins can be used.

In my case, I believe this plugin would fix it, but again, this library does not use any of the plugins that I indicate in babel.config.js.

connectdotz commented 4 years ago

hmmm... actually the parser should be able to parse the output from all plugins, see here. But it doesn't load the plugin for you, your project will still need to load the particular plugin so it can be transpired correctly for the parser to parse...

where did you see the error message? from the output window or developer console? Are you able to run jest in the terminal? Is vscode-jest using the same command (jest.pathToJest) above?

If you still have problems, please create a sample repo so we can quickly narrow it down.

JonathanWilbur commented 4 years ago

@connectdotz I have installed the two related plugins just to be sure. Neither worked.

        "@babel/plugin-proposal-optional-chaining": "^7.11.0",
        "@babel/plugin-syntax-optional-chaining": "^7.8.3",

And in babel.config.js, I indicated that I want both to be used.

    plugins: [
        "@babel/plugin-syntax-optional-chaining",
        "@babel/plugin-proposal-optional-chaining",
    ],

But still, the problem persists.

where did you see the error message? from the output window or developer console? Are you able to run jest in the terminal? Is vscode-jest using the same command (jest.pathToJest) above?

All of my tests run fine when I run with the jest command line, either via npm run test or the jest command directly.

JonathanWilbur commented 4 years ago

For clarity, I have tried with both plugins individually as well.

connectdotz commented 4 years ago

@JonathanWilbur this might sound silly, but you did restart the extension after adding the babel plugin, right?

If none worked, please create a sample repo. I am pretty sure the parser is able to parse optional chaining as I have used it in my projects.

JonathanWilbur commented 4 years ago

@connectdotz https://github.com/JonathanWilbur/jest-editor-support-issue-51

dinofx commented 3 years ago

optional chaining and many other typescript language features also cause parsing to fail. For example, I moved this function out of a test file to prevent parsing from failing:

export function assert(condition: any): asserts condition {
  expect(condition).toBe(true);
}

Given the ubiquity of TypeScript (the vscode-jest extension, for example), would be nice for editor support to enable TS support for .ts[x] files.

connectdotz commented 3 years ago

@JonathanWilbur I tried your repo and the test (in test/jest/index.test.js) showed pass, no parsing error, everything looks all right to me...

[update] However, I am using the vscode-jest v4 alpha version... maybe you can try https://github.com/jest-community/vscode-jest/releases/tag/v4.0.0-alpha.1, to see if your problem resolved?