Skip to content

Commit b4b64b4

Browse files
committed
Alert the user if ECFT thinks that an HTTP interceptor blocked rendering
* Check for an x-fastboot-testing header that is set by Ember CLI * Make sure there are no mirage errors * Make sure that fetch doesnt throw
1 parent bedb558 commit b4b64b4

7 files changed

Lines changed: 91 additions & 35 deletions

File tree

addon-test-support/index.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export function setup(hooks) {
1313
}
1414

1515
export async function fastboot(url, { headers = {} }) {
16-
let endpoint = `/__fastboot-testing?${param({url, headers})}`;
17-
let response = await fetch(endpoint);
16+
let response = await fetchFromEmberCli(url, headers);
1817
let result = await response.json();
1918

2019
let body = result.err ?
@@ -28,40 +27,49 @@ export async function fastboot(url, { headers = {} }) {
2827
}
2928

3029
export async function visit(url, options = {}) {
31-
let result;
30+
let result = await fastboot(url, { headers: options.headers || {} });
31+
32+
document.querySelector('#ember-testing').innerHTML = result.body;
33+
34+
return result;
35+
}
36+
37+
// private
38+
39+
let fetchFromEmberCli = async function(url, headers) {
40+
let endpoint = `/__fastboot-testing?${param({url, headers})}`;
41+
let response;
42+
let error;
3243

3344
try {
34-
result = await fastboot(url, { headers: options.headers || {} });
45+
response = await fetch(endpoint);
3546
} catch (e) {
36-
let message;
37-
3847
if (e.message && e.message.match(/^Mirage:/)) {
39-
message = `Ember CLI FastBoot Testing: It looks like Mirage is intercepting ember-cli-fastboot-testing's attempt to render ${url}. Please disable Mirage when running FastBoot tests.`;
40-
console.error(message);
41-
throw new Error(message);
42-
} else if (e.message) {
43-
message = `Ember CLI FastBoot Testing: generic error`
48+
error = `Ember CLI FastBoot Testing: It looks like Mirage is intercepting ember-cli-fastboot-testing's attempt to render ${url}. Please disable Mirage when running FastBoot tests.`;
4449
} else {
45-
50+
error = `Ember CLI FastBoot Testing: We were unable to render ${url}. Is your test suite blocking or intercepting HTTP requests? Error: ${e.message ? e.message : e}.`
4651
}
47-
4852
}
4953

50-
document.querySelector('#ember-testing').innerHTML = result.body;
54+
if (response && response.headers && response.headers.get && response.headers.get('x-fastboot-testing') !== 'true') {
55+
error = `Ember CLI FastBoot Testing: We were unable to render ${url}. Is your test suite blocking or intercepting HTTP requests?`;
56+
}
5157

52-
return result;
53-
}
58+
if (error) {
59+
// eslint-disable-next-line no-console
60+
console.error(error);
61+
throw new Error(error);
62+
}
5463

55-
export function renderedHtml() {
56-
return document.querySelector('#ember-testing').innerHTML;
57-
}
64+
return response;
65+
};
5866

59-
export function parseHtml(str) {
67+
let parseHtml = function(str) {
6068
let parser = new DOMParser();
6169
return parser.parseFromString(str, "text/html");
6270
}
6371

64-
export function extractBody(html) {
72+
let extractBody = function(html) {
6573
let start = '<script type="x/boundary" id="fastboot-body-start"></script>';
6674
let end = '<script type="x/boundary" id="fastboot-body-end"></script>';
6775

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ module.exports = {
5353
response: {}
5454
};
5555

56+
res.set('x-fastboot-testing', true);
57+
5658
this.fastboot
5759
.visit(urlToVisit, options)
5860
.then(page => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"eslint-plugin-ember": "^5.0.0",
6464
"eslint-plugin-node": "^6.0.1",
6565
"loader.js": "^4.2.3",
66+
"pretender": "~2.1.0",
6667
"qunit-dom": "^0.6.2"
6768
},
6869
"engines": {

tests/fastboot/basic-test.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import { module, test, skip } from 'qunit';
2-
import { setup, visit, renderedHtml } from 'ember-cli-fastboot-testing/test-support';
1+
import { module, test } from 'qunit';
2+
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';
33

44
module('Fastboot | basic', function(hooks) {
55
setup(hooks);
66

7-
skip('it renders html that matches the browser', async function(assert) {
8-
let { body } = await visit('/');
9-
10-
assert.equal(body, renderedHtml());
11-
});
12-
137
test('it renders the correct h1 title', async function(assert) {
148
await visit('/');
159

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { module, test } from 'qunit';
2+
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';
3+
import Pretender from 'pretender';
4+
5+
/*
6+
This test is setup to emulate the following scenario: Someone is using
7+
FastBoot testing as well as an http interceptor for mocking.
8+
When FastBoot testing asks ember-cli to render a page, the interceptor
9+
will block the http request and attempt to return a mock.
10+
11+
We'll simulate this by overriding fetch.
12+
13+
We want to provide a better error message when that happens.
14+
*/
15+
module('Fastboot | generic interceptor', function(hooks) {
16+
setup(hooks);
17+
18+
test('it doesnt work if an interceptor blocks our request to ember-cli', async function(assert) {
19+
let server = new Pretender(function() {
20+
this.get('/__fastboot-testing', () => {
21+
throw new Error("Blocked!");
22+
});
23+
});
24+
25+
assert.rejects(
26+
visit('/'),
27+
/We were unable to render \/. Is your test suite blocking or intercepting HTTP requests\? Error: Pretender intercepted GET \/__fastboot-testing\?url=%2F but encountered an error: Blocked/
28+
);
29+
30+
server.shutdown();
31+
});
32+
33+
test('mocked response', function(assert) {
34+
let server = new Pretender(function() {
35+
this.get('/__fastboot-testing', () => {
36+
return [
37+
200,
38+
{"Content-Type": "application/json"},
39+
JSON.stringify({ mocked: true })
40+
];
41+
});
42+
});
43+
44+
assert.rejects(
45+
visit('/'),
46+
/We were unable to render \/. Is your test suite blocking or intercepting HTTP requests\?/
47+
);
48+
49+
server.shutdown();
50+
});
51+
});

tests/fastboot/http-interceptors-test.js renamed to tests/fastboot/mirage-interceptors-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
1111
1212
We want to provide a better error message when that happens.
1313
*/
14-
module('Fastboot | http interceptors', function(hooks) {
14+
module('Fastboot | mirage interceptor', function(hooks) {
1515
setup(hooks);
1616
setupMirage(hooks);
1717

1818
test('it doesnt work if mirage blocks our http request to ember-cli', async function(assert) {
19-
await visit('/');
20-
21-
22-
19+
assert.rejects(
20+
visit('/'),
21+
/It looks like Mirage is intercepting ember-cli-fastboot-testing's attempt to render \//
22+
);
2323
});
2424

2525
});

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9994,7 +9994,7 @@ preserve@^0.2.0:
99949994
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
99959995
integrity sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=
99969996

9997-
pretender@2.1.1:
9997+
pretender@2.1.1, pretender@~2.1.0:
99989998
version "2.1.1"
99999999
resolved "https://registry.yarnpkg.com/pretender/-/pretender-2.1.1.tgz#5085f0a1272c31d5b57c488386f69e6ca207cb35"
1000010000
integrity sha512-IkidsJzaroAanw3I43tKCFm2xCpurkQr9aPXv5/jpN+LfCwDaeI8rngVWtQZTx4qqbhc5zJspnLHJ4N/25KvDQ==

0 commit comments

Comments
 (0)