Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Jest和Typescript测试单个文件_Node.js_Typescript_Jestjs_Ts Jest - Fatal编程技术网

Node.js 使用Jest和Typescript测试单个文件

Node.js 使用Jest和Typescript测试单个文件,node.js,typescript,jestjs,ts-jest,Node.js,Typescript,Jestjs,Ts Jest,如何使用Jest和Typescript测试运行单个文件?我试图运行的文件名为des.spec.ts 如果我cd进入包含该文件的目录并运行: npx jest -t 'des.spec.ts' Jest每次这样做大约一分钟: [……]获取元数据 然后不运行测试 我有以下配置: module.exports = { "roots": [ "<rootDir>/src/lib/" ], testMatch: [ "**/__tests__/

如何使用Jest和Typescript测试运行单个文件?我试图运行的文件名为
des.spec.ts

如果我
cd
进入包含该文件的目录并运行:

npx jest -t 'des.spec.ts'
Jest每次这样做大约一分钟:

[……]获取元数据

然后不运行测试

我有以下配置:

module.exports = {
    "roots": [
      "<rootDir>/src/lib/"
    ],
    testMatch: [
      "**/__tests__/**/*.+(ts|tsx|js)",
      "**/?(*.)+(spec|test).+(ts|tsx|js)"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "ts-jest"
    }
  }
module.exports={
“根”:[
“/src/lib/”
],
测试匹配:[
“***/\uuuuuuuuuuu测试”+(ts | tsx | js)”,
“***/?(*)+(规范测试)。+(ts | tsx | js)”
],
“转变”:{
“^.+\.(ts | tsx)$”:“ts笑话”
}
}

想法?

npm脚本的
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,
};
为了为单个文件运行测试用例,只需在
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

> react-apollo-graphql-starter-kit@1.0.0 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.
覆盖率测试:

☁  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

> react-apollo-graphql-starter-kit@1.0.0 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.

这对Typescript有用吗?我在
jest.config
中没有看到
ts jest
转换,所以我很好奇它是如何工作的?