David-Kunz / jester

A Neovim plugin to easily run and debug Jest tests
The Unlicense
198 stars 12 forks source link

Could not find any of the following: test, it, describe #13

Closed stephaneeybert closed 2 years ago

stephaneeybert commented 2 years ago

I created an Angular application which offered 3 tests by default: ng new ng-dummy

I then could run the tests from the command line:

11:12 $ ng test
✔ Browser application bundle generation complete.
19 06 2022 11:12:59.914:WARN [karma]: No captured browser, open http://localhost:9876/
19 06 2022 11:12:59.931:INFO [karma-server]: Karma v6.3.20 server started at http://localhost:9876/
19 06 2022 11:12:59.932:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
19 06 2022 11:12:59.936:INFO [launcher]: Starting browser Chrome
19 06 2022 11:13:01.190:INFO [Chrome 102.0.5005.115 (Linux x86_64)]: Connected on socket rEYPsN7y3S_ui1boAAAB with id 55219348
Chrome 102.0.5005.115 (Linux x86_64): Executed 3 of 3 SUCCESS (0.213 secs / 0.164 secs)
TOTAL: 3 SUCCESS
Chrome 102.0.5005.115 (Linux x86_64) ERROR
  Disconnected Client disconnected from CONNECTED state (transport close)
Chrome 102.0.5005.115 (Linux x86_64): Executed 3 of 3 SUCCESS (0.213 secs / 0.164 secs)
Chrome 102.0.5005.115 (Linux x86_64) ERROR
  Disconnected Client disconnected from CONNECTED state (transport close)

I was hopping to run them from within nvim:

use "David-Kunz/jester"

local status_ok, jester = pcall(require, "jester")    
if not status_ok then    
  return    
end      
jester.setup {    
}

With the current cursor location in the body of one test, I typed in the command: :lua require"jester".run()

But it did not run the test. Instead of displayed the message: Could not find any of the following: test, it, describe

Angular CLI: 13.3.3 Node: 16.14.2 Package Manager: npm 8.5.0 OS: linux x64 14:01 $ nvim --version NVIM v0.7.0 Build type: Release LuaJIT 2.1.0-beta3 Compiled by runner@fv-az316-460

David-Kunz commented 2 years ago

Hi @stephaneeybert ,

That's interesting. Would you mind showing the content of the file? Aren't there any describe/test/it functions?

stephaneeybert commented 2 years ago

@David-Kunz Hello ! Sorry for the late reply. So here is the full source code of the test generated by the Angular CLI tool:

  import { TestBed } from '@angular/core/testing';    
  import { RouterTestingModule } from '@angular/router/testing';    
  import { AppComponent } from './app.component';    

  describe('AppComponent', () => {    
    beforeEach(async () => {    
      await TestBed.configureTestingModule({    
        imports: [    
          RouterTestingModule    
        ],    
        declarations: [    
          AppComponent    
        ],    
      }).compileComponents();    
    });    

    it('should create the app', () => {    
      const fixture = TestBed.createComponent(AppComponent);    
      const app = fixture.componentInstance;    
      expect(app).toBeTruthy();    
    });    

    it(`should have as title 'ng-dummy'`, () => {    
      const fixture = TestBed.createComponent(AppComponent);    
      const app = fixture.componentInstance;    
      expect(app.title).toEqual('ng-dummy');    
    });    

    it('should render title', () => {    
      const fixture = TestBed.createComponent(AppComponent);    
      fixture.detectChanges();    
      const compiled = fixture.nativeElement as HTMLElement;    
      expect(compiled.querySelector('.content span')?.textContent).toContain('ng-dummy app is running!');    
    });    
  }); 
David-Kunz commented 2 years ago

Thanks @stephaneeybert!

So there are some function calls with name it as well as describe blocks.

What Jester does: It looks at the parsed source code (done by Tree-Sitter) and finds the next it/test/describe function call.

Would you mind installing the plugin https://github.com/nvim-treesitter/playground and show the output of :TSPlayground?

stephaneeybert commented 2 years ago

I installed the above plugin and could see it being installed.

    use {    
      "nvim-treesitter/nvim-treesitter",    
      run = ":TSUpdate",    
    }    
    use "nvim-treesitter/playground"

My configuration is:

  local status_ok, configs = pcall(require, "nvim-treesitter.configs")    
  if not status_ok then                            
    print("The treesitter extension could not be loaded")    
    return                    
  end      

  configs.setup {    
    sync_install = false, -- installing languages asynchronously    
    ignore_install = { "haskell", "go" }, -- List of parsers to ignore installing    
    autopairs = {    
      enable = true                           
    },       
    highlight = {                           
      enable = true, -- enable the syntax highlighting    
      disable = { "css", "markdown" }, -- list of languages to disable    
      additional_vim_regex_highlighting = { "php" },    
    },    
    indent = {                                
      enable = true,                                                                      
      disable = {                  
        "yaml"                               
      }                           
    },                                            
    playground = {                   
    }    
  }

But I guess I must have missed something, because when I type in the command :TSPlayground the right pane that opens up is totally empty, this on any project.

