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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: test.skipIf(condition, name, test) #3652

Closed
relekang opened this issue May 25, 2017 · 16 comments
Closed

Feature request: test.skipIf(condition, name, test) #3652

relekang opened this issue May 25, 2017 · 16 comments

Comments

@relekang
Copy link

First of all thanks for making Jest 馃檶

Do you want to request a feature or report a bug?
Request a feature. I will be happy to help out to make this happen if the feature is wanted.

What is the current behavior?
To skip test dynamically based on dynamic properties like the environment tests are running in it is necessary to wrap the body of the test or the whole test in an if statement. See example below from the jest codebase:

it('supports unix separators', () => {
  if (process.platform !== 'win32') {
    const path = '/path/to/__tests__/test.js';
    expect(searchSource.isTestFilePath(path)).toEqual(true);
  }
});

What is the expected behavior?
Would it be nice to be able to do it.skipIf(condition, name, test) and test.skipIf(condition, name, test)? That way the test report will show the tests as skipped instead of successful and it will be easier to detect if a skip condition is wrong.

it.skipIf(process.platform !== 'win32', 'supports unix separators', () => {
  const path = '/path/to/__tests__/test.js';
  expect(searchSource.isTestFilePath(path)).toEqual(true);
});

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Node v7.7.2, Jest v20.0.4, Yarn v0.24.5, no extra jest configuration

@cpojer
Copy link
Member

cpojer commented May 25, 2017

I don't think we'll support this for now as it doesn't seem like a necessary core feature. There is nothing that stops you from building your own wrapper for this feature, though :)

@cpojer cpojer closed this as completed May 25, 2017
@relekang
Copy link
Author

Will look into that, thanks for the quick response 馃檪

@relekang
Copy link
Author

If anyone stumbles over the issue looking for the same thing. I ended up with skip-if.

@kodikos
Copy link

kodikos commented Apr 29, 2018

Here is a simple pattern for the odd one-off occasion

const testIfCondition = mySkipCondition ? test.skip : test;
describe('Conditional Test', () => {
  testIfCondition('Only test on condition', () => {
    ...
  });
});

@roonyh
Copy link

roonyh commented Nov 21, 2018

Can't figure out how to do this for asynchronous tests :(

My skip condition is only available inside an asynchronous callback.

@kodikos
Copy link

kodikos commented Nov 21, 2018

Hi roonyh. Off the top of my head, the above would be something like:

whateverYourAsyncThingIs()
.then((values) => {
  return mySkipConditionBasedOnValues ? test.skip : test;
}).then((testIfCondition) => {
  describe('Conditional Test', () => {
    testIfCondition('Only test on condition', () => {
      ...
    });
  });
})

@roonyh
Copy link

roonyh commented Nov 21, 2018

Hi, Unfortunately that does not work. tests defined inside callbacks are not picked up by the runner.

@SimenB
Copy link
Member

SimenB commented Nov 21, 2018

Tests have to be defined synchronously. https://jestjs.io/docs/en/troubleshooting.html#defining-tests

@roonyh
Copy link

roonyh commented Nov 21, 2018

@SimenB Ah. I figured this out, but could not find it documented. Thanks for pointing to it.

@SimenB
Copy link
Member

SimenB commented Nov 21, 2018

You can do async work in globalSetup, but we currently do not have a good way of sharing stuff from globalSetup with tests. You can follow that in #7184

@dandv
Copy link
Contributor

dandv commented Jun 23, 2019

This is a dupe of #7245, (it's newer but has a lot more comments), which shows the demand for this feature.

@kodikos's workaround breaks IDEs, which won't recognize individual tests any more. Reposting a screenshot from the other issue, where the same workaround was suggeted:

image

@FabianoLothor
Copy link

FabianoLothor commented Aug 1, 2019

I would have liked to saw this feature implemented.

@ronny1982
Copy link

Here is what i'm doing:

(process.platform === 'linux' ? it : it.skip)('should test linux code only', () => {
    // this test will run on linux and be skipped on all others ...
});

Can also be used for e.g. file scopes:

// assign the global `it` method based on platform
it = (process.platform === 'linux' ? it : it.skip);

// or even the global `describe` method based on platform
describe = (process.platform === 'linux' ? describe : describe.skip);

@pxmage
Copy link

pxmage commented Sep 12, 2019

this is my way of achieving this

export const itif = (name: string, condition: () => boolean, cb) => {
  it(name, (done) => {
    if(condition()) {
      cb(done)
    } else {
      console.warn(`[skipped]: ${name}`)
      done();
    }
  })
}

I created a helper function itif, then I can use it in this way:

      itif('should check this just registered user', () => user !== undefined, (done) => {
           // you test code
      })

in the above example, the user is a variable assigned by test cases before this one asynchronously.

PS: I ignored the timeout argument, you can add it if you need it.

@qmenoret
Copy link

qmenoret commented Feb 7, 2020

If anyone is interested, we wrote a small extension to Jest to allow to select the OS to run the tests on nicely: https://www.npmjs.com/package/jest-os-detection 馃檪

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants