-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): set up integration testing (#461)
This will ensure that karma-webpack can run as expected within a live karma server. Fixes #460
- Loading branch information
Showing
8 changed files
with
9,648 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/integration/scenarios/basic-setup/basic-setup.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ScenarioUtils from '../../utils/ScenarioUtils'; | ||
|
||
const path = require('path'); | ||
|
||
describe('A basic karma-webpack setup', () => { | ||
let scenario; | ||
|
||
const TEST_PATH = path.resolve(__dirname, './index.scenario.js'); | ||
|
||
const config = { | ||
frameworks: ['webpack', 'mocha'], | ||
files: [{ pattern: TEST_PATH }], | ||
preprocessors: { [TEST_PATH]: ['webpack'] }, | ||
webpack: {}, | ||
browsers: ['ChromeHeadless'], | ||
// Explicitly turn off reporters so the simulated test results are not confused with the actual results. | ||
reporters: [], | ||
port: 2389, | ||
logLevel: 'OFF', | ||
singleRun: true, | ||
}; | ||
|
||
const plugins = ['karma-webpack', 'karma-chrome-launcher', 'karma-mocha']; | ||
|
||
beforeAll((done) => { | ||
ScenarioUtils.run(config, plugins) | ||
.then((res) => { | ||
scenario = res; | ||
done(); | ||
}) | ||
.catch((err) => { | ||
jest.fail('Karma run has failed with an error', err); | ||
}); | ||
}); | ||
|
||
it('should have three successful test runs', () => { | ||
expect(scenario.success).toBe(3); | ||
}); | ||
|
||
it('should have one failed test run', () => { | ||
expect(scenario.failed).toBe(1); | ||
}); | ||
|
||
it('should complete with no errors', () => { | ||
expect(scenario.error).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require('./js-source-example.js'); | ||
require('./js-test-example.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const AdvancedMath = { | ||
add, | ||
}; | ||
|
||
function add(a, b) { | ||
return a + b; | ||
} | ||
|
||
export default AdvancedMath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import AdvancedMath from './js-source-example'; | ||
|
||
const assert = require('assert'); | ||
|
||
describe('test one example', () => { | ||
it('an example passing test', () => { | ||
assert.strictEqual(1, 1); | ||
}); | ||
}); | ||
|
||
describe('test two example', () => { | ||
it('an example failing test', () => { | ||
assert.strictEqual(1, 0); | ||
}); | ||
}); | ||
|
||
describe('bundled js content', () => { | ||
it('should be able to bundle js content', () => { | ||
assert.strictEqual(!!AdvancedMath.add, true); | ||
}); | ||
it('should be able to interpret bundled js content', () => { | ||
assert.strictEqual(AdvancedMath.add(1, 1), 2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
process.env.CHROME_BIN = require('puppeteer').executablePath(); | ||
const karmaChromeLauncher = require('karma-chrome-launcher'); | ||
const karmaMocha = require('karma-mocha'); | ||
const { Server } = require('karma'); | ||
|
||
const karmaWebpack = require('../../../lib/index'); | ||
|
||
const PLUGINS = { | ||
'karma-webpack': karmaWebpack, | ||
'karma-mocha': karmaMocha, | ||
'karma-chrome-launcher': karmaChromeLauncher, | ||
}; | ||
|
||
function getPlugin(plugin) { | ||
if (PLUGINS[plugin]) return PLUGINS[plugin]; | ||
throw new Error( | ||
`Tried to load an unknown plugin [${plugin}] while running a karma scenario.` | ||
); | ||
} | ||
|
||
process.on('message', ({ config, plugins }) => { | ||
if (config.plugins) { | ||
throw new Error(` | ||
Error: please specify plugins as the second argument of ScenarioUtils.run. \n | ||
available plugins: [${Object.keys(PLUGINS).join(',')}]} | ||
`); | ||
} | ||
|
||
const karmaConfiguration = config; | ||
karmaConfiguration.plugins = plugins.map(getPlugin); | ||
const server = new Server(karmaConfiguration); | ||
// This will run once when the karma server completes running tests. | ||
server.on('run_complete', (browsers, results) => process.send(results)); | ||
server.start(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { fork } = require('child_process'); | ||
const path = require('path'); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const karma = require('karma'); | ||
|
||
const ScenarioUtils = { run }; | ||
|
||
/** | ||
* This allows you to run a karma with a given configuration and list of plugins, | ||
* on completion you will be returned a karma results object. | ||
* | ||
* @param {karma.ConfigOptions} config - The base karma configuration. | ||
* @param {Array<String>} plugins - A list of plugins to be required | ||
* @returns {Promise<karma.TestResults>} | ||
*/ | ||
function run(config, plugins) { | ||
return new Promise((resolve, reject) => { | ||
fork(path.resolve(`${__dirname}/KarmaWorker.js`)) | ||
.on('close', reject) | ||
.on('error', reject) | ||
.on('message', resolve) | ||
.send({ config, plugins }); | ||
}); | ||
} | ||
|
||
export default ScenarioUtils; |
Oops, something went wrong.