Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): set up integration testing #461

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "^4.17.2"
},
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',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be the reason, why you don't see logs?

Copy link
Owner Author

@codymikol codymikol Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added right at the end to keep the tests from being noisy, unrelated to the issue I was having. I'll try to get a repro up sometime today.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this made me realize that I totally missed pushing one of my files though 🤦‍♂️

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