Skip to content

Commit e18d3b3

Browse files
authored
Merge pull request #43 from embermap/notify-if-fake-server-intercepts
Notify if fake server intercepts
2 parents 8d631c9 + b4b64b4 commit e18d3b3

11 files changed

Lines changed: 239 additions & 18 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
2+
globals: {
3+
server: true,
4+
},
25
root: true,
36
parserOptions: {
47
ecmaVersion: 2017,

addon-test-support/index.js

Lines changed: 32 additions & 7 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 ?
@@ -35,16 +34,42 @@ export async function visit(url, options = {}) {
3534
return result;
3635
}
3736

38-
export function renderedHtml() {
39-
return document.querySelector('#ember-testing').innerHTML;
40-
}
37+
// private
38+
39+
let fetchFromEmberCli = async function(url, headers) {
40+
let endpoint = `/__fastboot-testing?${param({url, headers})}`;
41+
let response;
42+
let error;
43+
44+
try {
45+
response = await fetch(endpoint);
46+
} catch (e) {
47+
if (e.message && e.message.match(/^Mirage:/)) {
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.`;
49+
} else {
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}.`
51+
}
52+
}
53+
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+
}
57+
58+
if (error) {
59+
// eslint-disable-next-line no-console
60+
console.error(error);
61+
throw new Error(error);
62+
}
63+
64+
return response;
65+
};
4166

42-
export function parseHtml(str) {
67+
let parseHtml = function(str) {
4368
let parser = new DOMParser();
4469
return parser.parseFromString(str, "text/html");
4570
}
4671

47-
export function extractBody(html) {
72+
let extractBody = function(html) {
4873
let start = '<script type="x/boundary" id="fastboot-body-start"></script>';
4974
let end = '<script type="x/boundary" id="fastboot-body-end"></script>';
5075

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ module.exports = {
5858
response: {}
5959
};
6060

61+
res.set('x-fastboot-testing', true);
62+
6163
this.fastboot
6264
.visit(urlToVisit, options)
6365
.then(page => {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"ember-cli-htmlbars": "^2.0.1",
4747
"ember-cli-htmlbars-inline-precompile": "^1.0.0",
4848
"ember-cli-inject-live-reload": "^1.4.1",
49+
"ember-cli-mirage": "^0.4.15",
4950
"ember-cli-qunit": "^4.3.2",
5051
"ember-cli-shims": "^1.2.0",
5152
"ember-cli-sri": "^2.1.0",
@@ -62,6 +63,7 @@
6263
"eslint-plugin-ember": "^5.0.0",
6364
"eslint-plugin-node": "^6.0.1",
6465
"loader.js": "^4.2.3",
66+
"pretender": "~2.1.0",
6567
"qunit-dom": "^0.6.2"
6668
},
6769
"engines": {

tests/dummy/mirage/config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default function() {
2+
3+
// These comments are here to help you get started. Feel free to delete them.
4+
5+
/*
6+
Config (with defaults).
7+
8+
Note: these only affect routes defined *after* them!
9+
*/
10+
11+
// this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server
12+
// this.namespace = ''; // make this `/api`, for example, if your API is namespaced
13+
// this.timing = 400; // delay for each request, automatically set to 0 during testing
14+
15+
/*
16+
Shorthand cheatsheet:
17+
18+
this.get('/posts');
19+
this.post('/posts');
20+
this.get('/posts/:id');
21+
this.put('/posts/:id'); // or this.patch
22+
this.del('/posts/:id');
23+
24+
http://www.ember-cli-mirage.com/docs/v0.4.x/shorthands/
25+
*/
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function(/* server */) {
2+
3+
/*
4+
Seed your development database using your factories.
5+
This data will not be loaded in your tests.
6+
*/
7+
8+
// server.createList('post', 10);
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { JSONAPISerializer } from 'ember-cli-mirage';
2+
3+
export default JSONAPISerializer.extend({
4+
});

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+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { module, test } from 'qunit';
2+
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';
3+
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
4+
5+
6+
/*
7+
This test is setup to emulate the following scenario: Someone is using
8+
FastBoot testing as well as a local mirage server. When FastBoot testing
9+
asks ember-cli to render a page, mirage will intercept the http request and
10+
then say it didnt know how to handle the request.
11+
12+
We want to provide a better error message when that happens.
13+
*/
14+
module('Fastboot | mirage interceptor', function(hooks) {
15+
setup(hooks);
16+
setupMirage(hooks);
17+
18+
test('it doesnt work if mirage blocks our http request to ember-cli', async function(assert) {
19+
assert.rejects(
20+
visit('/'),
21+
/It looks like Mirage is intercepting ember-cli-fastboot-testing's attempt to render \//
22+
);
23+
});
24+
25+
});

0 commit comments

Comments
 (0)