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

Neotest-jest not working with describe.each #124

Open
tweiss777 opened this issue Jul 31, 2024 · 2 comments
Open

Neotest-jest not working with describe.each #124

tweiss777 opened this issue Jul 31, 2024 · 2 comments

Comments

@tweiss777
Copy link

I'm trying to run some unit tests using neotest-jest, and I can't see the test results for some reason when using describe.each.

When running a test, such as:

import { sum } from '../src/AlgorithmsInTypescript/math'

describe('test sum module',() =>{
   test('1 + 2 should equal 3',()=> {
    const [a,b] = [1,2]
    expect(sum(a,b)).toBe(3)
  })
})

I can see the tests running fine.

Screenshot 2024-07-31 at 15 54 17

However, if I run the following test:

import groupAnagrams from '../src/AlgorithmsInTypescript/GroupAnagrams'

const testCases = [
  {
    input: ['abc', 'cba', 'bca', 'xyz', 'yxz', 'zxy'],
    output: [
      ['abc', 'cba', 'bca'],
      ['xyz', 'yxz', 'zxy'],
    ],
  },
  {
    input: ['rat', 'tar', 'art', 'star', 'tars', 'rats'],
    output: [
      ['rat', 'tar', 'art'],
      ['star', 'tars', 'rats'],
    ],
  },
  {
    input: ['no', 'on', 'is', 'si', 'at', 'ta'],
    output: [
      ['no', 'on'],
      ['is', 'si'],
      ['at', 'ta'],
    ],
  },
  {
    input: ['bored', 'robed', 'debit card', 'bad credit', 'earth', 'heart'],
    output: [
      ['bored', 'robed'],
      ['debit card', 'bad credit'],
      ['earth', 'heart'],
    ],
  },
  {
    input: [''],
    output: [['']],
  },
  {
    input: ['a', 'b', 'c', 'a', 'b', 'c'],
    output: [
      ['a', 'a'],
      ['b', 'b'],
      ['c', 'c'],
    ],
  },
  {
    input: ['apple', 'paple', 'pale', 'leap', 'peal'],
    output: [
      ['apple', 'paple'],
      ['pale', 'leap', 'peal'],
    ],
  },
]

describe.each(testCases)('Test group anagrams', ({ input, output }) => {
  it(`The input: ${input} should return ${output}`, () => {
    const result = groupAnagrams(input)
    const resultGroups = result.map((group) => new Set(group))
    const expectedGroups = output.map((group) => new Set(group))

    expectedGroups.forEach((expectedGroup) => {
      expect(resultGroups).toEqual(
        expect.arrayContaining([
          expect.objectContaining({
            size: expectedGroup.size,
            ...expectedGroup,
          }),
        ]),
      )
    })

    resultGroups.forEach((resultGroup) => {
      expect(expectedGroups).toEqual(
        expect.arrayContaining([
          expect.objectContaining({ size: resultGroup.size, ...resultGroup }),
        ]),
      )
    })
  })
})

I don't see the result if I run it via Neo-Test Jest, and it gives me a blue icon in the test explorer. It will work if I run the test via CLI.

Screenshot 2024-07-31 at 15 56 12

Below is my lua config with lazy for Neotest jest showing how I configured it.

return {
    -- Main testing dependency
    "nvim-neotest/neotest",
    dependencies = {
        "nvim-neotest/nvim-nio",
        "nvim-lua/plenary.nvim",
        "antoinemadec/FixCursorHold.nvim",
        "nvim-treesitter/nvim-treesitter",
        "nvim-neotest/neotest-jest",
        "Issafalcon/neotest-dotnet"
    },
    config = function()
        require("neotest").setup({
            adapters = {
                require("neotest-dotnet")({
                    discovery_root = 'solution'
                }),
                require("neotest-jest")({
                    jestCommand = "yarn test", -- Adjust this based on your npm command
                    jestConfigFile = "jest.config.js", -- If you have a Jest config file
                    env = { CI = true },
                    
                    cwd = function(path)
                        -- Assuming tests are in a 'tests' folder
                        local relative_path = vim.fn.fnamemodify(path, ":~:.")
                        if string.match(relative_path, "^tests/") then
                            return vim.fn.getcwd()
                        end
                        return vim.fn.getcwd()
                    end,
                }),
            },
        })
    end,
}
@mikaoelitiana
Copy link
Contributor

Hello @tweiss777, did you manage to make this work? Have you tried setting jest_test_discovery ?

require('neotest-jest')({
      ...,
      jest_test_discovery = true,
    }),

https://github.com/nvim-neotest/neotest-jest?tab=readme-ov-file#parameterized-tests

@mikaoelitiana
Copy link
Contributor

Actually I have the same issue, even with jest_test_discovery set, the tests are not displayed as expected

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

2 participants