Ich entwickle eine Web-App bei React/Redux/Webpack und fange jetzt an, Tests mit Mocha zu integrieren.Verwendung von Webpack-Aliases in Mocha-Tests
Ich folgte the instructions for writing tests in the Redux documentation, aber jetzt habe ich ein Problem mit meinem Webpack-Aliase.
Nehmen wir zum Beispiel einen Blick auf die Einfuhren Abschnitt dieser Test für einen meiner Aktion Schöpfer:
import expect from 'expect' // resolves without an issue
import * as actions from 'actions/app'; // can't resolve this alias
import * as types from 'constants/actionTypes'; // can't resolve this alias
describe('Actions',() => {
describe('app',() => {
it('should create an action with a successful connection',() => {
const host = '***************',
port = ****,
db = '******',
user = '*********',
pass = '******';
const action = actions.createConnection(host, port, db, user, pass);
const expectedAction = {
type: types.CREATE_CONNECTION,
status: 'success',
payload: { host, port, database, username }
};
expect(action).toEqual(expectedAction);
});
});
});
Da die Kommentare deuten darauf hin, Mokka ist nicht in der Lage meine Import-Anweisungen zu lösen, wenn sie verweisen Alias-Abhängigkeiten.
Weil ich noch neu in webpack bin, hier meine webpack.config.js
:
module.exports = {
devtool: 'eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
resolve: {
extensions : ['', '.js', '.jsx'],
alias: {
actions: path.resolve(__dirname, 'src', 'actions'),
constants: path.resolve(__dirname, 'src', 'constants'),
/* more aliases */
}
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
exclude: /node_modules/,
include: __dirname
}]
}
};
Auch ich bin mit dem Befehl npm test
Mokka zu laufen, hier ist das Skript, das ich in meinem package.json
bin mit.
{
"scripts": {
"test": "mocha ./src/**/test/spec.js --compilers js:babel-core/register --recursive"
}
}
Also hier ist, wo ich stecken bleibe. Ich muss die Aliase von Webpack in Mocha einschließen, wenn es ausgeführt wird.
ich das gleiche Problem haben, haben Sie gefunden, einen Weg, dies zu tun? –
Ja, habe gerade meine Antwort gepostet. YMMV –