Skip to content

Commit

Permalink
test(testutils): use DOM instead of string comparison for integration…
Browse files Browse the repository at this point in the history
… tests

helps with failing smoke tests for adobe/helix-pipeline#281
  • Loading branch information
trieloff committed Apr 29, 2019
1 parent 90d28e5 commit 9a10673
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 18 deletions.
116 changes: 108 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"eslint-plugin-jsx-a11y": "6.1.1",
"eslint-plugin-react": "7.12.4",
"js-yaml": "3.13.1",
"jsdom": "^15.0.0",
"less": "3.8.1",
"lint-staged": "^8.1.5",
"mocha": "6.1.4",
Expand Down
21 changes: 11 additions & 10 deletions test/testUpCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { setupMocha: setupPolly } = require('@pollyjs/core');
const {
initGit,
assertHttp,
assertHttpDom,
assertFile,
createTestRoot,
} = require('./utils.js');
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('Integration test for up command', function suite() {
cmd
.on('started', async () => {
try {
await assertHttp(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response.html');
await assertHttp(`http://localhost:${cmd.project.server.port}/welcome.txt`, 200, 'welcome_response.txt');
myDone();
} catch (e) {
Expand Down Expand Up @@ -155,7 +156,7 @@ describe('Integration test for up command', function suite() {
});

try {
await assertHttp(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
} finally {
await cmd.stop();
}
Expand Down Expand Up @@ -186,8 +187,8 @@ describe('Integration test for up command', function suite() {
});

try {
await assertHttp(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
await assertHttp(`http://localhost:${cmd.project.server.port}/api/README.html`, 200, 'api_response_readme.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/api/README.html`, 200, 'api_response_readme.html');
} finally {
await cmd.stop();
}
Expand All @@ -211,7 +212,7 @@ describe('Integration test for up command', function suite() {
});

try {
await assertHttp(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/README.html`, 200, 'simple_response_readme.html');
} finally {
await cmd.stop();
}
Expand Down Expand Up @@ -276,8 +277,8 @@ describe('Integration test for up command', function suite() {
cmd
.on('started', async () => {
try {
await assertHttp(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response.html');
await assertHttp(`http://localhost:${cmd.project.server.port}/404.html`, 200, '404_response.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/404.html`, 200, '404_response.html');
await assertHttp(`http://localhost:${cmd.project.server.port}/welcome.txt`, 200, 'welcome_response.txt');
await assertHttp(`http://localhost:${cmd.project.server.port}/index.json`, 200, 'json_response.json');
await fse.copy(srcFile, dstFile);
Expand All @@ -290,7 +291,7 @@ describe('Integration test for up command', function suite() {
})
.on('build', async () => {
try {
await assertHttp(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response2.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/index.html`, 200, 'simple_response2.html');
await myDone();
} catch (e) {
await myDone(e);
Expand Down Expand Up @@ -325,7 +326,7 @@ describe('Integration test for up command', function suite() {
cmd
.on('started', async () => {
try {
await assertHttp(`http://localhost:${cmd.project.server.port}/index.dump.html`, 200, 'dump_response.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/index.dump.html`, 200, 'dump_response.html');
await fse.copy(srcFile, dstFile);
} catch (e) {
await myDone(e);
Expand All @@ -336,7 +337,7 @@ describe('Integration test for up command', function suite() {
})
.on('build', async () => {
try {
await assertHttp(`http://localhost:${cmd.project.server.port}/index.dump.html`, 200, 'dump_response2.html');
await assertHttpDom(`http://localhost:${cmd.project.server.port}/index.dump.html`, 200, 'dump_response2.html');
await myDone();
} catch (e) {
await myDone(e);
Expand Down
36 changes: 36 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const crypto = require('crypto');
const fse = require('fs-extra');
const http = require('http');
const unzip = require('unzip2');
const { JSDOM } = require('jsdom');
const { dom: { assertEquivalentNode } } = require('@adobe/helix-shared');
const BuildCommand = require('../src/build.cmd');

/**
Expand Down Expand Up @@ -100,6 +102,39 @@ async function assertHttp(url, status, spec, replacements = []) {
});
}

async function assertHttpDom(url, status, spec) {
return new Promise((resolve, reject) => {
let data = '';
http.get(url, (res) => {
try {
assert.equal(res.statusCode, status);
} catch (e) {
res.resume();
reject(e);
}

res
.on('data', (chunk) => {
data += chunk;
})
.on('end', () => {
try {
if (spec) {
const datadom = new JSDOM(data).window.document;
const specdom = new JSDOM(fse.readFileSync(path.resolve(__dirname, 'specs', spec)).toString()).window.document;
assertEquivalentNode(datadom, specdom);
}
resolve();
} catch (e) {
reject(e);
}
});
}).on('error', (e) => {
reject(e);
});
});
}

async function assertZipEntries(zipPath, entries) {
assertFile(zipPath);

Expand Down Expand Up @@ -270,6 +305,7 @@ module.exports = {
assertFile,
assertFileEqual,
assertHttp,
assertHttpDom,
initGit,
createTestRoot,
createFakeTestRoot,
Expand Down

0 comments on commit 9a10673

Please sign in to comment.