David-Kunz commented 2 years ago

Interesting, then it seems to be a problem with parsing.

Is Tree-Sitter enabled for the buffer? Is the correct language detected?

stephaneeybert commented 2 years ago

I wonder how to check that...

For information, I do have errors showing up in Typescript and Java files, and I have the LSP servers installed and can even debug in Typescript.

If I type in a syntax error in the above source code, then it shows a red mark and a diagnostic is available.

typescript: ',' expected.
typescript: Variable 'fi' implicitly has an 'any' type

My nvim configuration is available as well.

David-Kunz commented 2 years ago

Okay, then the LSP server and its interaction with Neovim is correctly working. It seems that Tree-Sitter isn't used for parsing.

stephaneeybert commented 2 years ago

The command TSPlaygroundToggle gives for a Typescript file, an empty right hand window for a Javascript file, an empty right hand window

There is no such thing as the DOM tree in that window.

Sorry to keep you busy :-)

David-Kunz commented 2 years ago

Interesting... Did you enable Tree-Sitter for those files in the first place?

In my configuration, I have

require'nvim-treesitter.configs'.setup {
  highlight = {
    enable = true
  }
}

to enable Tree-Sitter for syntax highlighting. Maybe this is a requirement.

Here's more info about how to enable it: https://github.com/nvim-treesitter/nvim-treesitter

lethanhan97 commented 2 years ago

Hey there, ran into this issue and this is what I did to resolve it

tree-sitter needs to be installed, along with the language servers and syntax highlighting to be enabled in order for this to work. This is the config I added to my init.vim

require'nvim-treesitter.configs'.setup {
  ensure_installed = { "javascript", "typescript" },
  highlight = {
    enable = true
  }
}

Perhaps we could update README.md? If you don't mind, I can make a PR in a couple of hours (since it's really late in my timezone right now)

David-Kunz commented 2 years ago

Thanks a ton for this info @lethanhan97 , yes, adding it to the README would be great! I will do this real quick.

David-Kunz commented 2 years ago

https://github.com/David-Kunz/jester/pull/14

stephaneeybert commented 2 years ago

@David-Kunz Yes I had this setup as well as seen in my pasted code above.

So I added the missing configuration:

ensure_installed = { "javascript", "typescript", "java" },

And now I can see the DOM tree for all 3 languages listed above.

Thanks guys !

stephaneeybert commented 2 years ago

All right there is progress. When using jester to run a test I now get another message:

nvim-treesitter.ts_utils.get_node_text is deprecated: use vim.treesitter.query.get_node_text

I reckon I need to clean up some cache or fix some config somewhere.

David-Kunz commented 2 years ago

Hi, this is just a warning because of deprecated APIs. You can ignore that for now, it shouldn't be a problem. I will change it soon.

stephaneeybert commented 2 years ago

Indeed I can pass the warning message. But I may have missed a step on the installation. As I get the following output from running a test:


jest -t 'AppComponent should create the app' -
-dummy/src/app/app\\.component\\.spec\\.ts
✔ ~/trash/ng-dummy [master L|✔] 
22:49 $ jest -t 'AppComponent should create th
ash/ng\\-dummy/src/app/app\\.component\\.spec\
Command 'jest' not found, did you mean:
command 'test' from deb coreutils (8.30-3ubu```
David-Kunz commented 2 years ago

Hi @stephaneeybert,

This is strange. Why does the jest command fail in the second example?

In any way: You can configure the jest path using the options of Jester, maybe that will help?

stephaneeybert commented 2 years ago

Oh ! There is such a thing as a Jest PATH ? That tells me I should install a jest utility on my system.

Now I see from your README here a link to https://jestjs.io and I discover a testing framework.

I had no clue your plugin needed the installation of an external command. Sorry !

I shall now proceed to install jest on my system and have it in my PATH and let you know about my mileage.

stephaneeybert commented 2 years ago

I now installed jest with my install script:

if ! command -v jest &> /dev/null
then
  printf "\nInstalling the jest program used by the jester plugin\n"
  npm install -g --save-dev jest
  printf "\nConfiguring jest for typescript\n"
  npm install --save-dev @babel/preset-typescript
  npm install --save-dev babel-jest @babel/core @babel/preset-env
  npm install --save-dev ts-jest
  npm install --save-dev @types/jest
fi

I'll try to configure jest for typescript and come back with my results. Thanks !

David-Kunz commented 2 years ago

I'm closing this issue, if you still experience any problems, please come back to me.

phortonssf commented 1 year ago

I now installed jest with my install script:

if ! command -v jest &> /dev/null
then
  printf "\nInstalling the jest program used by the jester plugin\n"
  npm install -g --save-dev jest
  printf "\nConfiguring jest for typescript\n"
  npm install --save-dev @babel/preset-typescript
  npm install --save-dev babel-jest @babel/core @babel/preset-env
  npm install --save-dev ts-jest
  npm install --save-dev @types/jest
fi

I'll try to configure jest for typescript and come back with my results. Thanks !

Did you come up with a working config for typescript?