Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling the 'Your test suite must contain at least one test' rule in Jest

Tags:

jestjs

Jest has a 'Your test suite must contain at least one test.' rule. It complains that if I have a file that isn't a test suite it cannot exist in the __test__/ directory that I have created (see below). This basically means everything in that folder (including the subfolders) must be a test suite.

How do I get around this? I really want to store my mock data objects with my test suites.

enter image description here

like image 597
Oliver Watkins Avatar asked Feb 01 '19 10:02

Oliver Watkins


People also ask

What is a test suite in Jest?

Next we call a global Jest function describe(). In our TweetUtils-test. js file we're not just creating a single test, instead we're creating a suite of tests. A suite is a collection of tests that collectively test a bigger unit of functionality.


2 Answers

Calling skip in your test utility file will suppress that rule violation:

test.skip('Workaround', () => 1)

Adding this for completeness. Brian's testMatch/testRegex solution seems more clean to me.

like image 113
Gabriel Deal Avatar answered Oct 05 '22 01:10

Gabriel Deal


The default glob patterns that Jest uses to find test files are

[ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]

In other words...

By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.

Because mockData.js is in a subdirectory of __tests__ it is being found by the default glob patterns and assumed to be a test.

To keep Jest from treating mockData.js as a test you can change the glob patterns Jest uses to find tests with the testMatch configuration option, or to use regular expressions instead of glob patterns use testRegex.

like image 44
Brian Adams Avatar answered Oct 05 '22 00:10

Brian Adams