Skip to content

Commit b41e696

Browse files
authored
Add browser test for graphql (#5263)
1 parent 736561f commit b41e696

File tree

13 files changed

+178
-38
lines changed

13 files changed

+178
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`graphql with mjs entrypoint correctly bundles files in development 1`] = `"Pikachu"`;
4+
5+
exports[`graphql with mjs entrypoint correctly bundles files in production 1`] = `"Pikachu"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const {
2+
bootstrap,
3+
startDevelopmentServer,
4+
startProductionServer,
5+
} = require('../../utils');
6+
const puppeteer = require('puppeteer');
7+
8+
beforeEach(async () => {
9+
await bootstrap({ directory: global.testDirectory, template: __dirname });
10+
global.appDevPort = await startDevelopmentServer({
11+
directory: global.testDirectory,
12+
});
13+
global.appProdPort = await startProductionServer({
14+
directory: global.testDirectory,
15+
});
16+
// Wait for serve to boot up
17+
await new Promise(resolve => setTimeout(resolve, 1000));
18+
});
19+
20+
// https://github.com/facebook/create-react-app/issues/5234
21+
// https://github.com/facebook/create-react-app/pull/5258
22+
describe('graphql with mjs entrypoint', () => {
23+
it('correctly bundles files in development', async () => {
24+
const browser = await puppeteer.launch({ headless: true });
25+
try {
26+
const page = await browser.newPage();
27+
await page.goto(`http://localhost:${global.appDevPort}/`);
28+
await page.waitForSelector('.Pokemon-Name-Data');
29+
const output = await page.evaluate(() => {
30+
return Array.from(
31+
document.getElementsByClassName('Pokemon-Name-Data')
32+
).pop().innerHTML;
33+
});
34+
expect(output).toMatchSnapshot();
35+
} finally {
36+
browser.close();
37+
}
38+
});
39+
40+
it('correctly bundles files in production', async () => {
41+
const browser = await puppeteer.launch({ headless: true });
42+
try {
43+
const page = await browser.newPage();
44+
await page.goto(`http://localhost:${global.appProdPort}/`);
45+
await page.waitForSelector('.Pokemon-Name-Data');
46+
const output = await page.evaluate(() => {
47+
return Array.from(
48+
document.getElementsByClassName('Pokemon-Name-Data')
49+
).pop().innerHTML;
50+
});
51+
expect(output).toMatchSnapshot();
52+
} finally {
53+
browser.close();
54+
}
55+
});
56+
});

fixtures/smoke/graphql-with-mjs/package.json fixtures/browser/graphql-with-mjs/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"graphql": "14.0.2",
55
"react-apollo": "2.2.1",
66
"react": "latest",
7-
"react-dom": "latest"
7+
"react-dom": "latest",
8+
"serve": "10.0.2"
89
}
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { Component } from 'react';
2+
import ApolloClient, { gql } from 'apollo-boost';
3+
import { ApolloProvider, Query } from 'react-apollo';
4+
5+
const GET_PIKA = gql`
6+
{
7+
pokemon(name: "Pikachu") {
8+
name
9+
}
10+
}
11+
`;
12+
13+
const client = new ApolloClient({
14+
uri: 'https://graphql-pokemon.now.sh/graphql',
15+
});
16+
17+
class Pokemon extends Component {
18+
render() {
19+
const { name } = this.props.pokemon;
20+
return (
21+
<h1>
22+
Pokemon name: <span className="Pokemon-Name-Data">{name}</span>
23+
</h1>
24+
);
25+
}
26+
}
27+
28+
class Data extends Component {
29+
state = {};
30+
componentDidCatch() {
31+
this.setState({ hasError: true });
32+
}
33+
render() {
34+
const { hasError } = this.state;
35+
return hasError ? (
36+
<div className="Pokemon-Name-Data">Error :(</div>
37+
) : (
38+
<Query query={GET_PIKA}>
39+
{({ loading, error, data }) => {
40+
if (loading) {
41+
return <div>Loading...</div>;
42+
}
43+
if (error) {
44+
return <div className="Pokemon-Name-Data">Error :(</div>;
45+
}
46+
return <Pokemon pokemon={data.pokemon} />;
47+
}}
48+
</Query>
49+
);
50+
}
51+
}
52+
53+
class App extends Component {
54+
render() {
55+
return (
56+
<ApolloProvider client={client}>
57+
<Data />
58+
</ApolloProvider>
59+
);
60+
}
61+
}
62+
63+
export default App;

fixtures/browser/jest.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testMatch: ['**/*.test.js'],
4+
testPathIgnorePatterns: ['/src/', 'node_modules'],
5+
setupTestFrameworkScriptFile: './setupBrowserTests.js',
6+
forceExit: true,
7+
};

fixtures/browser/setupBrowserTests.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs-extra');
2+
const tempy = require('tempy');
3+
beforeEach(() => {
4+
global.testDirectory = tempy.directory();
5+
jest.setTimeout(1000 * 60 * 5);
6+
});
7+
afterEach(() => {
8+
fs.removeSync(global.testDirectory);
9+
});

fixtures/smoke/graphql-with-mjs/index.test.js

-17
This file was deleted.

fixtures/smoke/graphql-with-mjs/src/App.js

-20
This file was deleted.

fixtures/utils.js

+32
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,43 @@ async function getOutputProduction({ directory, env = {} }) {
135135
}
136136
}
137137

138+
async function startDevelopmentServer({ directory, env = {} }) {
139+
const port = await getPort();
140+
execa('./node_modules/.bin/react-scripts', ['start'], {
141+
cwd: directory,
142+
env: Object.assign(
143+
{},
144+
{
145+
BROWSER: 'none',
146+
PORT: port,
147+
CI: 'false',
148+
FORCE_COLOR: '0',
149+
},
150+
env
151+
),
152+
});
153+
return port;
154+
}
155+
156+
async function startProductionServer({ directory, env = {} }) {
157+
const port = await getPort();
158+
await execa('./node_modules/.bin/react-scripts', ['build'], {
159+
cwd: directory,
160+
env: Object.assign({}, { CI: 'false' }, env),
161+
});
162+
execa('./node_modules/.bin/serve', ['-s', 'build', '-p', port], {
163+
cwd: directory,
164+
});
165+
return port;
166+
}
167+
138168
module.exports = {
139169
bootstrap,
140170
isSuccessfulDevelopment,
141171
isSuccessfulProduction,
142172
isSuccessfulTest,
143173
getOutputDevelopment,
144174
getOutputProduction,
175+
startDevelopmentServer,
176+
startProductionServer,
145177
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"meow": "^5.0.0",
3333
"multimatch": "^2.1.0",
3434
"prettier": "1.14.3",
35+
"puppeteer": "^1.8.0",
3536
"strip-ansi": "^4.0.0",
3637
"svg-term-cli": "^2.1.1",
3738
"tempy": "^0.2.1"

tasks/e2e-behavior.sh

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ git clean -df
9595
# Now that we have published them, run all tests as if they were released.
9696
# ******************************************************************************
9797

98+
# Browser tests
99+
CI=true ./node_modules/.bin/jest --config fixtures/browser/jest.config.js
100+
98101
# Smoke tests
99102
CI=true ./node_modules/.bin/jest --config fixtures/smoke/jest.config.js
100103

0 commit comments

Comments
 (0)