Skip to content

Commit

Permalink
Restore test env (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravtiwari authored Jun 10, 2018
1 parent 8ca372d commit 651f812
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/install/config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()
4 changes: 3 additions & 1 deletion package/__tests__/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 4 additions & 2 deletions package/__tests__/staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
26 changes: 26 additions & 0 deletions package/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
4 changes: 2 additions & 2 deletions package/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions package/environments/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Base = require('./base')

module.exports = class extends Base {}

0 comments on commit 651f812

Please sign in to comment.