1

I'm using jest and ts-jest configured in the way ts-jest describes in their docs.

If I run yarn test --listTests I can see the test file I want to run: processNewUser.ts included in the output in a __test__ folder in my project.

I can run that test only with yarn test --testPathPattern='processNewUser'

But If I try use yarn test --testNamePattern='Processes new user. Auth' which is the name of the test, then every test runs, including all with no name string like what is specified.

Same with: yarn test -t="Auth" yarn test -t Auth yarn test --testNamePattern "Auth" jest Auth jest -t="Processes" etc etc. and every combo of syntax really. Also tried this with naming a describe function wrapper instead of a test. No luck.

My tsconfig.json is:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true,
    "allowJs": true,
    "strict": true,
    "noImplicitAny": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "removeComments": false,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "lib": [
      "ES2020.Promise",
      "ES2015.Iterable",
      "ES2015.Symbol.WellKnown"
    ],
  },
  "include": ["src/**/*"],
}

My jest.config.ts is:

import type {Config} from '@jest/types';
const config: Config.InitialOptions = {
  clearMocks: true,
  coverageDirectory: 'coverage',
  coverageProvider: 'v8',
  moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
  preset: 'ts-jest',
  setupFiles: ['dotenv/config'],
  setupFilesAfterEnv: ['./jest.setup.js'],
  testEnvironment: 'node',
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
    '^.+\\.ts?$': 'ts-jest',
  },
  transformIgnorePatterns: ['/node_modules/', '\\.pnp\\.[^\\/]+$'],
  
}

export default config;

Yarn script is just: "test": "jest",

I want this to be able to tag a test with auth for example to have all auth tests run.

Any help?

Sean
  • 548
  • 5
  • 20

1 Answers1

0

The predicate for filtering out tests by name is defined here:

  if (globalConfig.testNamePattern) {
    const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
    env.specFilter = (spec: Spec) => testNameRegex.test(spec.getFullName());
  }

Notice that it checks created regexp against full name of each spec. I.e. the name of the test suite itself plus names of all surrounding describe blocks.

aleksxor
  • 5,898
  • 15
  • 20
  • thats just a definition, but no explanation for "why does jest run all tests, when i filter tests by a pattern?" as i understand, filters can only filter by spec-name (`describe('spec-name')`), and fine-grained control is possible with [test.only](https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest) – Mila Nautikus Mar 31 '22 at 11:57