feat(learn): lazy load Enzyme in the test frame
This commit is contained in:
parent
ce902c7475
commit
00a1d25fbb
@ -9,7 +9,10 @@ if (window.frameElement && window.frameElement.id === testId) {
|
|||||||
document.addEventListener('DOMContentLoaded', initTestFrame);
|
document.addEventListener('DOMContentLoaded', initTestFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initTestFrame() {
|
// For tests in CI.
|
||||||
|
document.__initTestFrame = initTestFrame;
|
||||||
|
|
||||||
|
async function initTestFrame() {
|
||||||
const code = (document.__source || '').slice(0);
|
const code = (document.__source || '').slice(0);
|
||||||
if (!document.__getUserInput) {
|
if (!document.__getUserInput) {
|
||||||
document.__getUserInput = () => code;
|
document.__getUserInput = () => code;
|
||||||
@ -38,8 +41,16 @@ function initTestFrame() {
|
|||||||
const assert = chai.assert;
|
const assert = chai.assert;
|
||||||
/* eslint-enable no-unused-vars */
|
/* eslint-enable no-unused-vars */
|
||||||
|
|
||||||
if (document.Enzyme) {
|
let Enzyme;
|
||||||
window.Enzyme = document.Enzyme;
|
if (document.__loadEnzyme) {
|
||||||
|
let Adapter16;
|
||||||
|
/* eslint-disable no-inline-comments */
|
||||||
|
[{ default: Enzyme }, { default: Adapter16 }] = await Promise.all([
|
||||||
|
import(/* webpackChunkName: "enzyme" */ 'enzyme'),
|
||||||
|
import(/* webpackChunkName: "enzyme-adapter" */ 'enzyme-adapter-react-16')
|
||||||
|
]);
|
||||||
|
/* eslint-enable no-inline-comments */
|
||||||
|
Enzyme.configure({ adapter: new Adapter16() });
|
||||||
}
|
}
|
||||||
|
|
||||||
document.__runTest = async function runTests(testString) {
|
document.__runTest = async function runTests(testString) {
|
||||||
|
@ -112,6 +112,7 @@ function* executeDOMChallengeSaga(proxyLogger) {
|
|||||||
const meta = yield select(challengeMetaSelector);
|
const meta = yield select(challengeMetaSelector);
|
||||||
const document = yield getContext('document');
|
const document = yield getContext('document');
|
||||||
const ctx = yield call(buildDOMChallenge, files, meta);
|
const ctx = yield call(buildDOMChallenge, files, meta);
|
||||||
|
ctx.loadEnzyme = Object.keys(files).some(key => files[key].ext === 'jsx');
|
||||||
yield call(createTestFrame, document, ctx, proxyLogger);
|
yield call(createTestFrame, document, ctx, proxyLogger);
|
||||||
// wait for a code execution on a "ready" event in jQuery challenges
|
// wait for a code execution on a "ready" event in jQuery challenges
|
||||||
yield delay(100);
|
yield delay(100);
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
import { toString, flow } from 'lodash';
|
import { toString, flow } from 'lodash';
|
||||||
import { configure, shallow, mount } from 'enzyme';
|
|
||||||
import Adapter16 from 'enzyme-adapter-react-16';
|
|
||||||
import { setConfig } from 'react-hot-loader';
|
|
||||||
|
|
||||||
// we use two different frames to make them all essentially pure functions
|
// we use two different frames to make them all essentially pure functions
|
||||||
// main iframe is responsible rendering the preview and is where we proxy the
|
// main iframe is responsible rendering the preview and is where we proxy the
|
||||||
@ -33,17 +30,10 @@ const createHeader = (id = mainId) => `
|
|||||||
|
|
||||||
export const runTestInTestFrame = async function(document, test, timeout) {
|
export const runTestInTestFrame = async function(document, test, timeout) {
|
||||||
const { contentDocument: frame } = document.getElementById(testId);
|
const { contentDocument: frame } = document.getElementById(testId);
|
||||||
// Enable Stateless Functional Component. Otherwise, enzyme-adapter-react-16
|
|
||||||
// does not work correctly.
|
|
||||||
setConfig({ pureSFC: true });
|
|
||||||
try {
|
|
||||||
return await Promise.race([
|
return await Promise.race([
|
||||||
new Promise((_, reject) => setTimeout(() => reject('timeout'), timeout)),
|
new Promise((_, reject) => setTimeout(() => reject('timeout'), timeout)),
|
||||||
frame.__runTest(test)
|
frame.__runTest(test)
|
||||||
]);
|
]);
|
||||||
} finally {
|
|
||||||
setConfig({ pureSFC: false });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const createFrame = (document, id) => ctx => {
|
const createFrame = (document, id) => ctx => {
|
||||||
@ -83,18 +73,14 @@ const buildProxyConsole = proxyLogger => ctx => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const writeTestDepsToDocument = frameReady => ctx => {
|
const writeTestDepsToDocument = frameReady => ctx => {
|
||||||
const { sources } = ctx;
|
const { sources, loadEnzyme } = ctx;
|
||||||
// add enzyme
|
|
||||||
// TODO: do programatically
|
|
||||||
// TODO: webpack lazyload this
|
|
||||||
configure({ adapter: new Adapter16() });
|
|
||||||
ctx.document.Enzyme = { shallow, mount };
|
|
||||||
// default for classic challenges
|
// default for classic challenges
|
||||||
// should not be used for modern
|
// should not be used for modern
|
||||||
ctx.document.__source = sources && 'index' in sources ? sources['index'] : '';
|
ctx.document.__source = sources && 'index' in sources ? sources['index'] : '';
|
||||||
// provide the file name and get the original source
|
// provide the file name and get the original source
|
||||||
ctx.document.__getUserInput = fileName => toString(sources[fileName]);
|
ctx.document.__getUserInput = fileName => toString(sources[fileName]);
|
||||||
ctx.document.__frameReady = frameReady;
|
ctx.document.__frameReady = frameReady;
|
||||||
|
ctx.document.__loadEnzyme = loadEnzyme;
|
||||||
return ctx;
|
return ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ module.exports = (env = {}) => {
|
|||||||
},
|
},
|
||||||
devtool: __DEV__ ? 'inline-source-map' : 'source-map',
|
devtool: __DEV__ ? 'inline-source-map' : 'source-map',
|
||||||
output: {
|
output: {
|
||||||
|
publicPath: '/js/',
|
||||||
|
chunkFilename: '[name].js',
|
||||||
path: path.join(__dirname, './static/js')
|
path: path.join(__dirname, './static/js')
|
||||||
},
|
},
|
||||||
stats: {
|
stats: {
|
||||||
@ -32,7 +34,10 @@ module.exports = (env = {}) => {
|
|||||||
{ modules: false, targets: '> 0.25%, not dead' }
|
{ modules: false, targets: '> 0.25%, not dead' }
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
plugins: ['@babel/plugin-transform-runtime']
|
plugins: [
|
||||||
|
'@babel/plugin-transform-runtime',
|
||||||
|
'@babel/plugin-syntax-dynamic-import'
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user