vuejs / babel-preset-vue-app

Babel preset for Vue app.
MIT License
124 stars 25 forks source link

AVA sets NODE_ENV=test, which breaks tests #12

Open ydnar opened 7 years ago

ydnar commented 7 years ago

We use AVA for testing our Vue app. Recently they released version 0.23 which sets NODE_ENV=test if not already set (see avajs/ava#1470 and avajs/ava#1523 for additional context).

This change blew up our test builds. We traced it to this package (babel-prest-vue-app), which overrides targets if NODE_ENV=test, with no way to circumvent this via configuration. This breaks client-side code which is intended to run in a browser (we use Puppeteer to control chrome for integration tests).

We’re currently using a workaround to explicitly set process.env.BABEL_ENV = 'production' before building. See example code below.

Request: do not override targets if NODE_ENV == test.


import test from 'ava'
import { Nuxt, Builder } from 'nuxt'
import { resolve } from 'path'

export var nuxt = null
export var host = null
export var port = null
export var baseURL = null

// Init Nuxt.js and start listening at http://localhost:4000
test.before('Init Nuxt.js', async t => {
  // Explicitly override BABEL_ENV='production' here because
  // ava sets NODE_ENV='test' which breaks babel-preset-vue-app,
  // which in turn breaks uglify-js, which breaks our build.
  process.env.BABEL_ENV = 'production'
  const rootDir = resolve(__dirname, '../..')
  let config = {}
  config = require(resolve(rootDir, 'nuxt.config.js'))
  config.rootDir = rootDir // project folder
  config.dev = false // production build
  nuxt = new Nuxt(config)
  const builder = new Builder(nuxt)
  await builder.build()
  host = process.env.HOST || 'localhost'
  port = process.env.PORT || 4000
  nuxt.listen(port, host)
  baseURL = `http://${host}:${port}`
})

// Teardown
test.after.always('Shutdown Nuxt.js server', async t => {
  nuxt.close()
})
egoist commented 7 years ago

well I think it actually should set the target to current since it prevents from doing unnecessary transpiling during testing.

ydnar commented 7 years ago

We need the target(s) preserved for integration tests, which run in both Node and the browser. Transpiling is necessary.

egoist commented 7 years ago

I'm not entirely sure so at least I think it's fine to allow overriding targets in test env. https://github.com/vuejs/babel-preset-vue-app/pull/13

ydnar commented 6 years ago

Thanks!