2016-11-21 29 views
5

在我使用Jest測試客戶端代碼(testEnvironment:'jsdom')和服務器端代碼(testEnvironment:'node')以及爲客戶端和服務器端收集代碼覆蓋範圍的節點應用程序內。無論如何擴展一個jest配置文件?

目前我正在使用4 Jest配置文件與大量的冗餘配置來實現這一點。

客戶

{ 
    "bail": true, 
    "verbose": true, 
    "notify": true, 
    "scriptPreprocessor": "./node_modules/babel-jest", 
    "testPathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build" 
    ], 
    "testRegex": "\\.test\\.js" 
} 

客戶覆蓋

{ 
    "bail": true, 
    "verbose": true, 
    "notify": true, 
    "scriptPreprocessor": "./node_modules/babel-jest", 
    "testPathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build" 
    ], 
    "testRegex": "\\.test\\.js", 
    "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"], 
    "collectCoverage": true, 
    "coverageDirectory": "./coverage", 
    "coveragePathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build", 
    "./test" 
    ], 
    "coverageThreshold": { 
    "global": { 
     "branches": 100, 
     "functions": 100, 
     "lines": 100, 
     "statements": 100 
    } 
    } 
} 

服務器

{ 
    "bail": true, 
    "verbose": true, 
    "notify": true, 
    "scriptPreprocessor": "./node_modules/babel-jest", 
    "testPathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build" 
    ], 
    "testRegex": "\\.test\\.js", 
    "testEnvironment": "node" 
} 

服務器覆蓋

{ 
    "bail": true, 
    "verbose": true, 
    "notify": true, 
    "scriptPreprocessor": "./node_modules/babel-jest", 
    "testPathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build" 
    ], 
    "testRegex": "\\.test\\.js", 
    "testEnvironment": "node", 
    "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"], 
    "collectCoverage": true, 
    "coverageDirectory": "./coverage", 
    "coveragePathIgnorePatterns": [ 
    "./node_modules", 
    "./coverage", 
    "./dist", 
    "./build", 
    "./test" 
    ], 
    "coverageThreshold": { 
    "global": { 
     "branches": 100, 
     "functions": 100, 
     "lines": 100, 
     "statements": 100 
    } 
    } 
} 

如何在不重複我的配置4次的情況下實現此目標?我查看了preset配置選項。使用我必須爲每個配置創建一個單獨的包。這是推薦的方式嗎?

回答

1

是的,既然Jest v20可以將配置定義爲JS文件,並使用它來共享類似配置的公共部分。 Docs on configuring Jest

默認情況下玩笑擡頭爲:

  • jest.config.js
  • "jest"條目package.json

...並把父目錄作爲rootDir

此外請務必查看projects選項,這可以更輕鬆地在monorepos中運行Jest(例如,在一個代碼庫中的客戶端+服務器代碼)。查看此答案以供參考:Testing two environments with jest

相關問題