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

JEST - Test Nearest on tests that has .each() does not work #672

Open
ckangnz opened this issue Jul 26, 2022 · 13 comments
Open

JEST - Test Nearest on tests that has .each() does not work #672

ckangnz opened this issue Jul 26, 2022 · 13 comments

Comments

@ckangnz
Copy link

ckangnz commented Jul 26, 2022

When you have jest tests;

describe.each([1, 2 ])('Loop the test with given array',()=>{})

it.each([1, 2 ])('Loop the test with given array',()=>{})

test.each([1, 2 ])('Loop the test with given array',()=>{})

the functionality of "Test Nearest" does not work and skips the test.

@codeinabox
Copy link
Collaborator

Could you please confirm if this is still an issue after updating to the latest version of the plug-in

@ckangnz
Copy link
Author

ckangnz commented Aug 19, 2022

I'll check it out next monday when I'm back at work!! Looking forward to experience the bug fix!! Thank you

@ckangnz
Copy link
Author

ckangnz commented Aug 20, 2022

@codeinabox it seems like they're not working.

I have tests that are:

  1. NOT WORKING
describe.each([1,2])('Given %s' , (number)=>{
  it.each(['a','b'])(' should print %s', (string)=>{
    expect(number).toBe(number); <---------- TEST THIS
    expect(string).toBe(string);
  })
})

  1. NOT WORKING
describe.each([1,2])('Given %s' , (number)=>{
  it(' should print a', ()=>{
    expect(number).toBe(number); <-------- TEST THIS
  })
})
  1. NOT WORKING
describe('Given number is 1' , ()=>{
  it.each(['a','b'])(' should print %s', (string)=>{
    expect(string).toBe(string); <----------TEST THIS
  })
})

@codeinabox
Copy link
Collaborator

Thank you for checking, so to confirm TestNearest is being called on the lines where you have "<----------TEST THIS"?

@ckangnz
Copy link
Author

ckangnz commented Aug 23, 2022

@codeinabox that's correct.

I just pulled down the latest update on vim-test, and apparently there was a fix around playwright test #682.

When I tested, it looks like it is half working. The test at least doesn't skip anymore, however, only a single test runs.

For example,

describe('Given number is 1' , ()=>{
  it.each(['a','b'])(' should print %s', (string)=>{
    expect(string).toBe(string); <----------TEST THIS
  })
})

will only run for the b case.

@ckangnz
Copy link
Author

ckangnz commented Aug 23, 2022

Given I have these tests:

describe('Given number is 1', () => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(string).toBe(string);<--- CURSOR HERE
  });
});

describe.each([1, 2])('Given %s', number => {
  it('should print a', () => {
    expect(number).toBe(number); 
  });
});
describe.each([1, 2])('Given %s', number => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(number).toBe(number);
    expect(string).toBe(string);
  });
});

The test SKIPS.


describe('Given number is 1', () => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(string).toBe(string);
  });
});

describe.each([1, 2])('Given %s', number => {
  it('should print a', () => {
    expect(number).toBe(number); <--- CURSOR HERE
  });
});
describe.each([1, 2])('Given %s', number => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(number).toBe(number);
    expect(string).toBe(string);
  });
});

I get
image


describe('Given number is 1', () => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(string).toBe(string);
  });
});

describe.each([1, 2])('Given %s', number => {
  it('should print a', () => {
    expect(number).toBe(number); 
  });
});
describe.each([1, 2])('Given %s', number => {
  it.each(['a', 'b'])('should print %s', string => {
    expect(number).toBe(number);<--- CURSOR HERE
    expect(string).toBe(string);
  });
});

The test SKIPS.

The describe.each() only runs a single test
The it.each() skips completely.

@arcogabbo
Copy link

same issue for me, do you guys have any update?

@codeinabox
Copy link
Collaborator

Thank you for all these examples that aren't working properly, we should add them to the spec for Jest so this issue can be resolved. I am pretty flat out these days, though if I find time I'll look into it.

@ckangnz
Copy link
Author

ckangnz commented Jan 19, 2023

Thanks so much! Wish i could be more helpful to contribute but I'm not too familiar with vim script..😅

@codeinabox
Copy link
Collaborator

I see the issue now, any each test using the printf notation isn't going to run because the currently generated regex won't match.

Given the earlier example you provided:

describe.each([1,2])('Given %d' , (number)=>{
  it.each(['a','b'])(' should print %s', (string)=>{
    expect(number).toBe(number);  // <---------- TEST THIS
    expect(string).toBe(string);
  })
})

The generated regex is currently ^Given %s a$, which has no matches. If I change it to ^Given then the tests will run, but it will now match other tests that start with "Given ". Ideally, vim-test would generate a regex like ^Given \d+ it should print \w+$, which would be a perfect match for this test.

However, converting the printf expressions into a flexible enough regex could get quite complicated. A simple fix would be to truncate the regex the moment it encounters any printf expressions, for example ^Given %s a$ would become ^Given . It may run more than just the nearest tests, but it would be an improvement over the current situation

@ckangnz
Copy link
Author

ckangnz commented Dec 7, 2023

That makes sense. Looking forward too try out the patch when it's fixed..!

@codeinabox
Copy link
Collaborator

Just remembered this, as I've run into a similar issue with Vitest, so I will try to pick this up again.

@ckangnz
Copy link
Author

ckangnz commented Apr 10, 2024

@codeinabox thanks so much! I've been waiting on this..!

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

3 participants