最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

无法找到模块“模块”

IT培训 admin 2浏览 0评论

无法找到模块“模块”

我想用我的噶(+的WebPack打字稿)单元测试使用rewire。我的单元测试都写在打字稿,用的WebPack捆绑,然后用噶运行。我不断收到错误:

PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  Error: Cannot find module "module"
  at src/myTest.spec.ts:187

我看着重新布线的代码,问题来自于线

var Module = require("module"),

我知道有一个Webpack Rewire插件,但是当我使用它,我有同样的问题作为already reported in an issue。

不使用rewire所有我的测试中正常工作。

这里是我的测试文件:

import rewire = require("rewire");
const decorators = rewire("./decorators");

describe('something', () => {
    it('should do something', () => {
        decorators.__set__('Test', () => 'hello');
        // In know this is pointless, but it's just to make sure that rewire works.
        expect(decorators.Test).toBe('hello');
    });
});

这是我的WebPack配置:

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');

var nodeModules = {};
fs.readdirSync('node_modules')
    .filter(function (x) {
        return ['.bin'].indexOf(x) === -1;
    })
    .forEach(function (mod) {
        nodeModules[mod] = 'commonjs ' + mod;
    });

// Our Webpack Defaults
var webpackConfig = {
    entry: './src/index.ts',
    target: 'node',
    module: {
        loaders: [
            {test: /\.ts$/, loaders: ['ts-loader'], exclude: /node_modules/}
        ]
    },
    plugins: [
        new webpack.BannerPlugin({banner: 'require("source-map-support").install();', raw: true, entryOnly: false}),
        new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
        new webpack.optimize.AggressiveMergingPlugin(),
    ],
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'index.js',
        sourceMapFilename: 'index.map'
    },
    externals: nodeModules,
    devtool: 'source-map',
    resolve: {
        extensions: ['.ts', '.js']
    }
};

module.exports = webpackConfig;

这里是我的karma.conf.js我的(相关)部分:

frameworks: ['jasmine'],
files: [
    'src/**/*.spec.ts',
    'test/**/*.ts'
],
exclude: [],
webpack: {
    devtool: webpackConfig.devtool,
    module: webpackConfig.module,
    resolve: webpackConfig.resolve,
},
webpackMiddleware: {
    quiet: true,
    stats: {
        colors: true
    }
},
preprocessors: {
    'src/**/*.spec.ts': ['webpack', 'sourcemap'],
    'test/**/*.ts': ['webpack', 'sourcemap']
},
回答如下:

我觉得你写的这个毛病进口的ReWire =要求(“重新连接”);

它应该是

import rewire from 'rewire' 

对于ES6或

const rewire = require('rewire')

对于ES5

无法找到模块“模块”

我想用我的噶(+的WebPack打字稿)单元测试使用rewire。我的单元测试都写在打字稿,用的WebPack捆绑,然后用噶运行。我不断收到错误:

PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  Error: Cannot find module "module"
  at src/myTest.spec.ts:187

我看着重新布线的代码,问题来自于线

var Module = require("module"),

我知道有一个Webpack Rewire插件,但是当我使用它,我有同样的问题作为already reported in an issue。

不使用rewire所有我的测试中正常工作。

这里是我的测试文件:

import rewire = require("rewire");
const decorators = rewire("./decorators");

describe('something', () => {
    it('should do something', () => {
        decorators.__set__('Test', () => 'hello');
        // In know this is pointless, but it's just to make sure that rewire works.
        expect(decorators.Test).toBe('hello');
    });
});

这是我的WebPack配置:

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');

var nodeModules = {};
fs.readdirSync('node_modules')
    .filter(function (x) {
        return ['.bin'].indexOf(x) === -1;
    })
    .forEach(function (mod) {
        nodeModules[mod] = 'commonjs ' + mod;
    });

// Our Webpack Defaults
var webpackConfig = {
    entry: './src/index.ts',
    target: 'node',
    module: {
        loaders: [
            {test: /\.ts$/, loaders: ['ts-loader'], exclude: /node_modules/}
        ]
    },
    plugins: [
        new webpack.BannerPlugin({banner: 'require("source-map-support").install();', raw: true, entryOnly: false}),
        new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
        new webpack.optimize.AggressiveMergingPlugin(),
    ],
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'index.js',
        sourceMapFilename: 'index.map'
    },
    externals: nodeModules,
    devtool: 'source-map',
    resolve: {
        extensions: ['.ts', '.js']
    }
};

module.exports = webpackConfig;

这里是我的karma.conf.js我的(相关)部分:

frameworks: ['jasmine'],
files: [
    'src/**/*.spec.ts',
    'test/**/*.ts'
],
exclude: [],
webpack: {
    devtool: webpackConfig.devtool,
    module: webpackConfig.module,
    resolve: webpackConfig.resolve,
},
webpackMiddleware: {
    quiet: true,
    stats: {
        colors: true
    }
},
preprocessors: {
    'src/**/*.spec.ts': ['webpack', 'sourcemap'],
    'test/**/*.ts': ['webpack', 'sourcemap']
},
回答如下:

我觉得你写的这个毛病进口的ReWire =要求(“重新连接”);

它应该是

import rewire from 'rewire' 

对于ES6或

const rewire = require('rewire')

对于ES5

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论