Skip to content

Add a way to conditionally skip a test #2295

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

Closed
gajus opened this issue Nov 18, 2019 · 3 comments
Closed

Add a way to conditionally skip a test #2295

gajus opened this issue Nov 18, 2019 · 3 comments

Comments

@gajus
Copy link

gajus commented Nov 18, 2019

Use case

When writing integrations tests, it is common that a subset of tests require some configuration to be present, e.g. database connection URI. It is desirable not to run tests when that configuration is not present.

Proposed implementation

I would like to be able to skip a test from a within the test itself, e.g.

test('authenticates using captcha', async (t) => {
  if (!process.env.CAPTCHA_SOLVE_ID) {
    return t.skip('CAPTCHA_SOLVE_ID is not configured');
  }

  const captchaSolver = createCaptchaSolver(process.env.CAPTCHA_SOLVE_ID);

  const error = await t.throwsAsync(createAuthenticationCookieJar(captchaSolver, '[email protected]', 'bar'));

  t.true(error instanceof AuthenticationError);
  t.is(error.message, 'Invalid email or password.');
});
@gajus
Copy link
Author

gajus commented Nov 18, 2019

Another way this can be solved in user-land is:

const main = () => {
  if (!process.env.CAPTCHA_SOLVE_ID) {
    test.todo('integration tests cannot run because process.env.CAPTCHA_SOLVE_ID is not configured');

    return;
  }

  test('authenticates using captcha', async (t) => {
    const captchaSolver = createCaptchaSolver(process.env.CAPTCHA_SOLVE_ID);

    const error = await t.throwsAsync(createAuthenticationCookieJar(captchaSolver, '[email protected]', 'bar'));

    t.true(error instanceof AuthenticationError);
    t.is(error.message, 'Invalid email or password.');
  });
};

main();

@sindresorhus
Copy link
Member

I think this is a duplicate of #2010. I agree, something like this would be very useful.

@gajus
Copy link
Author

gajus commented Nov 18, 2019

You are right. It is duplicate. Sorry, missed the other issue. t.cancel suggestion makes sense too.

@gajus gajus closed this as completed Nov 18, 2019
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