vue-cli增加测试环境

vue-vli自带有开发和正式两个环境,但是我们有时是需要添加测试环境的

1.增加测试环境

1. 在package.json中添加test命令

"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "lint": "eslint --ext .js,.vue src",
    "build": "node build/build.js",
    "test": "node build/test.js"
  },

2. 修改config/index

添加:testBuild测试环境
(和build对象基本一样,只是更改了打包后的文件为dist-test文件)

// 测试环境
  testBuild: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist-test/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist-test'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }

3.添加test.env.js

在config文件夹下添加test.env.js

'use strict'
module.exports = {
  NODE_ENV: '"test"',
  API_HOST: '"http://127.0.0.1:3000/"'
}

API_HOST为测试环境的接口路径
可以在开发和正式环境都加上 API_HOST,然后根据 process.env.NODE_ENV
的不同的环境,进而得到不同的 process.env.API_HOST

4.添加test.js

在buid文件夹下添加test.js
可以直接将build复制过来,只是将

  • 环境变为test
process.env.NODE_ENV = 'test'
  • 引入文件变为webpack.test.conf.js
const webpackConfig = require('./webpack.test.conf')
  • 将build对象改为测试对象 testBuild

5.添加webpack.test.conf.js

在buid文件夹下添加webpack.test.conf.js
可以直接将webpack.prod.conf.js复制过来,只是将

  • 引入文件变为config/test.env.js
const env = require('../config/test.env')
  • 将build对象改为测试对象 testBuild

猜你喜欢

转载自blog.csdn.net/haochangdi123/article/details/80898506