Skip to content

Commit

Permalink
chore(test): set up integration testing (#461)
Browse files Browse the repository at this point in the history
This will ensure that karma-webpack
can run as expected within a live
karma server.

Fixes #460
  • Loading branch information
codymikol committed Jan 14, 2021
1 parent f7af31f commit e8ad372
Show file tree
Hide file tree
Showing 8 changed files with 9,648 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"nsp": "^3.2.1",
"pre-commit": "^1.2.2",
"prettier": "^1.14.2",
"puppeteer": "^5.5.0",
"standard-version": "^4.4.0",
"webpack": "^5.10.0"
},
Expand Down
47 changes: 47 additions & 0 deletions test/integration/scenarios/basic-setup/basic-setup.test.js
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);
});
});
2 changes: 2 additions & 0 deletions test/integration/scenarios/basic-setup/index.scenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./js-source-example.js');
require('./js-test-example.js');
9 changes: 9 additions & 0 deletions test/integration/scenarios/basic-setup/js-source-example.js
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;
24 changes: 24 additions & 0 deletions test/integration/scenarios/basic-setup/js-test-example.js
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);
});
});
35 changes: 35 additions & 0 deletions test/integration/utils/KarmaWorker.js
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();
});
27 changes: 27 additions & 0 deletions test/integration/utils/ScenarioUtils.js
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;
Loading

0 comments on commit e8ad372

Please sign in to comment.