开玩笑没有测试

Ken*_*AUD 18 javascript unit-testing node.js jestjs

运行docker mhart/alpine-node:8在macOS上

nodejs(6.10.3-r0)(18/18)纱线0.24.6 jest 20.0.4

但是,在运行代码时,我有一个__tests __/index.test.js文件

node_modules/.bin/jest --watchAll 我得到以下输出

未检测到
/usr/src/app
5个文件已检查.
testMatch:/ __ tests __ / /*.js?(x),**/?(*.)(spec|test).js?(x) - 1匹配
testPathIgnorePatterns:/ node_modules /,/ src,src - 0匹配
模式:"" - 0场比赛

我已经重新安装了包裹号码,但无济于事.

小智 15

在 package.json 中有一个用于查找测试文件的模式。在此模式中,您可以根据文件位置更改它或将其设为全局模式。

我编写了一个测试,不在特定文件夹中并遵循命名约定*.test.js并进行更改testMatch

"testMatch": [
      "<rootDir>/src/**/*.(test).{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 13

如果您有如下文件结构

myFolder
?   myFile1.js
?   myFile2.js
?   ...
?   
????__tests__
        myFile1.spec.js
        myFile2.spec.js
        ...
Run Code Online (Sandbox Code Playgroud)

那么您需要具有jest.config.js以下testMatch属性模式:

testMatch: ['**/__tests__/*.js?(x)'],
Run Code Online (Sandbox Code Playgroud)

简单的例子jest.config.js

const jestConfig = {
  verbose: true,
  testURL: "http://localhost/",
  'transform': {
    '^.+\\.jsx?$': 'babel-jest',
  },
  testMatch: ['**/__tests__/*.js?(x)'],
}

module.exports = jestConfig
Run Code Online (Sandbox Code Playgroud)


Zac*_*ith 10

你的输出说testMatch1 match,可能是你的__tests__/index.test.js文件.看起来你testPathIgnorePatterns的导致该测试套件被忽略了. No tests found In /usr/src/app说Jest正在寻找测试/usr/src/app,并testPathIgnorePatterns: /node_modules/,/src,src说Jest忽略了/src目录中的文件.

__tests__/index.test.js如果它位于/ src目录之外,则指向Jest以查看文件的位置,或者不要testPathIgnorePatterns忽略/ src目录.


Kar*_*nga 7

您可以尝试jest不带任何参数运行。需要注意的关键一点是测试文件名必须遵循约定*.test.js*.spec.js.


小智 5

如果您想运行测试文件夹中的所有测试,您只需执行以下操作 jest __tests__ --watch