From 651f812886241fc6112c189d99bd01ee92fbb46b Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 10 Jun 2018 19:17:28 +0100 Subject: [PATCH] Restore test env (#1563) --- CHANGELOG.md | 4 ++++ lib/install/config/webpack/test.js | 5 +++++ package/__tests__/production.js | 4 +++- package/__tests__/staging.js | 6 ++++-- package/__tests__/test.js | 26 ++++++++++++++++++++++++++ package/env.js | 4 ++-- package/environments/test.js | 3 +++ 7 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 lib/install/config/webpack/test.js create mode 100644 package/__tests__/test.js create mode 100644 package/environments/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bcfb1d06..adf77539c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ - postcss-next is replaced with postcss-preset-env - babel@7 +## Fixed + + - Bring back test env [#1563](https://github.com/rails/webpacker/pull/1563) + ## [4.0.0-pre.2] - 2018-04-2 diff --git a/lib/install/config/webpack/test.js b/lib/install/config/webpack/test.js new file mode 100644 index 000000000..c5edff94a --- /dev/null +++ b/lib/install/config/webpack/test.js @@ -0,0 +1,5 @@ +process.env.NODE_ENV = process.env.NODE_ENV || 'development' + +const environment = require('./environment') + +module.exports = environment.toWebpackConfig() diff --git a/package/__tests__/production.js b/package/__tests__/production.js index 96f212817..c42821263 100644 --- a/package/__tests__/production.js +++ b/package/__tests__/production.js @@ -13,9 +13,11 @@ describe('Production environment', () => { test('should use production config and environment', () => { process.env.RAILS_ENV = 'production' - const { environment } = require('../index') + process.env.NODE_ENV = 'production' + const { environment } = require('../index') const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs')) expect(config.output.publicPath).toEqual('/packs/') expect(config).toMatchObject({ diff --git a/package/__tests__/staging.js b/package/__tests__/staging.js index 794e61553..131ef9ec0 100644 --- a/package/__tests__/staging.js +++ b/package/__tests__/staging.js @@ -11,11 +11,13 @@ describe('Custom environment', () => { describe('toWebpackConfig', () => { beforeEach(() => jest.resetModules()) - test('should use staging config and production environment', () => { + test('should use staging config and default production environment', () => { process.env.RAILS_ENV = 'staging' - const { environment } = require('../index') + delete process.env.NODE_ENV + const { environment } = require('../index') const config = environment.toWebpackConfig() + expect(config.output.path).toEqual(resolve('public', 'packs-staging')) expect(config.output.publicPath).toEqual('/packs-staging/') expect(config).toMatchObject({ diff --git a/package/__tests__/test.js b/package/__tests__/test.js new file mode 100644 index 000000000..58b69c3b0 --- /dev/null +++ b/package/__tests__/test.js @@ -0,0 +1,26 @@ +/* test expect, describe, afterAll, beforeEach */ + +const { resolve } = require('path') +const { chdirTestApp, chdirCwd } = require('../utils/helpers') + +chdirTestApp() + +describe('Test environment', () => { + afterAll(chdirCwd) + + describe('toWebpackConfig', () => { + beforeEach(() => jest.resetModules()) + + test('should use test config and production environment', () => { + process.env.RAILS_ENV = 'test' + process.env.NODE_ENV = 'test' + + const { environment } = require('../index') + const config = environment.toWebpackConfig() + + expect(config.output.path).toEqual(resolve('public', 'packs-test')) + expect(config.output.publicPath).toEqual('/packs-test/') + expect(config.devServer).toEqual(undefined) + }) + }) +}) diff --git a/package/env.js b/package/env.js index 494922d62..c1c3d454c 100644 --- a/package/env.js +++ b/package/env.js @@ -2,7 +2,7 @@ const { resolve } = require('path') const { safeLoad } = require('js-yaml') const { readFileSync } = require('fs') -const NODE_ENVIRONMENTS = ['development', 'production'] +const NODE_ENVIRONMENTS = ['development', 'production', 'test'] const DEFAULT = 'production' const configPath = resolve('config', 'webpacker.yml') @@ -11,7 +11,7 @@ const nodeEnv = process.env.NODE_ENV const config = safeLoad(readFileSync(configPath), 'utf8') const availableEnvironments = Object.keys(config).join('|') -const regex = new RegExp("^(" + availableEnvironments + ")$", 'g') +const regex = new RegExp(`^(${availableEnvironments})$`, 'g') module.exports = { railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT, diff --git a/package/environments/test.js b/package/environments/test.js new file mode 100644 index 000000000..cb3d14ba9 --- /dev/null +++ b/package/environments/test.js @@ -0,0 +1,3 @@ +const Base = require('./base') + +module.exports = class extends Base {}