簡體   English   中英

業力測試運行程序未獲取組件子目錄中的酶測試文件

[英]Karma test runner not picking up Enzyme test files within a component's sub-directory

我正在使用Chai的Expect庫使用Enzyme(測試實用程序)測試我的React項目,並使用Karma(測試運行程序)運行測試。

我的工作項目目錄結構如下:app->組件->按鈕-> __test-> test.jsx

Karma可以輕松找到上述測試並成功運行。

有問題的目錄結構如下:app->組件->列表-> ListHeader-> __test-> test.jsx

如果測試位於組件的子目錄中,則Karma不會接受這些測試。

我已經設置了autoWatch=true ,在karma.config添加了路徑,並在karma.config尋找線索/提示/解決方案,但是沒有運氣。 如果有幫助,我已經添加了karma.config文件。

感謝您抽出寶貴的時間來研究這個問題!

const webpack = require('webpack')
const argv = require('yargs').argv
const path = require('path')

const aliases = require('./webpack/alias')
const config = require('./webpack/settings')
const loaders = require('./webpack/loaders')
const preloaders = require('./webpack/preloaders')

const karmaConfig = (config) => {
  config.set({
    // only use PhantomJS for our 'test' browser
    browsers: ['PhantomJS'],

    // just run once by default unless --watch flag is passed
    singleRun: !argv.watch,

    // Enable or disable colors in the output (reporters and logs)
    colors: true,

    // which karma frameworks do we want integrated
    frameworks: ['mocha', 'chai', 'sinon'],

    // displays tests in a nice readable format
    reporters: ['spec'],

    // include some polyfills for babel and phantomjs
    files: [
      './node_modules/babel-polyfill/dist/polyfill.js',
      './node_modules/phantomjs-polyfill/bind-polyfill.js',
      './app/**/**/__test/test.jsx', // specify files to watch for tests
      './app/**/**/__test/test.js', // specify files to watch for tests
      './app/**/**/**/__test/test.jsx', // specify files to watch for tests
      './app/**/**/**/__test/test.js' // specify files to watch for tests
    ],
    preprocessors: {
      // these files we want to be precompiled with webpack
      // also run tests throug sourcemap for easier debugging
      ['./app/**/**/__test/*']: ['webpack', 'sourcemap']
    },
    // A lot of people will reuse the same webpack config that they use
    // in development for karma but remove any production plugins like UglifyJS etc.
    // I chose to just re-write the config so readers can see what it needs to have
    webpack: {
       devtool: 'inline-source-map',
       resolve: {
        // allow us to import components in tests like:
        // import Example from 'components/Example';
        root: path.resolve(__dirname, './app'),

        // allow us to avoid including extension name
        extensions: ['', '.js', '.jsx'],

        // required for enzyme to work properly
        alias: aliases
      },

      module: {
        // don't run babel-loader through the sinon module
        noParse: [
          /sinon(\\|\/)pkg(\\|\/)sinon\.js/
        ],
        // run babel loader for our tests
        // preLoaders: preloaders,
        loaders: loaders,
      },
      // required for enzyme to work properly
      externals: {
        'jsdom': 'window',
        'cheerio': 'window',
        'react-dom/server': 'window',
        'text-encoding': 'window',
        'react/addons': true,
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': 'window'
      },
    },
    webpackMiddleware: {
      noInfo: true,
      stats: {
        colors: true,
        chunks: false
      }
    },
    // tell karma all the plugins we're going to be using to prevent warnings
    plugins: [
      'karma-*'
    ],

    autoWatch: true
  })
}

module.exports = karmaConfig

這是通配的一個已知問題:

最后,主要原因似乎是如果我在karma.config中使用了glob模式,該模式在比第二級更深的文件夾中包含雙星號(* /)。

然后設法通過直接使用node-glob復制錯誤,以生成相同的錯誤。

最后,使用glob.sync路由並自己構建URL數組可以解決此問題。 希望這可以為作者提供一些線索,說明他可以做什么來解決。 干杯

使用jsdom和glob作為替代。 例如:

const glob = require('glob');
const jsdom = require('jsdom');
const chai = require('chai');

global.document = jsdom.jsdom();
global.window = document.defaultView;

global.navigator = window.navigator || {};
global.Node = window.Node;
global.addEventListener = window.addEventListener;
global.MouseEvent = window.MouseEvent;
global.KeyboardEvent = window.KeyboardEvent;
global.Event = window.Event;
global.btoa = window.btoa;
global.FormData = window.FormData;
global.FileReader = window.FileReader;
global.File = window.File;

window.beforeEach = global.beforeEach;
window.afterEach = global.afterEach;
window.before = global.before;
window.after = global.after;
window.mocha = true;

參考文獻

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM