2

エクスポート/インポートおよびカルマ/ WebPACKを持つ/ constのES6を使用してみましょう - トップ

export { test }; 

const test = (str) => { 
    return str; 
}; 
で輸出を
import { test } from './func'; 

describe('func',() => { 
    describe('func',() => { 
     it('should return the same string',() => { 
      expect(test('hello world')).to.equal('hello world'); 
     }); 
    }); 
}); 

テスト機能があるため、巻上の定義されていません、私は考えます。 私がしなければ理由:

const test = (str) => { 
    return str; 
}; 

export { test }; 

テスト作品。

しかし、簡単に参照できるように、ファイルの先頭にエクスポートを保存したいと思います。

これを実現する方法はありますか?

マイkarma.conf.js:package.jsonの

const webpackConfig = require('./webpack.config'); 
const fileGlob = 'src/**/*.test.js'; 

module.exports = (config) => { 
    config.set({ 
     basePath: '', 
     frameworks: ['mocha', 'chai'], 
     files: [fileGlob], 
     preprocessors: { 
      [fileGlob]: ['webpack'] 
     }, 
     webpack: webpackConfig, 
     webpackMiddleware: {noInfo: true}, 
     reporters: ['progress', 'mocha'], 
     port: 9876, 
     colors: true, 
     logLevel: config.LOG_INFO, 
     autoWatch: false, 
     browsers: ['Firefox'], 
     singleRun: true, 
     concurrency: Infinity, 
    }); 
}; 

や関連部品:

"devDependencies": { 
    "webpack": "^3.5.5", 

    "babel-core": "^6.26.0", 

    "babel-loader": "^7.1.2", 
    "babel-plugin-add-module-exports": "^0.2.1", 
    "babel-preset-es2015": "^6.24.1", 

    "chai": "^4.1.1", 
    "mocha": "^3.5.0", 
    "karma": "^1.7.0", 
    "karma-chai": "^0.1.0", 
    "karma-mocha": "^1.3.0", 

    "karma-webpack": "^2.0.4", 

    }, 

答えて

4

ESモジュールのインポートがモジュール・エクスポートの状態を反映しています。 const宣言が吊り上げられていないにもかかわらず、export { test }が評価されたときには一時的な不感地帯にありますが、モジュールがインポートされたときには、すでにtestの実績値が反映されています。

モジュールの蒸散による誤った動作が原因で問題が発生する可能性があります。 Babel doesn't implement module exports correctly、これはundefinedになります。

here(ESモジュール、つまり最新のChromeをサポートするブラウザで利用可能)のように、モジュールのエクスポートは意図した通りに機能します。

TypeScript handles exports as intendedもあります。

既存の実装とのコード互換性を持たせるために、それは次のようになります。

export const test = (str) => { 
    return str; 
}; 

または:

const test = (str) => { 
    return str; 
}; 

export { test }; 

の両方が、特にこの問題が原因の輸出を、行うには、従来の方法です。モジュールの終わりのエクスポートは、CommonJSモジュールの使用に起因するコーディング習慣に従います。

+1

確かにバベルのバグです。 – loganfsmyth

関連する問題