Skip to content

Commit

Permalink
fix(test): inject missing webpack config into tests
Browse files Browse the repository at this point in the history
This means NODE_ENV and AEGIR_* env variables are now
properly injected
  • Loading branch information
dignifiedquire committed Dec 18, 2017
1 parent 41efd8d commit bb4c0e4
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 37 deletions.
21 changes: 19 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
sudo: false
language: node_js
node_js:
- stable

matrix:
include:
- node_js: 8
env: CXX=g++-4.8

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

script:
- yarn lint
- yarn test
- yarn coverage -- -u -p codecov coveralls

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"scripts": {
"lint": "node cli.js lint",
"test:node": "node cli.js test -t node",
"test:browser": "node cli.js test -t browser webworker --files test/browser.spec.js",
"test:node": "AEGIR_TEST=hello node cli.js test -t node",
"test:browser": "AEGIR_TEST=hello node cli.js test -t browser webworker --files test/browser.spec.js",
"test": "npm run test:node && npm run test:browser",
"coverage": "node cli.js coverage -t node",
"watch": "node cli.js test -t node --watch",
Expand Down
3 changes: 0 additions & 3 deletions src/config/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const webpackConfig = require('./webpack')

let concurrency = 1
let reporters = ['mocha-own']

Expand All @@ -24,7 +22,6 @@ module.exports = function (config) {
preprocessors: {
'test/**/*.js': ['webpack', 'sourcemap']
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
Expand Down
6 changes: 4 additions & 2 deletions src/config/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const utils = require('../../utils')
const base = require('./base')
const user = require('../user')()

function webpackConfig () {
function webpackConfig (env) {
env = env || 'production'

return utils.getPkg().then((pkg) => {
const libraryName = utils.getLibraryName(pkg.name)
const userConfig = user.webpack
Expand All @@ -25,7 +27,7 @@ function webpackConfig () {
path: utils.getPathToDist()
},
plugins: [
new webpack.DefinePlugin(utils.getEnv('production').stringified)
new webpack.DefinePlugin(utils.getEnv(env).stringified)
]
}, userConfig)
})
Expand Down
59 changes: 33 additions & 26 deletions src/test/browser-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const _ = require('lodash')

const user = require('../config/user')
const webpackConfig = require('../config/webpack')

const CONFIG_FILE = path.join(__dirname, '..', 'config', 'karma.conf.js')

Expand Down Expand Up @@ -56,34 +57,40 @@ function getConfig (isWebworker, ctx) {
const userKarma = user().karma
const userFiles = userKarma.files || []

return _.defaultsDeep({
files: ctxFiles.concat(fixtureFiles).concat(userFiles)
}, userKarma, {
configFile: CONFIG_FILE,
singleRun: !ctx.watch,
watch: ctx.watch,
frameworks: isWebworker ? ['mocha-webworker'] : ['mocha'],
logLevel: ctx.verbose ? 'debug' : 'error',
client: getClient(isWebworker, ctx),
mochaOwnReporter: {
reporter: 'spec'
},
junitReporter: {
outputFile: isWebworker ? 'junit-report-webworker.xml' : 'junit-report-browser.xml'
},
browserNoActivityTimeout: 50 * 1000,
customLaunchers: {
ChromeCustom: {
base: 'Chrome',
flags: [!ctx.cors ? '--disable-web-security' : '']
return webpackConfig('test').then((webpack) => {
// no need for entry
webpack.entry = ''

return _.defaultsDeep({
files: ctxFiles.concat(fixtureFiles).concat(userFiles)
}, userKarma, {
configFile: CONFIG_FILE,
singleRun: !ctx.watch,
watch: ctx.watch,
frameworks: isWebworker ? ['mocha-webworker'] : ['mocha'],
logLevel: ctx.verbose ? 'debug' : 'error',
client: getClient(isWebworker, ctx),
mochaOwnReporter: {
reporter: 'spec'
},
junitReporter: {
outputFile: isWebworker ? 'junit-report-webworker.xml' : 'junit-report-browser.xml'
},
FirefoxCustom: {
base: 'Firefox',
prefs: {
'security.fileuri.strict_origin_policy': ctx.cors
browserNoActivityTimeout: 50 * 1000,
customLaunchers: {
ChromeCustom: {
base: 'Chrome',
flags: [!ctx.cors ? '--disable-web-security' : '']
},
FirefoxCustom: {
base: 'Firefox',
prefs: {
'security.fileuri.strict_origin_policy': ctx.cors
}
}
}
}
},
webpack: webpack
})
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function testBrowser (isWebworker) {

return (ctx) => {
return preHook(ctx)
.then(() => karma(getConfig(isWebworker, ctx)))
.then(() => getConfig(isWebworker, ctx))
.then(karma)
.then(() => postHook(ctx))
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ exports.getListrConfig = () => {
*/
exports.getEnv = (env) => {
const PREFIX = /^AEGIR_/i
let NODE_ENV = env || 'development'
if (JSON.stringify(process.env.NODE_ENV) !== JSON.stringify(undefined) && process.env.NODE_ENV) {
NODE_ENV = process.env.NODE_ENV
}

const raw = Object.keys(process.env)
.filter((key) => PREFIX.test(key))
.reduce((env, key) => {
Expand All @@ -115,7 +120,7 @@ exports.getEnv = (env) => {
return env
}
}, {
NODE_ENV: process.env.NODE_ENV || env || 'development'
NODE_ENV: NODE_ENV
})

const stringifed = {
Expand Down
5 changes: 5 additions & 0 deletions test/browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ describe('browser', () => {
const myFixture = loadFixture(__dirname, 'fixtures/test.txt')
expect(myFixture.toString()).to.be.eql('Hello Fixture\n')
})

it('process.env', () => {
expect(process.env.NODE_ENV).to.eql('test')
expect(process.env.AEGIR_TEST).to.eql('hello')
})
})

0 comments on commit bb4c0e4

Please sign in to comment.