Skip to content

Commit 651f812

Browse files
authored
Restore test env (#1563)
1 parent 8ca372d commit 651f812

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
- postcss-next is replaced with postcss-preset-env
1212
- babel@7
1313

14+
## Fixed
15+
16+
- Bring back test env [#1563](https://github.com/rails/webpacker/pull/1563)
17+
1418

1519
## [4.0.0-pre.2] - 2018-04-2
1620

lib/install/config/webpack/test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
2+
3+
const environment = require('./environment')
4+
5+
module.exports = environment.toWebpackConfig()

package/__tests__/production.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ describe('Production environment', () => {
1313

1414
test('should use production config and environment', () => {
1515
process.env.RAILS_ENV = 'production'
16-
const { environment } = require('../index')
16+
process.env.NODE_ENV = 'production'
1717

18+
const { environment } = require('../index')
1819
const config = environment.toWebpackConfig()
20+
1921
expect(config.output.path).toEqual(resolve('public', 'packs'))
2022
expect(config.output.publicPath).toEqual('/packs/')
2123
expect(config).toMatchObject({

package/__tests__/staging.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ describe('Custom environment', () => {
1111
describe('toWebpackConfig', () => {
1212
beforeEach(() => jest.resetModules())
1313

14-
test('should use staging config and production environment', () => {
14+
test('should use staging config and default production environment', () => {
1515
process.env.RAILS_ENV = 'staging'
16-
const { environment } = require('../index')
16+
delete process.env.NODE_ENV
1717

18+
const { environment } = require('../index')
1819
const config = environment.toWebpackConfig()
20+
1921
expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
2022
expect(config.output.publicPath).toEqual('/packs-staging/')
2123
expect(config).toMatchObject({

package/__tests__/test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* test expect, describe, afterAll, beforeEach */
2+
3+
const { resolve } = require('path')
4+
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5+
6+
chdirTestApp()
7+
8+
describe('Test environment', () => {
9+
afterAll(chdirCwd)
10+
11+
describe('toWebpackConfig', () => {
12+
beforeEach(() => jest.resetModules())
13+
14+
test('should use test config and production environment', () => {
15+
process.env.RAILS_ENV = 'test'
16+
process.env.NODE_ENV = 'test'
17+
18+
const { environment } = require('../index')
19+
const config = environment.toWebpackConfig()
20+
21+
expect(config.output.path).toEqual(resolve('public', 'packs-test'))
22+
expect(config.output.publicPath).toEqual('/packs-test/')
23+
expect(config.devServer).toEqual(undefined)
24+
})
25+
})
26+
})

package/env.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { resolve } = require('path')
22
const { safeLoad } = require('js-yaml')
33
const { readFileSync } = require('fs')
44

5-
const NODE_ENVIRONMENTS = ['development', 'production']
5+
const NODE_ENVIRONMENTS = ['development', 'production', 'test']
66
const DEFAULT = 'production'
77
const configPath = resolve('config', 'webpacker.yml')
88

@@ -11,7 +11,7 @@ const nodeEnv = process.env.NODE_ENV
1111

1212
const config = safeLoad(readFileSync(configPath), 'utf8')
1313
const availableEnvironments = Object.keys(config).join('|')
14-
const regex = new RegExp("^(" + availableEnvironments + ")$", 'g')
14+
const regex = new RegExp(`^(${availableEnvironments})$`, 'g')
1515

1616
module.exports = {
1717
railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,

package/environments/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Base = require('./base')
2+
3+
module.exports = class extends Base {}

0 commit comments

Comments
 (0)