Ole
Ole

Reputation: 47008

Test a single file with Jest and TypeScript

How to do test run a single file with Jest and TypeScript? The file I'm trying to run is called des.spec.ts.

If I cd into the directory containing the file and run:

npx jest -t 'des.spec.ts'

Jest does this for about a minute each time:

[..................] fetchMetaData ....

And then does not run the test.

I have the following config:

module.exports = {
    "roots": [
      "<rootDir>/src/lib/"
    ],
    testMatch: [
      "**/__tests__/**/*.+(ts|tsx|js)",
      "**/?(*.)+(spec|test).+(ts|tsx|js)"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "ts-jest"
    }
  }

Thoughts?

Upvotes: 1

Views: 6355

Answers (1)

Lin Du
Lin Du

Reputation: 102327

npm script of package.json:

"scripts": {
  "test": "jest"
}

jest.config.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'enzyme',
  setupFilesAfterEnv: [
    'jest-enzyme',
    './jest.setup.js',
  ],
  testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
  verbose: true,
};

In order to run test cases for single file, just specify an absolute path of test file after npm t --:

☁  react-apollo-graphql-starter-kit [master] npm t -- /Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/61928263/index.test.jsx

> [email protected] test /Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit
> jest "/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/61928263/index.test.jsx"

 PASS  stackoverflow/61928263/index.test.jsx
  61928263
    ✓ should pass without using mock store (34ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.648s, estimated 9s
Ran all test suites matching /\/Users\/ldu020\/workspace\/github.com\/mrdulin\/react-apollo-graphql-starter-kit\/stackoverflow\/61928263\/index.test.jsx/i.

test with coverage:

☁  react-apollo-graphql-starter-kit [master] npm t -- --coverage /Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/61928263/index.test.jsx

> [email protected] test /Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit
> jest "--coverage" "/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/61928263/index.test.jsx"

 PASS  stackoverflow/61928263/index.test.jsx
  61928263
    ✓ should pass without using mock store (35ms)

--------------------|---------|----------|---------|---------|-------------------
File                | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------------|---------|----------|---------|---------|-------------------
All files           |     100 |      100 |     100 |     100 |                   
 LoadingMessage.jsx |     100 |      100 |     100 |     100 |                   
 index.jsx          |     100 |      100 |     100 |     100 |                   
 user.actions.ts    |     100 |      100 |     100 |     100 |                   
--------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.453s, estimated 4s
Ran all test suites matching /\/Users\/ldu020\/workspace\/github.com\/mrdulin\/react-apollo-graphql-starter-kit\/stackoverflow\/61928263\/index.test.jsx/i.

Upvotes: 1

Related Questions