0

I do want to create test data in the beforeEach method and access that data to give to the parameterized test but I get an error TS2454: Variable  testData  is used before being assigned.

describe('Test', async () => {
  let testProduct: Product

  beforeEach(async () => {
      testData = await createUser();
  })

  [
    { input: testProduct.id, error: false }, // TS2454: Variable  testData  is used before being assigned
    { input: "Non Existent", error: true },
  ].forEach(({ input, error }) => 
    it('Test Query', async () => {
      const client = createApolloTestClient();
      const res = await client.query<{userFound: boolean}, {id: string}>({
        query: ProductQuery,
        variables: {id: input}
      });
      expect(res.data?.userFound).toBe(error)
    }))
})

How can I refactor this to make it work?

6
  • You're missing many closing parentheses... Commented Nov 24, 2023 at 10:58
  • (Also FWIW I think having conditional behaviour inside the test suggests that those two cases shouldn't be parameterised. Just write two tests.) Commented Nov 24, 2023 at 11:04
  • There was one parenthesis too many. I deleted it. Also yes the expected is simplified cause I didn't want to copy & paste my production code. I still need it as parameterized Commented Nov 24, 2023 at 11:05
  • No, you have quite a lot too few. What you've posted isn't JavaScript. Commented Nov 24, 2023 at 11:08
  • True it's Typescript. Where do you want to see extra paranthesis? Just for you I removed the condition Commented Nov 24, 2023 at 11:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.