Skip to content

Commit 885ce57

Browse files
committed
test(wtr): shift logic out of wrapper into static test runner file
we want to keep the generated wrappers as thin as possible and use regular js files as much as possible
1 parent 8ff59c5 commit 885ce57

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

packages/@lwc/integration-not-karma/configs/plugins/serve-hydration.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,14 @@ async function wrapHydrationTest(filePath) {
178178
: await getCompiledModule(suiteDir, true);
179179
const ssrOutput = await getSsrCode(componentDefSSR, filePath, expectedSSRConsoleCalls);
180180

181-
// FIXME: can we turn these IIFEs into ESM imports?
182181
return `
183182
import { runTest } from '/helpers/test-hydrate.js';
184-
import config from '/${filePath}?original=1';
185-
import Component from '/${componentEntrypoint}';
186-
187-
${onlyFileExists ? 'it.only' : 'it'}('${filePath}', async () => {
188-
const ssrRendered = ${JSON.stringify(ssrOutput) /* escape quotes */};
189-
return await runTest(ssrRendered, Component, config);
190-
});
183+
runTest(
184+
'/${filePath}?original=1',
185+
'/${componentEntrypoint}',
186+
${JSON.stringify(ssrOutput) /* escape quotes */},
187+
${onlyFileExists}
188+
);
191189
`;
192190
} finally {
193191
requiredFeatureFlags?.forEach((featureFlag) => {

packages/@lwc/integration-not-karma/helpers/test-hydrate.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,42 @@ function setFeatureFlags(requiredFeatureFlags, value) {
2222
});
2323
}
2424

25-
async function runTest(ssrRendered, Component, testConfig) {
26-
const container = appendTestTarget(ssrRendered);
27-
const selector = container.firstChild.tagName.toLowerCase();
28-
let target = container.querySelector(selector);
29-
30-
let testResult;
31-
const consoleSpy = spyConsole();
32-
setFeatureFlags(testConfig.requiredFeatureFlags, true);
33-
34-
if (testConfig.test) {
35-
const snapshot = testConfig.snapshot ? testConfig.snapshot(target) : {};
36-
37-
const props = testConfig.props || {};
38-
const clientProps = testConfig.clientProps || props;
39-
40-
LWC.hydrateComponent(target, Component, clientProps);
41-
42-
// let's select again the target, it should be the same elements as in the snapshot
43-
target = container.querySelector(selector);
44-
testResult = await testConfig.test(target, snapshot, consoleSpy.calls);
45-
} else if (testConfig.advancedTest) {
46-
testResult = await testConfig.advancedTest(target, {
47-
Component,
48-
hydrateComponent: LWC.hydrateComponent.bind(LWC),
49-
consoleSpy,
50-
container,
51-
selector,
52-
});
53-
}
54-
55-
consoleSpy.reset();
56-
57-
return testResult;
25+
export function runTest(configPath, componentPath, ssrRendered, focused) {
26+
const test = focused ? it.only : it;
27+
test(configPath, async () => {
28+
const testConfig = await import(configPath);
29+
const Component = await import(componentPath);
30+
const container = appendTestTarget(ssrRendered);
31+
const selector = container.firstChild.tagName.toLowerCase();
32+
let target = container.querySelector(selector);
33+
34+
let testResult;
35+
const consoleSpy = spyConsole();
36+
setFeatureFlags(testConfig.requiredFeatureFlags, true);
37+
38+
if (testConfig.test) {
39+
const snapshot = testConfig.snapshot ? testConfig.snapshot(target) : {};
40+
41+
const props = testConfig.props || {};
42+
const clientProps = testConfig.clientProps || props;
43+
44+
LWC.hydrateComponent(target, Component, clientProps);
45+
46+
// let's select again the target, it should be the same elements as in the snapshot
47+
target = container.querySelector(selector);
48+
testResult = await testConfig.test(target, snapshot, consoleSpy.calls);
49+
} else if (testConfig.advancedTest) {
50+
testResult = await testConfig.advancedTest(target, {
51+
Component,
52+
hydrateComponent: LWC.hydrateComponent.bind(LWC),
53+
consoleSpy,
54+
container,
55+
selector,
56+
});
57+
}
58+
59+
consoleSpy.reset();
60+
61+
return testResult;
62+
});
5863
}
59-
60-
export { runTest };

0 commit comments

Comments
 (0)